1
votes

is it somehow possible to have a reactive plot size in shiny?

Here's a small example, how I want it to work. It gives an error though as the reactive expression is not inside the output.

The ui.R, where you can choose the width and two plot outputs:

shinyUI(pageWithSidebar(
 headerPanel("Title"),
 sidebarPanel(
 selectInput("width", "Choose width:", 
             choices = c("200", "400"))
 ),

 mainPanel(
 plotOutput(outputId = "main_plot",  width = "100%"),
 plotOutput(outputId = "main_plot2", width = "100%")
 )
))

The server.R, where the second plot shall have the input width:

shinyServer(function(input, output) { 
 x <- 1:10
 y <- x^2
 width <- reactive({
 switch(input$direction,
       '200' = 200,
       '400' = 400)
 })
 output$main_plot <- renderPlot({    
 plot(x, y)}, height = 200, width = 400)
 output$main_plot2 <- renderPlot({
 plot(x, y) }, height = 200, width = width() )
})
1

1 Answers

6
votes

Your close, try this:

ui <- shinyUI(pageWithSidebar(
  headerPanel("Title"),
  sidebarPanel(
    selectInput("direction", "Choose width:", 
                choices = c("200", "400"))
  ),

  mainPanel(
    plotOutput(outputId = "main_plot",  width = "100%"),
    plotOutput(outputId = "main_plot2", width = "100%")
  )
))

server <- shinyServer(function(input, output) { 
  x <- 1:10
  y <- x^2

  output$main_plot <- renderPlot({    
    plot(x, y)}, height = 200, width = 400)

  observe({
    output$main_plot2 <- renderPlot({
      plot(x, y) }, height = 200, width = as.numeric(input$direction))
  })

})

shinyApp(ui=ui,server=server)