簡體   English   中英

Shiny中SelectInput的“動態”選擇

[英]“Dynamic” Choice on SelectInput in Shiny

selectInput(
  inputId = "histvariable", 
  label = "Select a variable",
  choices = list("Age", "SibSp", "Parch", "Fare")
)

我想問一下我是否可以使choices更加“動態”? 我的意思是, choices可以從.csv文件中選擇所有變量。 上面的示例代碼僅從.csv文件中選擇四個變量( AgeSibSpParchFare )。

這是一個基於上載的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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM