使在一个反应​​性对象内创建的对象对另一个对象可用 [英] Make object created inside one reactive object available to another in shiny

查看:109
本文介绍了使在一个反应​​性对象内创建的对象对另一个对象可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个闪亮的应用程序,其中我基于滑块定义对象并从中创建data.frame.这将用于创建绘图,在该绘图下方我想包含一个汇总表.我的问题是必须在反应对象内部定义图以基于滑块进行更新,但是我发现当我尝试访问对象以在不同反应对象内部打印摘要时,找不到.

I have a shiny app in which I define an object based on sliders and create a data.frame from it. This gets used to create a plot, below which I'd like to include a summary table. My issue is that the plot must be defined inside of a reactive object to update based on the sliders, but I found that when I tried to access the object to print a summary inside of a different reactive object, it wasn't found.

我不想让相同的计算代码运行两次,只是为了使该对象进入另一个反应对象.我的两个初步想法:

I'd like to not run through the same calculation code twice just to get the object into the other reactive object. My two initial ideas:

  • 我尝试在第一个反应对象中使用save(object, file = "..."),然后在第二个反应对象中使用load(file = "..."),但是它没有更新.
  • 我曾想过在反应式plotOutput定义之外定义对象,但是我敢肯定,当用户输入更改时,它不会更新.它只会保留初始input对象默认设置的值.
  • I tried using save(object, file = "...") inside the first reactive object and then putting load(file = "...") inside the second, but it doesn't update.
  • I thought of defining my object outside of my reactive plotOutput definition, but I'm pretty sure it won't update when user inputs are changed; it will just stay with the value of the initial input object default settings.

好吧,只是继续尝试了后一个想法,它甚至还没有走得那么远:

Well, just went ahead and tried the latter idea, and it doesn't even get that far:

  • 我将其放在shinyServer(...)之前,并且它无法生成数据,因为我的通话取决于input,并且此时不知道input.
  • 我把它放在shinyServer(...)之后但在renderPlot(...)之前,试图为所有输出对象创建某种全局可用"的对象,这让我为尝试做一些在反应式之外使用input的事情而感到责备功能.
  • I put it before shinyServer(...) and it can't generate the data because my call depends on input and it doesn't know about input at that point.
  • I put it after shinyServer(...) but before renderPlot(...) to try and make sort of a "globally available" object for all output objects, and it scolding me for trying to do something that uses input outside of a reactive function.

下面是一个示例,该示例是从Shiny的"Hello Shiny!"修改而来的.例子:

Here's an example to illustrate, modified from Shiny's "Hello Shiny!" example:

ui.R

library(shiny)

# Define UI for application that plots random distributions 
shinyUI(pageWithSidebar(

  # Application title
  headerPanel("Hello Shiny!"),

  # Sidebar with a slider input for number of observations
  sidebarPanel(
    sliderInput("obs", 
                "Number of observations:", 
                min = 1,
                max = 1000, 
                value = 500)
  ),

  # Show a plot of the generated distribution
  mainPanel(
    plotOutput("distPlot"),
    tableOutput("summary")
  )
))

server.R

library(shiny)

# Define server logic required to generate and plot a random distribution
shinyServer(function(input, output) {

  output$distPlot <- renderPlot({

    # generate an rnorm distribution and plot it
    dist <- rnorm(input$obs)
    hist(dist)
  })

  output$summary <- renderTable({
    #dist <- rnorm(input$obs)
    summary <- table(data.frame(cut(dist, 10)))
  })
})

如果按原样运行它,则cut将抛出错误,因为它不了解dist.如果您取消注释我们在plotOutput中重新定义dist的行,则它将起作用.

If you run it as is, cut will throw and error because it doesn't know about dist. If you uncomment the line where we re-define dist as is done in plotOutput, then it will work.

这是简单的代码-我的代码涉及更多,我真的不希望只运行两次以使另一个反应式输出知道相同的结果.

This is simple code -- mine is quite a bit more involved and I'd really prefer not to run it twice just to make the same result known to another reactive output.

建议?

推荐答案

仅基于 查看全文

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