renderPlot priority (and/or nesting reactivity)

253 views
Skip to first unread message

Jonathan

unread,
Oct 13, 2014, 2:47:13 PM10/13/14
to shiny-...@googlegroups.com
Hi,

I have 3 plots that depend on the same data. For example, something like this:

get_data <- reactive({ <read .csv file> })
output$p1 <- renderPlot({plot(get_data()$x,get_data()$y1)})
output$p2 <- renderPlot({plot(get_data()$x,get_data()$y2)})
output$p3 <- renderPlot({plot(get_data()$x,get_data()$y3)})

I would like guarantee they update in a specified order. It seems like I could do this one of two ways that both require nesting reactivity. 1) using 'priority' option of multiple observe's or 2) nesting the renderPlots in one observe. But I don't know if/when/how nesting reactivity like this is ok, or if it behaves like I think. I haven't found a good article that talks about it. 

What happens when we put a 'renderPlot' inside an 'observe'? Is either of these kosher and/or is there a better way?

1) using 'priority':
get_data <- reactive({ <read .csv file> })
observe({output$p1 <- renderPlot({plot(get_data()$x,get_data()$y1)}),priority=3)}
observe({output$p2 <- renderPlot({plot(get_data()$x,get_data()$y2)}),priority=2)}
observe({output$p3 <- renderPlot({plot(get_data()$x,get_data()$y3)}),priority=1)}

2) nesting renderPlot in one observe:
get_data <- reactive({ <read .csv file> })
observe({
  output$p1 <- renderPlot({plot(get_data()$x,get_data()$y1)})
  output$p2 <- renderPlot({plot(get_data()$x,get_data()$y2)})
  output$p3 <- renderPlot({plot(get_data()$x,get_data()$y3)})
})

Jonathan

Joe Cheng

unread,
Oct 15, 2014, 4:11:12 AM10/15/14
to Jonathan, shiny-...@googlegroups.com
No, neither option will work. `output$p1 <- renderPlot(...)` doesn't mean "Update p1 right now with this renderPlot data", instead it means "p1 should always be filled with the up-to-date result of this expression". In other words, there's already an observer being created when you assign something to output$whatever, and creating an observer from inside an observer is a Bad Smell--its behavior is well-defined but almost always a sign that you're going about the problem the wrong way.

Instead, do this:

outputOptions(output, "p1", priority = 3)
outputOptions(output, "p2", priority = 2)
outputOptions(output, "p3", priority = 1)

This will ensure that the priority is set on the observers that underlie those outputs.

--
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/6acb6653-31c6-483b-95ed-3357b0e912b6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages