How is plot handled when rendering plots?

when you create a plot using plot() and then render it using renderPlot() I always assumed plot() returns a plot object and that renderPlot receives that object. However, I found out that plot() returns nothing. How is it implemented in practice? is the plot() rendering mechanism taken over by shiny?

How does this work for ggplot plots? do you have to return them in the renderPlot invocation?

Hi,

Yes plot() and ggplot() behave differently. If you do this: myPlot = ggplot(data, ...) it will save a ggplot object to that variable without actually rendering it. If you then run myPlot or if you just run ggplot(data, ...) without assigning it, it will actually render the output to the graphics device. The plot function however always renders to the graphics device, and can't be stored into a variable, even if you try.

The renderPlot() expects a rendered output, so this will produce results

output$myPlot = renderPlot({ggplot(data, ...)})

#OR
output$myPlot = renderPlot({plot(data)})

This one will not
output$myPlot = renderPlot({myPlot = ggplot(data, ...)})

Unless like this

myPlot = ggplot(data, ...)
output$myPlot = renderPlot({myPlot })

OR
output$myPlot = renderPlot({
  myPlot = ggplot(data, ...)
  myPlot
})

Hope this helps,
PJ

Thank you, but the semantics and behavior remain confusing. If renderPlot expects a rendered output (I assume as a return?) , plot() is definitely not returning it (as plot() does not return anything) and ggplot returns something that is not rendered.

basically it works by your plot() function being quoted and shiny makes a png of the plot and shows that to the user. Shiny is not relying on your 'plot object' because as you say no such thing exists, it relies on your code to produce the plot, and they use that code to output to png and display that.


look at the drawplot function here