R Shiny:如何将相同的图分配给两个不同的 plotOutput [英] R Shiny: How to assign the same plot to two different plotOutput

查看:54
本文介绍了R Shiny:如何将相同的图分配给两个不同的 plotOutput的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这篇文章 [1]:Shiny 的 tabsetPanel 不显示多个图tabs 包含我想要实现的完美示例.因为我不想创建相同的图太多次,但只有一次提出的解决方案对我不起作用.

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.

回答以下问题的用户提到可以将相同的图分配给两个不同的 plotOutputs,这正是我想要做的.

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?

推荐答案

每个图都需要一个唯一的 id,因此您需要多次调用 render 类型的函数来分配该 id.如果图形的渲染是一个瓶颈,您可以将其渲染为 png 并显示图形.

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

这篇关于R Shiny:如何将相同的图分配给两个不同的 plotOutput的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆