【发布时间】:2018-02-27 04:46:38
【问题描述】:
根据第一个 selectInput 函数中选择的绘图类型,我在更新边栏中(可选)数据框的列名时遇到问题。 colnames 仅更新一次,这意味着 x 变量仅对第一个绘图类型可见,而 y 或区分变量仅适用于第二个绘图类型。第三种绘图类型不提供任何选择。
PS:从今天开始,我是 stackoverflow 的新手——我希望下面的示例能够提供足够的信息。提前致谢!
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)
【问题讨论】:
-
@MLavoie 感谢您的评论!我已经看过上面的链接,我的代码也包含观察函数。我通过分配 colnames 在您的链接中修改了此代码,因为我想根据用户可以自己读取的数据集动态更改 colnames。 “Contents”是剩余的代码内容,现在被“cb_options”代替了还是不行。
-
如果查看文档
updateSelectInput()未在反应参数中使用 -
对不起,我不明白。你能告诉我更多或举一个简短的例子吗?谢谢!
-
在
selectInput你没有提供任何选择。如果您添加选项,您的代码将起作用。不清楚为什么需要updateSelectInput。