【问题标题】:Shiny: ignoreNULL not working when the eventReactive function take output from another eventReactive function as input?闪亮:当 eventReactive 函数将另一个 eventReactive 函数的输出作为输入时,ignoreNULL 不起作用?
【发布时间】:2019-06-27 13:13:39
【问题描述】:

我正在开发一个 Shiny 应用程序,它首先生成数据,然后根据所选输入(要绘制的列、使用的颜色等)绘制数据。

我期望数据仅在用户单击操作按钮(操作按钮 1)后生成,并且将根据默认选择的输入与数据一起创建绘图。当用户想要更改输入时,他们将需要单击另一个操作按钮(操作按钮 2)来更新绘图。当用户单击操作按钮 2 时,不应重新生成数据。

有办法吗?

我已经为 eventReactive() 尝试了 ignoreNULL,但是当数据生成反应函数设置为 TRUE 时,将 ignoreNULL = FALSE 设置为绘图反应函数似乎不起作用

以下是我正在尝试做的一个简单示例:

library(shiny)

ui <- fluidPage(
  titlePanel("Test"),
  sidebarLayout(
    sidebarPanel(
      actionButton('data', 'Generate Random Data'),
      hr(),
      radioButtons("column", label = "Select Column: ", choices = c("x", "y")),
      actionButton('plot', 'Plot')
    ),
    mainPanel(
      plotOutput("result")
    )
  )
)

server <- function(input, output) {
  #### read in data
  dataReactive <- eventReactive(input$data, {
    dt <- data.frame("x" = rnorm(100), "y" = rnorm(100, mean = 100))
  }, ignoreNULL = FALSE)
  plotReactive <- eventReactive(input$plot, {
    dt <- dataReactive()
    p <- hist(dt[[input$column]])
  }, ignoreNULL = FALSE)
  output$result <- renderPlot({
    p <- plotReactive()
  })
}

# Run the application 
shinyApp(ui = ui, server = server) 

我希望应用程序从空白页面开始,当第一次单击生成随机数据按钮时,将显示直方图,而无需单击绘图按钮。但更改选择列时,必须单击绘图按钮以更新绘图。

原因是因为我正在处理的实际应用程序实际上需要很长时间才能从数据库中提取数据。所以我不希望应用程序在用户准备好之前自动启动。而且我不想每次用户想要更改绘图设置时都提取数据,因为所有绘图都使用相同的数据。

任何帮助将不胜感激。谢谢。

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    这是你所期待的吗?

    library(shiny)
    
    ui <- fluidPage(
      titlePanel("Test"),
      sidebarLayout(
        sidebarPanel(
          actionButton('data', 'Generate Random Data'),
          hr(),
          radioButtons("column", label = "Select Column: ", choices = c("x", "y")),
          actionButton('plot', 'Plot')
        ),
        mainPanel(
          plotOutput("result")
        )
      )
    )
    
    server <- function(input, output) {
      #### read in data
      dataReactive <- eventReactive(input$data, {
        print('generationg')
        dt <- data.frame("x" = rnorm(100), "y" = rnorm(100, mean = 100))
      })
      plotReactive <- eventReactive({
          input$plot
          input$data
        }, {
        dt <- dataReactive()
        p <- hist(dt[[input$column]])
      }, ignoreNULL = FALSE)
      output$result <- renderPlot({
        p <- plotReactive()
      })
    }
    
    # Run the application 
    shinyApp(ui = ui, server = server) 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-04-21
      • 1970-01-01
      • 2015-11-26
      • 2020-12-04
      • 2019-05-20
      • 1970-01-01
      • 2016-02-07
      相关资源
      最近更新 更多