I want to include multiple plots of varying heights within a single shiny dashboard tab.
Many existing answers cover setting the height parameter in ggplotly. Using this parameter does change how a figure will render. But, it doesn't fix my problem. Objects around it render as if the adjusted figure still has its original height. For example, if the first figure is lengthened, the second figure will appear over top of it. Or, if the first figure is shortened, there will be a large empty space before the next figure.
Alexander Leow provides one solution to my problem in his buried answer to the following question: Shiny plotlyOutput() not responding to height and width sizes
Unfortunately, his answer seems unnecessarily complicated by the multiple plot list structure.
I tried to adapt his solution for a single plot but I was unsuccessful. The following code produces an error when running in an app: "object type closure is not subsettable"
library(plotly)
library(shiny)
library(shinydashboard)
ui <- dashboardPage(dashboardHeader(title = "Test"),
dashboardSidebar(
menuItem("Test", tabName = "test")
),
dashboardBody(
tabItems(
tabItem(tabName = "test",
fluidRow(
column(12, uiOutput('plot'))
)
)
)
)
)
server <- function(input, output) {
output$plot <- renderUI({
plot_output_object <- renderPlotly({
p <- ggplot() +
geom_point(aes(x=1,y=2))
ggplotly(p)
})
attr(plot_output_object,'outputArgs') <- list(height="200px")
return(plot_output_object)
})
}
shinyApp(ui = ui, server = server)
The following example demonstrates how the ggplotly height parameter does not solve my issue. There is a large gap between the first and second plots.
library(shiny)
library(shinydashboard)
library(plotly)
ui <- dashboardPage(dashboardHeader(title = "Test"),
dashboardSidebar(
menuItem("Test", tabName = "test")
),
dashboardBody(
tabItems(
tabItem(tabName = "test",
fluidRow(
column(12, plotlyOutput('plot'))
),
fluidRow(
column(12, plotlyOutput('plot2'))
)
)
)
)
)
server <- function(input, output, session) {
output$plot <- renderPlotly({
p <- ggplot() + geom_point(aes(x = 1, y = 2))
ggplotly(p, height = 200)
})
output$plot2 <- renderPlotly({
p2 <- ggplot() + geom_point(aes(x = 1, y = 2))
ggplotly(p2)
})
}
shinyApp(ui = ui, server = server)