I am trying to put a plot inside a box in a shiny app. The size of plot changes so currently I am defining height of box, which is not a solution. I want a vertically scroll-able plot which adjust its width automatically and box height equal to height of window.
This is what I have tried:
library(shiny)
library(shinydashboard)
ui <-dashboardPage(
dashboardHeader(),
dashboardSidebar(sidebarMenu(
menuItem("Box Plot", tabName = "boxplot")
)),
dashboardBody(
tabItems(tabItem(tabName = "boxplot",
fluidPage(fluidRow(column(4),
column(8, box(title = "Box Plot", width = NULL,
solidHeader = TRUE, status = "primary", collapsible = TRUE,
plotOutput("plot1", inline=F, width="100%", height=1500), style = 'overflow-y: scroll;')
)))))))
server <- shinyServer(function(input, output, session) {
output$plot1 <- renderPlot({
x <- data.frame(matrix(rnorm(1000), ncol = 20))
input_data <- rnorm(ncol(x))
d <- data.frame(x)
plot.data <- gather(d, variable, value)
plot.data$test_data <- as.numeric(rep(input_data, each = nrow(x)))
p = ggplot(plot.data, aes(x = 0, y=value)) +
geom_boxplot() +
geom_point(aes(x = 0, y = test_data), color = "red") +
facet_wrap(~variable, scales = "free_y", switch = "y", nrow = 1) +
xlab("") + ylab("") +
theme(legend.position="none") + theme_bw() + theme(axis.text.x=element_blank(),
axis.text.y=element_text(angle=90),
axis.ticks.x=element_blank())
print(p, vp=viewport(angle=270, width = unit(3, "npc"), height = unit(0.32, "npc")))
})
})
shinyApp(ui = ui, server = server)