Open see24 opened 3 weeks ago
I think this is the behavior you're looking for (the default width
is 100%
, so the behavior you're seeing seems expected)?
shinyApp(
ui = page_sidebar(
sidebar = sidebar(
actionButton("newplot", "New plot")
),
plotOutput("plot", width = 400, fill = FALSE)
),
server = function(input, output) {
output$plot <- renderPlot({
input$newplot
# Add a little noise to the cars data
cars2 <- cars + rnorm(nrow(cars))
plot(cars2)
},
width = 400)
}
)
That works for this example but in my actual use case I need to set the width on the server side using a function.
Here is a better reprex
shinyApp(
ui = page_sidebar(
sidebar = sidebar(
actionButton("newplot", "New plot")
),
plotOutput("plot", fill = FALSE)
),
server = function(input, output) {
counter <- reactiveValues(n = 1)
observeEvent(input$newplot, {
counter$n <- counter$n + 1
})
output$plot <- renderPlot({
input$newplot
# Add a little noise to the cars data
par(mfrow = c(1,counter$n))
for(i in 1:counter$n){
cars2 <- cars + rnorm(nrow(cars))
plot(cars2)
}
},
width = function(){
400*counter$n
})
}
)
In that case, you could set width = "auto"
instead. But, I think what you really want is a different plotOutput("plot_i", width = 400)
for each plot, then something like layout_column_wrap()
to wrap the new plots onto new lines when needed.
No I want to adjust the width of the plot output based on the calculation I do on the server side and I want it to adjust appropriately to changes in window size like it does with basic shiny
.
In my real example I am using a faceted ggplot bar chart and the user can add any number of scenarios and re-run the model which adds another facet to the ggplot plot. Therefore, I want to control the width of the plot so the bars are not super wide when there are not very many scenarios and I need to have it all in one plot with coordinated legend etc.
Here is an example of what my actual plot looks like:
In that case, you'll want a renderUI()
that returns a plotOutput(width = 400*counter$n)
@cpsievert what you supplied is a useful workaround but I think that bslib is still not behaving as expected. Why doesn't it respect the width that is set it renderPlot
in the way that shiny does? Is this difference documented somewhere?
I switched from plain shiny to access some of the awesome theming features that bslib has but it would be helpful if these sorts of differences were minimized and or explained.
That's true, I'm not sure we'd change the behavior at this point, but we could do more to document difference
Describe the problem
If I create a plot with a width specified in
renderPlot
it works as expected when the page first renders. But if I resize the page the plot image is stretched to fill the whole space. If I generate a new plot it has the expected width again. In my actual use case this also happens after the first time an actionButton is pressed which is more problematic than if it only ever happened with window resizing.Before resize
After resize
Session Info