Problem
I use the code below to dynamically change the size of a shinydashboard::box via javascript. This works fine in principle, but the image from plotOutput inside the box does not re-render properly in terms of size. That is, the plot width stays the same irrespective of the size of the surrounding box. Funny enough, when I use Inspect in Chrome to have a look at the source code, the graph suddenly gets resized.
Screenshot
Question
How do I need to adapt my code to resize the plot on input$cols changes? It is important to mention that in my real code, the content of the box is dynamically rendered via modules. Thus, I have little control over the renderPlot function itself. As the image gets resized as soon as I 'inspect' it, I hope that there is a javascript function which I could call, which does the job for me.
Code
library(shiny)
library(shinydashboard)
library(shinyjs)
library(glue)
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(selectInput("cols", NULL, c(2, 3, 4, 6, 12), 4)),
dashboardBody(
useShinyjs(),
div(
box(solidHeader = TRUE,
title = "Box",
width = 4,
status = "info",
sliderInput("sld", "n:", 1, 100, 50),
plotOutput("plt")
), id = "box-parent")
))
server <- function(input, output) {
observe({
cols <- req(input$cols)
runjs(code = glue('var $el = $("#box-parent > :first");',
'$el.removeClass(function (index, className) {{',
'return (className.match(/(^|\\s)col-sm-\\d+/g) || []).join(" ")',
'}});',
'$el.addClass("col-sm-{cols}");'))
})
output$plt <- renderPlot(plot(rnorm(input$sld), rnorm(input$sld)))
}
shinyApp(ui, server)

$(window).trigger('resize'). Anything I might overlook with this approach? - thothalwidth = "100%"in theplotOutput()function first to let the CSS adjust the width. If that doesn't work, triggering a resize may be the way to go. - Ryan Mortonwidth=100%but did not work, so I will stick with thetrigger. - thothal