我有一个带条件面板的应用程序,它依赖于一系列用户选择.提供一个可重复的示例太复杂了,但我会尝试提供一个简化版本来获取我要求的内容,并且还包括实际应用程序中的相关部分.
在我的应用程序中,如果用户加载Excel文件然后从单选按钮中选择另一个条件,则会出现某个条件面板.如果加载了Excel文件并进行了适当的选择,则给定的条件面板将按预期显示.如果加载了csv文件,则不会显示条件面板,但是应该有一个红色消息object sheetrID not found.错误(实际上是Shiny中的一个警告)纯粹是装饰性的,但我想了解为什么会发生这种情况.
作为一种方法,在这个简单的应用程序中,在条件面板上获取类似错误消息的方法是:
library(shiny)
ui <- fluidPage(
column(3,
h3("Add new data"),
uiOutput("dummyslider")
), # close column
column(9,
h3("Matching"),
uiOutput("condition_select")
) # close column
) # close fluidPage
server <- function(input, output, session) {
output$dummyslider <- renderUI({
sliderInput(inputId = "num",
label = "Choose a number",
value = 25, min = 1, max = 100)
})
output$condition_select <- shiny::renderUI({
shiny::conditionalPanel(
condition="input.num > 40",
shiny::sliderInput(
inputId="slider2",
label="test label",
min=0,
max=1,
value=0.1,
step=0.01
)
)
})
} # close server
shinyApp(ui = ui, server = server)
Run Code Online (Sandbox Code Playgroud)
编辑:响应@Pork Chop推荐 - 我不清楚我将在何处插入该req声明.条件面板依赖输入$ method_cat为'both'以便显示面板.我插入了print语句,用于验证输入$ method_cat不是表,还验证getExtension是'csv':
output$condition_cat_both <- shiny::renderUI({
print(input$method_cat)
print(getExtension())
shiny::conditionalPanel(
condition="input.method_cat == 'both'",
shinyWidgets::pickerInput(
inputId="cat_vars_selected_both",
label="Select variables you wish to be categorical:",
choices=smartSelectionCatVars(importDataSet(), importCatVarLookup()),
multiple=TRUE,
options = list(`actions-box` = TRUE)
), # close pickerInput
) # close conditionalPanel
Run Code Online (Sandbox Code Playgroud)
method_cat是单选按钮的输出:
output$select_method_cat <- shiny::renderUI({
shinyWidgets::awesomeRadio(
inputId="method_cat",
label="Select the method to treat categorical variables:",
choices=getMethodCatOptions(),
selected="thresh"
)
}) # close renderUI
Run Code Online (Sandbox Code Playgroud)
只有当加载的文件是Excel时,getMethodCatOptions才会将'both'作为单选按钮选项之一返回:
getMethodCatOptions <- shiny::reactive({
req(input$variable_relations)
req(input$cat_var_lookup)
if(getExtension() == "xlsx" && input$variable_relations == "Yes" &&
input$cat_var_lookup == "Yes") {
return(c("Threshold"="thresh", "Select Variables Specifically"="pick",
"Use categorical variable lookup"="sheet",
"Select both"="both"))
}
return(c("Threshold"="thresh", "Select Variables Specifically"="pick"))
}) # close reactive
Run Code Online (Sandbox Code Playgroud)
因此,我不清楚在哪里插入req语句,因为根据定义,条件面板应该仅在输入文件是Excel时出现(因为这是唯一触发'sheet'作为无线电选项之一输出的东西按钮).
更新:我意识到这个声称的错误消息实际上是Shiny中的警告:`警告:匹配错误:对象'sheetrId'未找到
我可以压制警告,从而消除这个问题,但我宁愿理解为什么会这样.可能是因为条件面板在renderUI函数内吗?
然后,我的玩具示例具有相同的情况,并且该条件面板没有问题......
为了演示我上面的评论,这里是示例应用程序的改编版本,它会产生相同的错误消息。但是,正如我提到的,该错误与您的应用程序无关,而是由于使用openxlsx::loadWorkbook().
在此示例中,一旦您将滑块移动到 40 以上并成功上传文件,条件面板就会打开。如果您尝试openxlsx::loadWorkbook()访问 .csv,则会收到您所描述的错误(未成功上传),并且面板将无法打开。您可以切换到read.csv()以避免错误并打开面板。
validate()在您的情况下,如果您不希望在用户尝试上传 .csv 文件时打开面板,那么包含一个告诉用户上传 Excel 文件而不是 .csv 文件的函数可能更具指导意义。
更新的用户界面:
library(shiny)
ui <- fluidPage(
column(3,
h3("Add new data"),
uiOutput("dummyslider")
), # close column
column(3,
h3("Matching"),
uiOutput("condition_select")
), # close column
column(3,
h3("Table/error"),
tableOutput("tbl")
)
) # close fluidPage
Run Code Online (Sandbox Code Playgroud)
更新的服务器:
server <- function(input, output, session) {
output$dummyslider <- renderUI({
tagList(
sliderInput(inputId = "num",
label = "Choose a number",
value = 25, min = 1, max = 100),
fileInput("upl", label = "Upload any .csv file:")
)
})
output$condition_select <- shiny::renderUI({
shiny::conditionalPanel(
condition="input.num > 40 & output.uploaded",
shiny::sliderInput(
inputId="slider2",
label="test label",
min=0,
max=1,
value=0.1,
step=0.01
)
)
})
data <- reactive({
uplFile <- input$upl
if (is.null(uplFile))
return(NULL)
#tbl <- read.csv(uplFile$datapath)
tbl <- openxlsx::loadWorkbook(file=as.character(uplFile$datapath))
return(tbl)
})
output$uploaded <- reactive({ return( !is.null(data()) ) })
outputOptions(output, "uploaded", suspendWhenHidden=FALSE)
output$tbl <- renderTable ({
if (is.null(data())) return()
head(data.frame(read.xlsx(data())),10)
})
}
shinyApp(ui, server)
Run Code Online (Sandbox Code Playgroud)