R 闪亮:updateSelectInput不会针对不同的selectInput条件进行更新
根据第一个selectInput函数中选择的绘图类型,更新侧栏中(可选)数据框的列名时出现问题。 ColName只更新一次,这意味着x变量仅对第一种打印类型可见,y或Difference变量仅对第二种打印类型有效。第三种打印类型不提供任何选择 PS:从今天开始,我是stackoverflow新手-我希望下面的示例提供足够的信息。提前谢谢R 闪亮:updateSelectInput不会针对不同的selectInput条件进行更新,r,shiny,R,Shiny,根据第一个selectInput函数中选择的绘图类型,更新侧栏中(可选)数据框的列名时出现问题。 ColName只更新一次,这意味着x变量仅对第一种打印类型可见,y或Difference变量仅对第二种打印类型有效。第三种打印类型不提供任何选择 PS:从今天开始,我是stackoverflow新手-我希望下面的示例提供足够的信息。提前谢谢 ui <- fluidPage( sidebarLayout( sidebarPanel( selectInput(inputId = "plotty
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
selectInput(inputId = "plottype",
label = "Choose your favorite plot type",
choices = c("Histogram" = 1,
"Scatterplot" = 2,
"Whatever" = 3)),
conditionalPanel(
condition = "input.plottype == 1",
selectInput(inputId = "x_var",
label = "X-variable",
choices = ""),
uiOutput("choose_columns_1")),
conditionalPanel(
condition = "input.plottype == 2",
selectInput(inputId = "x_var",
label = "X-variable",
choices = ""),
selectInput(inputId = "y_var",
label = "Y-variable",
choices = ""),
selectInput(inputId = "fill",
label = "Distinguish",
choices = ""),
uiOutput("choose_columns_2")),
conditionalPanel(
condition = "input.plottype == 3",
selectInput(inputId = "x_var",
label = "X-variable",
choices = ""),
selectInput(inputId = "y_var",
label = "Y-variable",
choices = ""),
selectInput(inputId = "fill",
label = "Distinguish",
choices = ""),
uiOutput("choose_columns_3"))),
mainPanel()
)
)
server <- function(input, output, session) {
getData <- reactive({
if(is.null(input$file1)) return(mtcars)
req(input$file1)
req(input$sep)
req(input$quote)
read.csv(input$file1$datapath,
header = TRUE,
sep = input$sep,
quote = input$quote)
})
observe({
dsnames <- names(getData())
cb_options <- list()
cb_options[dsnames] <- dsnames
updateSelectInput(session, "x_var",
label = "X-variable",
choices = cb_options,
selected = "")
updateSelectInput(session, "y_var",
label = "Y-variable",
choices = cb_options,
selected = "")
updateSelectInput(session, "fill",
label = "Distinguish",
choices = cb_options,
selected = "")
})
output$choose_columns_1 <- renderUI({
if(is.null(input$dataset))
return()
colnames <- cb_options
updateSelectInput(session, "x_var",
label = "X-variable",
choices = colnames,
selected = "")
})
output$choose_columns_2 <- renderUI({
if(is.null(input$dataset))
return()
colnames2 <- cb_options
updateSelectInput(session, "x_var",
label = "X-variable",
choices = colnames2,
selected = "")
updateSelectInput(session, "y_var",
label = "Y-variable",
choices = colnames2,
selected = "")
updateSelectInput(session, "fill",
label = "Distinguish",
choices = colnames2,
selected = "")
})
output$choose_columns_3 <- renderUI({
if(is.null(input$dataset))
return()
colnames3 <- cb_options
updateSelectInput(session, "x_var",
label = "X-variable",
choices = colnames3,
selected = "")
updateSelectInput(session, "y_var",
label = "Y-variable",
choices = colnames3,
selected = "")
updateSelectInput(session, "fill",
label = "Distinguish",
choices = colnames3,
selected = "")
})
}
shinyApp(ui, server)
ui您可以尝试(使用三个数据集模拟您的需求):
data\u set您看到了吗:内容来自哪里?@MLavoie谢谢您的评论!我已经看到了上面的链接,我的代码也包含了observe函数。我通过指定colname修改了链接中的这段代码,因为我想根据用户可以自己读取的数据集动态更改colname。“内容”是一个遗留的代码内容,现在被“cb_选项”替换,但仍然不起作用。如果查看文档updateSelectInput()
未在被动参数中使用,对不起,我不理解。你能告诉我更多或者举个简单的例子吗?谢谢在选择input
中,您不提供任何选择。如果您添加选项,您的代码将正常工作。不清楚您为什么需要updateSelectInput
。非常感谢您提供的代码。我的问题是,对于不同的条件面板,它仍然不能使用相同的uiOutput。例如,如果您将uiOutput(“x_var”)
添加到ui中的最后一个条件面板(绘图类型2和3)。您需要在原始帖子中更加具体。代码按照您的要求执行。同一个uiOutput不能使用两次。啊!我不知道我不能使用相同的uiOutput两次!非常感谢你!帮了很多忙!
data_sets <- c("mtcars", "morley", "rock")
ui = fluidPage(
sidebarLayout(
sidebarPanel(
uiOutput("choose_dataset"),
selectInput(inputId = "plottype",
label = "Choose your favorite plot type",
choices = c("Histogram" = 1,
"Scatterplot" = 2,
"Whatever" = 3)),
conditionalPanel(
condition = "input.plottype == 1", uiOutput("x_var")),
conditionalPanel(
condition = "input.plottype == 2 || input.plottype == 3",
uiOutput("y_var"), uiOutput("fill"))
),
mainPanel()
)
)
server <- function(input, output, session) {
output$choose_dataset <- renderUI({
selectInput("dataset", "Data set", as.list(data_sets))
})
output$x_var <- renderUI({
# If missing input, return to avoid error later in function
if(is.null(input$dataset))
return()
# Get the data set with the appropriate name
dat <- get(input$dataset)
colnames <- names(dat)
# Create the checkboxes and select them all by default
selectInput(inputId = "x_var",
label = "X-variable",
choices = colnames,
selected = colnames)
})
output$y_var <- renderUI({
# If missing input, return to avoid error later in function
if(is.null(input$dataset))
return()
# Get the data set with the appropriate name
dat <- get(input$dataset)
colnames <- names(dat)
# Create the checkboxes and select them all by default
selectInput(inputId = "y_var",
label = "Y-variable",
choices = colnames,
selected = colnames)
})
output$fill <- renderUI({
# If missing input, return to avoid error later in function
if(is.null(input$dataset))
return()
# Get the data set with the appropriate name
dat <- get(input$dataset)
colnames <- names(dat)
# Create the checkboxes and select them all by default
selectInput(inputId = "fill",
label = "Distinguish",
choices = colnames,
selected = colnames)
})
}
shinyApp(ui = ui, server = server)