闪亮的检查reactiveValue存在与验证 - 未找到

时间:2016-02-21 20:33:26

标签: r shiny

我有一个闪亮的代码,如下所示。我需要将变量定义为可更新的reactiveValues(或者我可以定义它们我认为是全局的但是我必须从Rstudio中按下干净的对象,这不是非常用户友好的。)我尝试运行验证代码来检查是否存在我定义为reactiveValues的数据。验证(需要(存在(“GSEmRNA $ d”),message =“未找到数据帧”))产生“未找到数据帧”因此,不绘制我的箱图。如果我将它们定义为全局变量而忘记按下干净的对象,代码可能会混淆,因为旧数据可以像新的一样传递。任何帮助表示赞赏。

server.R

shinyServer(function(input, output) {
  observeEvent(input$GoButton,{
    dataset <- data.frame(first= c(1,5,9),second=c(8,5,13), third=c(10,3,17))
    GSEmRNA <- reactiveValues(d=dataset)
  })
  output$BoxplotDataset <- renderPlot({
    if (input$GoButton== 0) {return()}       
    else{
      validate(need(exists("GSEmRNA$d"),message="Dataframe not found"))
      boxplot(GSEmRNA$d)}
  })
})

ui.R

library(shiny)
shinyUI(pageWithSidebar(
headerPanel("Dataset Selection"),

sidebarPanel(

  actionButton("GoButton","GO")

),
mainPanel(
  wellPanel(
    column(8, plotOutput("BoxplotDataset")
  )
)
)))

为了记录,我也将这个问题发布到了SHINY GOOGLE GOOGLE讨论组https://groups.google.com/forum/#!topic/shiny-discuss/ZV5F6Yy-kFg

1 个答案:

答案 0 :(得分:3)

以下是更新后的代码。要点是:

library(shiny)  
server <-shinyServer(function(input, output) {
  GSEmRNA <- reactiveValues(d=NULL) #define it ouside

  observeEvent(input$GoButton,{
    dataset <- data.frame(first= c(1,5,9),second=c(8,5,13), third=c(10,3,17))
    GSEmRNA$d <- dataset  #assign it inside
    })

  output$BoxplotDataset <- renderPlot({
      validate(need(GSEmRNA$d,"Dataframe not found")) # changed as well
      boxplot(GSEmRNA$d)
  })
})

ui <- pageWithSidebar(
  headerPanel("Dataset Selection"),

  sidebarPanel(

    actionButton("GoButton","GO")

  ),
  mainPanel(
    wellPanel(
      column(8, plotOutput("BoxplotDataset")
      )
    )
  ))

runApp(list(ui=ui,server=server))
  1. 在observeEvent
  2. 之外定义reactiveValues
  3. 更改了observeEvent
  4. 内的reactiveValues
  5. 更改了validateneed
相关问题