since couple of days I am trying to find solution to plot appearing wrongly (beyond box width) in shiny app while using function show/hide sidebar.
Here is example code:
library(shiny)
library(shinydashboard)
library(ggplot2)
library(shinyjs)
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(),
dashboardBody(
useShinyjs(),
extendShinyjs(text = 'shinyjs.hideSidebar = function(params) { $("body").addClass("sidebar-collapse") }'),
extendShinyjs(text='shinyjs.showSidebar = function(params) { $("body").removeClass("sidebar-collapse") }'),
fluidRow(tabsetPanel(id='tabs',
tabPanel(value=1,title="Plot1",
fluidRow(
column(12,
plotOutput('plot1', height=800)))),
tabPanel(value=2, title="Plot2",
fluidRow(
column(12,
plotOutput('plot2', height=800))))
)
)))
)
)
server <- function(input, output, session) {
output$plot1 <- renderPlot({
out <- ggplot(data.frame(X1=rnorm(1000)),aes(X1))+
geom_density(fill='light blue')+
theme_minimal()
print(out)
})
output$plot2 <- renderPlot({
out <- ggplot(data.frame(X1=rnorm(1000)),aes(X1))+
geom_density(fill='light blue')+
theme_minimal()
print(out)
})
observe({
if (input$tabs == 1) {
js$hideSidebar()
}
else {
js$showSidebar()
}
})
}
shinyApp(ui, server)
As we can see in this example code, i would like the sidebar to appear when the user opens the second tab (sidebar collapsed when input$tabs == 1
). It is working pretty good, except when i press to see tab2, then go back to tab1 and again to tab2, the plot is resized and the x axis is cut: