R Shiny: How to assign the same plot to two different plotOutput

五迷三道 提交于 2020-01-16 00:38:14

问题


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?


回答1:


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())})
  }


来源:https://stackoverflow.com/questions/25423448/r-shiny-how-to-assign-the-same-plot-to-two-different-plotoutput

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!