[英]“Dynamic” Choice on SelectInput in Shiny
selectInput(
inputId = "histvariable",
label = "Select a variable",
choices = list("Age", "SibSp", "Parch", "Fare")
)
我想問一下我是否可以使choices
更加“動態”? 我的意思是, choices
可以從.csv文件中選擇所有變量。 上面的示例代碼僅從.csv文件中選擇四個變量( Age
, SibSp
, Parch
, Fare
)。
這是一個基於上載的csv文件的別名的SelectInput
的代碼段。
library(shiny)
ui = fluidPage(
tabsetPanel(
tabPanel("“Dynamic” Choice on SelectInput in Shiny
",
sidebarLayout(
sidebarPanel(
fileInput('file1', 'Upload your CSV File'),
htmlOutput("varselectOne")),
mainPanel(
verbatimTextOutput("rendertext")
)))))
server <- function(input, output, session) {
myData1 <- reactive({
inFile <- input$file1
if (is.null(inFile)) return(NULL)
data <- read.csv(inFile$datapath, header = TRUE, sep=",")
data
})
#observe(print(myData1()))
output$varselectOne <- renderUI({
#browser()
req(myData1())
# Variable selection:
selectInput(inputId = "varsOne", label = "Variables to use:", choices=names(myData1()), selected=names(myData1()), multiple =FALSE)
})
output$rendertext <- renderPrint ({
req(input$varsOne) #To ensure values are available before proceeding with a calculation or action,
#and prevent ?renderPrint from through an error before file uploaded
input$varsOne
})
}
shinyApp(ui = ui, server = server)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.