Change default value of width argument in shiny::renderPlot

Let's say I want to change the default value of width in shiny::renderPlot to 500. How can I do that?

Below is my try with defining customRenderPlot function but it doesn't react to input changes later on.

library(shiny)

customRenderPlot <- function(plot, width = 500) {
  renderPlot(plot, width = width)
}

ui <- fluidPage(
  numericInput(
    inputId = "bins",
    label = "Number of bins:",
    value = 2
  ),
  plotOutput(outputId = "distPlot")
)

server <- function(input, output) {
  output$distPlot <- customRenderPlot({
    x <- faithful$waiting
    bins <- seq(min(x), max(x), length.out = input$bins + 1)

    hist(x, breaks = bins)
  })
}

shinyApp(ui = ui, server = server)

Define it in the plotOutput().

https://shiny.rstudio.com/reference/shiny/0.11/plotOutput.html

Thanks. Can you explain why is it so?
What about changing default value for res since it's only available in renderPlot?

I don't know. Someone else might have the answer though. I think from memory, it sometimes wouldn't work as well with boxes, but not sure.

In general if you want to provide a wrapper of a render function, you will need to take extra step to make what you pass a reactive context.

library(shiny)

customRenderPlot <- function(plot, width = 500) {
  renderPlot(plot(), width = width)
}

ui <- fluidPage(
  numericInput(
    inputId = "bins",
    label = "Number of bins:",
    value = 2
  ),
  plotOutput(outputId = "distPlot")
)

server <- function(input, output) {
  output$distPlot <- customRenderPlot({
    reactive({
    x <- faithful$waiting
    bins <- seq(min(x), max(x), length.out = input$bins + 1)
    hist(x, breaks = bins)
  })})
}

shinyApp(ui = ui, server = server)
1 Like

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.