R闪亮的csv或excel上传选项

时间:2016-10-18 18:48:31

标签: r excel csv xlsx shinydashboard

我要求用户选择以.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在没有单选按钮的情况下工作,如果有条件。我无法调试,因为调试器一次运行代码块。

有人可以帮忙吗?

感谢,

Manoj Agrawal

1 个答案:

答案 0 :(得分:2)

Aargh ......在if条件中我只是缺少“”,所以它应该是

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