Change plotOutput Size Reactively

2,538 views
Skip to first unread message

Dario Strbenac

unread,
Sep 15, 2015, 1:00:09 AM9/15/15
to Shiny - Web Framework for R
Can plot area dimensions be changed during the application running ? I have a use case where the user can choose a factor to create a facet_grid of plots with. Depending on the factor chosen, there are a variable number of rows of plots to show. If the plot dimensions remain the same, the plots are squashed when there is a factor of many levels.

Joe Cheng

unread,
Sep 16, 2015, 12:17:22 AM9/16/15
to Dario Strbenac, Shiny - Web Framework for R
Yes. Set the plotOutput height/width to "auto", then in server.R when you call renderPlot, you can pass functions for the width and height arguments. Those can use reactive inputs, values, and expressions.

output$plot <- renderPlot({
}, width = function() {
  input$cols * 300
}, height = function() {
  input$rows * 400
})

On Tue, Sep 15, 2015 at 6:00 AM Dario Strbenac <dario....@gmail.com> wrote:
Can plot area dimensions be changed during the application running ? I have a use case where the user can choose a factor to create a facet_grid of plots with. Depending on the factor chosen, there are a variable number of rows of plots to show. If the plot dimensions remain the same, the plots are squashed when there is a factor of many levels.

--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/shiny-discuss/ad2883b3-961c-474b-a228-beae4bc8f933%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Dario Strbenac

unread,
Oct 3, 2015, 7:00:14 AM10/3/15
to Shiny - Web Framework for R, dario....@gmail.com
Your example works for me, but how do I use values computed within the reactive expression ? For example.

library(shiny)
shinyApp(
ui = fluidPage(
fluidRow(plotOutput("aGraph", "auto", "auto"))
),
server = function(input, output, session) {
output[["aGraph"]] <- renderPlot({
calcWidth <<- 500
plot(1:5)
}, width = function(){get("calcWidth", envir = .GlobalEnv)}, height = 300)
}
)

does not work.

Joe Cheng

unread,
Oct 3, 2015, 12:11:00 PM10/3/15
to Dario Strbenac, Shiny - Web Framework for R
Make calcWidth a separate reactive expression, that you use from both the renderPlot body and the width function.
--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.

Dario Strbenac

unread,
Oct 6, 2015, 4:00:23 AM10/6/15
to Shiny - Web Framework for R, dario....@gmail.com
I don't understand how to implement it. Can you show me a working example ? What does it mean to use the reactive expression in the renderPlot body ? I thought you meant something like :

calcWidth <- reactive({
    c(100 * length(input[["scoresTable_rows_selected"]]))
  })

output[["aPlot"]] <- renderPlot({
  input[["plotButton']]
   ...  # Don't know how/why to use it here.
}, width = function(){calcWidth})

Joe Cheng

unread,
Oct 6, 2015, 8:31:20 AM10/6/15
to Dario Strbenac, Shiny - Web Framework for R
I meant whatever "calcWidth <<- 500" was supposed to be a stand-in for, should go into a reactive expression. I was assuming that the reason you were calculating the width in the renderPlot was because whatever information is used to calculate that info, is needed in renderPlot too. If you give me a more realistic example, I can show you how to refactor it.

Also, in your example, `width = function(){calcWidth}` should be `width = function(){calcWidth()}` (I know it is easy to forget the parentheses...).

--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.

Dario Strbenac

unread,
Oct 6, 2015, 7:00:24 PM10/6/15
to Shiny - Web Framework for R, dario....@gmail.com
My minimal non-working example is :

library(shiny)
library(DT)
mtcars[, "cyl"] <- factor(mtcars[, "cyl"])

shinyApp(
  ui = fluidPage(
    fluidRow(plotOutput("aGraph", "auto", "auto")),
    dataTableOutput("carsTable")

  ),
  server = function(input, output, session) {
    output[["carsTable"]] <- renderDataTable(mtcars)
   
    output[["aGraph"]] <- renderPlot({
      rows <- input[["carsTable_rows_selected"]]
      carSubset <- mtcars[rows, ]
     
      calcWidth <- 100 * length(unique(carSubset[, "cyl"]))
      ggplot(data = carSubset, aes(x = cyl, y = mpg)) + geom_boxplot()
    }, width = calcWidth, height = 300)
  }
)
Reply all
Reply to author
Forward
0 new messages