【问题标题】:Shiny R renderDataTable with null table带有空表的闪亮 R renderDataTable
【发布时间】:2020-12-26 09:03:51
【问题描述】:

如果数据存在,那么仅以闪亮的方式呈现数据表的最佳方法是什么?现在,我收到以下错误,因为我告诉 shiny 渲染数据表,即使它是 NULL。

    Warning in file(file, "rt") :
    cannot open file '\': No such file or directory
    Warning: Error in file: cannot open the connection

我的代码是这样拆分的,一旦用户选择了 csv 文件,我就会在其中读取数据。用户选择 csv 文件后,错误消失,一切正常。如何告诉 Shiny 在选择有效文件之前不要显示数据表?

filedata <- reactive({
  if (is.null(input$file_selector)){
    # User has not uploaded a file yet
    return(NULL)
  } else {
  read.csv(paste0(parseDirPath(c(home = 'C:\\Users\\Ruben'), file_dir()),'\\',input$file_selector),skip=1)}
})

output$filetable <- renderDataTable({
  filedata()
})

我尝试将 output$filetable

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    你可以使用 req() 函数,这将检查文件是否存在然后它会转到处理代码

    filedata <- reactive({
      file <-input$file_selector
      req(file)
      read.csv(paste0(parseDirPath(c(home = 'C:\\Users\\Ruben'), file_dir()),'\\',file),skip=1)}
    })
    

    【讨论】:

      【解决方案2】:

      请使用req(),如下图

      filedata <- reactive({
        req(input$file_selector)
        read.csv(paste0(parseDirPath(c(home = 'C:\\Users\\Ruben'), file_dir()),'\\',input$file_selector),skip=1)
      })
      

      【讨论】:

        猜你喜欢
        • 2019-05-03
        • 2014-07-06
        • 2018-07-30
        • 2016-01-02
        • 2014-08-30
        • 2018-05-02
        • 2020-09-18
        • 1970-01-01
        • 2021-11-21
        相关资源
        最近更新 更多