selectInput(
inputId = "histvariable",
label = "Select a variable",
choices = list("Age", "SibSp", "Parch", "Fare")
)
我想问一下我是否可以使choices
更具“动态性”?我的意思是choices
可以从.csv文件中选择所有变量。上面的示例代码仅从.csv文件中选择四个变量(Age
,SibSp
,Parch
,Fare
)。
答案 0 :(得分:0)
这是一个SelectInput
的代码段,基于上载的csv文件的名称。
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)