简体   繁体   English

R/Shiny:使用来自上传文件的标题填充 SelectInput 选项

[英]R/Shiny: Populate SelectInput Options using Headers From Uploaded File

I'm trying to build a page using R Shiny that has:我正在尝试使用 R Shiny 构建一个页面,该页面具有:

  • A File Widget for uploading CSV files用于上传 CSV 文件的文件小部件

  • A SelectInput component一个 SelectInput 组件

I'd like to use these as follows:我想按如下方式使用这些:

  • Upon uploading a valid CSV file, populate the SelectInput whose options are the headers from the CSV file, the first header being the default selected上传有效的 CSV 文件后,填充 SelectInput,其选项是来自 CSV 文件的标题,第一个标题是默认选择的

I've tried various forms of observe() and observeEvent() so far, but have had no success in getting this even close.到目前为止,我已经尝试了各种形式的observe()observeEvent() ,但都没有成功实现这一点。 Any suggestions you may have would be great.您可能有任何建议都会很棒。

Here's an option -这是一个选项——

library(shiny)

#Sample data
#write.csv(mtcars, 'data.csv', row.names = FALSE)

ui <- fluidPage(
  fileInput('file', 'Upload csv file'),
  uiOutput('dropdown')
)

server <- function(input, output) {
  data <- reactive({
    req(input$file)
    read.csv(input$file$datapath)
  })
  
  output$dropdown <- renderUI({
    req(data())
    selectInput('cols', 'Select Column', names(data()))
  })
}


shinyApp(ui, server)

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM