我正在尝试根据来自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))