1

This post [1]: Shiny's tabsetPanel not displaying plots in multiple tabs contains a perfect example of what I want to achieve. As I don't want to create the same plot too many times but just once the solution proposed does not work for me.

The user who answered the below question mentions that it is possible to assign the same plot to two different plotOutputs which is exactly what I want to do do.

Could someone please help me figure this out?

Community
  • 1
  • 1
Christos
  • 805
  • 8
  • 25
  • Welcome to Stack Overflow. This is not a good way to ask a question here. Did you try anything so far to solve your problem? Show your effort first so people might show theirs. Please read [FAQ](http://stackoverflow.com/tour), [How to Ask](http://stackoverflow.com/help/how-to-ask) and [help center](http://stackoverflow.com/help) as a start. – Nahuel Ianni Aug 21 '14 at 10:06
  • Thanks for welcoming me. I have indeed tried some ideas but I didn't think that it would be useful to post code here as it would be more confusing than helpful for other users to understand the nature of the problem. I have also spent many hours looking for something relevant in the web but unfortunatelly I still haven't come up with a solution. – Christos Aug 22 '14 at 07:38

1 Answers1

3

Each plot needs a unique id so you will need multiple calls to a render type function to assign that id. If the rendering of the graph is a bottleneck you could render it once to png for example and display the graphic.

library(shiny)
runApp(list(
  ui = bootstrapPage(
    numericInput('n', 'Number of obs', 100),
    plotOutput('plot1'),
    plotOutput('plot2')
  ),
  server = function(input, output) {
    myPlot <- reactive({function(){hist(runif(input$n))}})
    output$plot1 <- renderPlot({myPlot()()})
    output$plot2 <- renderPlot({myPlot()()})
  }
))

alternatively you can define the server function as:

  server = function(input, output) {
    myPlot <- reactive({hist(runif(input$n))})
    output$plot1 <- renderPlot({myPlot()})
    output$plot2 <- renderPlot({plot(myPlot())})
  }
jdharrison
  • 30,085
  • 4
  • 77
  • 89