【问题标题】:How to render multiple output from the same analysis without executing it multiple time? (Shiny)如何在不多次执行的情况下从同一个分析中渲染多个输出? (闪亮的)
【发布时间】:2016-05-06 15:12:22
【问题描述】:

我正在编写一个闪亮的应用程序,其中包含一个随机函数生成四个对象 - 一个绘图和三个表格。但是,我想在不执行该函数四次的情况下在不同的选项卡中呈现每个对象,因为这个随机函数将生成四个不同的版本。我在网上进行了研究,发现很多人推荐“reactive()”,但我仍然不太明白如何将其应用于我的问题。我如何在渲染中使用这四个对象,只在函数上执行一次?

我的“server.R”结构基本上如下所示:

shinyServer(function(input, output) {

     stochastic_function() {
          ...
       plot1 <- ...
       table1 <- ...
       table2 <- ...
       table3 <- ...
       result <- list(plot, table1, table2, table3)
     return(result)
     }

 output$plot <- renderPlot({

})

 output$table1 <- renderTable({

})

 output$table2 <- renderTable({

})

 output$table3 <- renderTable({

})

...

所以,我尝试了以下随机函数:

model <- eventReactive(input$goButton, {
        reactive(WG_Model(cdata = cdata(), # load data from outside env
                          sdata = sdata(), # load data from outside env
                          N = input$n, 
                          end_date = input$end_date,
                          cpx_goal = input$cpx,
                          N_new = input$n2, 
                          end_date_new = input$end_date2,
                          spend_range = input$s_range,
                          spend_incr = input$s_incr
                          ) 
                )
})

这个想法是添加一个“GoButton”来启动函数,然后将所有输出保存在一个响应式 fun() 中。所以我可以渲染每个输出:

 output$plot <- renderPlot({
   model$gplot
})

 output$table <- renderTable({
   model$table
})

# Render UI section 
 output$tb <- renderUI({
  tabsetPanel(tabPanel("About Model", plotOutput("plot")), 
              tabPanel("About Model", tableOutput("table")))

  })

但是,我在 UI 输出中只收到“错误:'closure' 类型的对象不是子集”。我错过了哪一部分?

【问题讨论】:

  • 嘿@warmoverflow :) 我还没有尝试过任何东西,但我正在考虑使用“model
  • @warmoverflow 我已经对其进行了测试并收到了错误消息。我用结果更新了帖子。如果您有任何想法,请告诉我。谢谢!:)
  • reactiveEvent 内,您不再需要reactivemodel 应该返回一个可以使用model() 访问的值。您忘记在 render* 函数中添加括号。 (应该是model()$table
  • @UnnamedUser 是正确的,你需要使用model(),但这也不会得到你想要的,因为我认为每次你调用model() 它都会再次运行代码。跨度>
  • @warmoverflow 我明白了。我遵循“@UnnamedUser”提供的解决方案。尽管该功能已成功启动并完成,但我没有在 UI 上看到任何绘图或表格。 ;(

标签: r shiny


【解决方案1】:

如果您的model() 是一个列表并且包含所有表格和绘图的数据,它应该像我的示例一样工作。

在这个应用程序中,按下按钮后,会生成一个随机数和一个表格和一个绘图的数据。然后将数字、表格数据和绘图作为列表返回,并使用适当的render* 函数进行渲染。

此应用说明model 函数在其他反应函数中使用model() 访问后不会重新运行。

然而,有一个奇怪的事情......情节并不总是被渲染。有时您必须多次单击该按钮才能获得绘图。桌子一直在工作。


library(shiny)

ui <- shinyUI(fluidPage(
   br(),
   actionButton("numb", "generate a random numbers"),
   br(),
   br(),
   verbatimTextOutput("text"),
   plotOutput("plot"),
   tableOutput("table")
))

server <- shinyServer(function(input, output) {

  model <- eventReactive(input$numb, {
    # draw a random number and print it
    random <- sample(1:100, 1)
    print(paste0("The number is: ", random))

    # generate data for a table and plot
    data <- rnorm(10, mean = 100)
    table <- matrix(data, ncol = 2)

    # create a plot 
    Plot <- plot(1:length(data), data, pch = 16, xlab ="", ylab = "")

    # return all object as a list
    list(random = random, Plot = Plot, table = table)
  })

   output$text <- renderText({
     # print the random number after accessing "model" with brackets.
     # It doesn't re-run the function.
     youget <- paste0("After using model()$random you get: ", model()$random,
                      ". Compare it with a value in the console")
     print(youget)
     youget
   })

   output$plot <- renderPlot({
     # render saved plot
     model()$Plot
   })

   output$table <- renderTable({

     model()$table
   })
})


shinyApp(ui = ui, server = server)

【讨论】:

  • 不,这只是一个小错字。;P 谢谢!哦,顺便提一个问题,以防万一您知道答案 - 在运行 model() 函数时,它会在控制台中使用“print("xxx")”打印大量日志信息。有没有办法可以将这些“Print()”日志信息呈现到 UI?
  • 是的,这可能是一种方法,但我想在 model() 运行时向用户显示日志文本。由于它是一个优化搜索功能,我希望用户查看 model() 运行时发生了什么。有可能吗?
  • 不可能(或者我不知道)任何可以在反应式进程运行时向 UI 输出消息的方法(输出将在反应式进程结束后出现)。根本原因是Shiny和R都是单线程的,不能同时做两个任务。
  • @UnnamedUser 谢谢分享!这很有帮助!为什么有时我点击“Go!”后什么也没发生按钮,过了一会儿它开始运行?
  • @warmoverflow Gotcha - 有道理。感谢您的澄清!:)
猜你喜欢
  • 2015-03-28
  • 1970-01-01
  • 2021-04-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-17
相关资源
最近更新 更多