0

我正在尝试根据来自renderUI. 然后将这些数据以observe块的形式传递给进一步的工作。

下面是一个示例,其中服务器代码存在问题。我在识别if子句输入的代码方面遇到困难 - 或者更准确地说,它开始于NULL然后"Every row"input被识别。如果我carbData()observe下面取消注释,我会收到以下错误。如果我在observe环境中移动子集,也会收到类似的错误。

监听http://127.0.0.1:7400
NULL ### from check()
警告:if 错误:参数长度为零
61:reactive:carbData [#9]
45:carbData
44:[#17]
1:运行应用

这是由于使用的一些延迟评估renderUI吗?如果是这样,否则我该如何解决?

我的代码:

server <- function(input, output) {

    amData <- reactive({ mtcars[am %in% input$am, ] }) 
    output$gear <- renderUI({ selectInput("Gear", "GEAR", 
                                choices=unique(amData()[, "gear"]), selected=4 ) })
    
    gearData <- reactive({ amData()[gear %in% input$Gear, ] }) 
    output$carb <- renderUI({ selectInput("Carb", "CARB", 
                                choices=c("Every row", unique(gearData()[, "carb"])), selected="Every row") })
    
    carbData <-  reactive({ if(input$Carb %in% "Every row") gearData() else gearData()[ carb %in% input$Carb ] }) #### PROBLEM
    
    # Some text where it can be seen that input$Carb  is initially NULL
    check <-  reactive({ p <- input$Carb; print(p) }) 
    
    observe({ 
      check(); 
        
      # problems with if clause in both of these:
      # carbData() 
        
      # Moving the data subset inside `observe` doesn't help
      # df <- if(input$Carb %in% "Every row") gearData() else gearData()[ carb %in% input$Carb ]
      })
}

其余代码允许它运行:

library(shiny)
library(shinydashboard)

data(mtcars); data.table::setDT(mtcars)

ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(
   sidebarMenu(
       selectInput("am", "AM", unique(mtcars$am), selected = "0"),
       uiOutput("gear"),
       uiOutput("carb")
       )),
  dashboardBody())

runApp(shinyApp(ui, server))
4

1 回答 1

0

我有点不确定这个问题是否已经结束。但是,我冒昧地编辑了您的代码以找到问题所在。

我使用tibbles而不是data.table因为我不熟悉语法。到目前为止@stefan 是绝对正确的,req(input$Carb)它将为您解除烦恼。

gear %in% input$Gear有效和input$Carb %in% "Every row"无效的原因是reactive-hierachy 不支持input$Carb,并且null在启动时,您本质上是在测试NULL %in% "Every Row"gear应用程序启动时存在的构造。

正如我确定您所知道的那样,这与input$Carb %in% "NULL"NULL就此而言不同。

这是我玩过的你的代码,

server <- function(input, output) {
        amData <- reactive({
                mtcars %>% filter(am %in% input$am)
                
                
        })
        
        gearData <- reactive({
                amData() %>% filter(gear %in% input$Gear)
                
                
        })
        
        
        output$gear <- renderUI({
                selectInput("Gear",
                            "GEAR",
                            choices = unique(amData()[, "gear"]),
                            selected = 4)
        })
        
        
        output$carb <- renderUI({
                selectInput(
                        "Carb",
                        "CARB",
                        choices = c("Every row", unique(gearData()[, "carb"])),
                        selected = "Every row"
                )
        })
        
        carbData <-
                reactive({
                        
                        req(input$Carb)
                        
                        if (input$Carb %in% "Every row")
                                gearData()
                        else
                                gearData() %>% filter(carb %in% input$Carb)
                }) #### PROBLEM
        
        # Some text where it can be seen that input$Carb  is initially NULL
        check <-  reactive({
                p <- input$Carb
                print(p)
        })
        
        observe({
                check()
                
                
                # problems with if clause in both of these:
                carbData()
                
        })
}
于 2021-06-21T15:55:03.653 回答