R shiny csv 或 excel 上传选项

R shiny csv or excel upload option

我需要让用户可以选择上传 .csv/.txt 或 .xlsx 格式的文件。

我正在使用 xlsx 包并在 UI 上提供了一个单选按钮,例如

ui <- dashboardPage(
  dashboardHeader(title = "SKU Health Check App"),
  dashboardSidebar(
    width = 350,
    radioButtons(
      "fileType_Input",
      label = h4("Choose File type"),
      choices = list(".csv/txt" = 1, ".xlsx" = 2),
      selected = 1,
      inline = TRUE
    ),
    fileInput(
      'file1',
      h4('Upload Items List'),
      accept = c(
        'text/csv',
        'text/comma-separated-values,text/plain',
        '.csv',
        '.xlsx'
      )
    ),

并将服务器中的选项处理为

server <- function(input, output, session) {

  # Get the upload file
  get_item_list <- reactive({
    inFile <- input$file1

    if (is.null(inFile)) {
      return(NULL) }

    if (input$fileType_Input == 1) {
      read.csv(inFile$datapath,
               header = TRUE,
               stringsAsFactors = FALSE)
    } else {
      read.xlsx(inFile$datapath,
                header = TRUE,sheetIndex = 1,
                stringsAsFactors = FALSE)
    }
  })

但是我遇到了错误,因为即使使用选项 1 也无法读取我的文件,该选项在没有单选按钮和 if 条件的情况下工作较早。我无法调试,因为调试器会立即运行代码块。

有人可以帮忙吗?

谢谢,

马诺·阿格拉瓦尔

啊...我只是在 if 条件中漏掉了“”所以它应该是

if (input$fileType_Input == "1") {
      read.csv(inFile$datapath,