简体   繁体   English

带有空表的闪亮 R renderDataTable

[英]Shiny R renderDataTable with null table

What is the best way to only render a data table in shiny if data exists?如果数据存在,仅以闪亮的方式呈现数据表的最佳方法是什么? Right now, I am getting the following error because I am telling shiny to render a data table, even when it is NULL.现在,我收到以下错误,因为我告诉 Shiny 渲染数据表,即使它是 NULL。

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

My code is split like this, where I read the data once the user chooses a csv file.我的代码是这样拆分的,一旦用户选择了一个 csv 文件,我就会在其中读取数据。 After a user chooses a csv file, the error goes away and everything works fine.用户选择 csv 文件后,错误消失,一切正常。 How do I tell Shiny to not display a data table until a valid file is chosen?如何告诉 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()
})

I've tried putting the output$filetable <- .... code after the read.csv... line in the filedata <- ... function, but that doesn't work either.我试过将 output$filetable <- .... 代码放在 filedata <- ... 函数中 read.csv... 行之后,但这也不起作用。 What else should I be doing here?我还应该在这里做什么?

Please use req() , as shown below请使用req() ,如下图

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

You can use req() function, this will check whether the file the file is present or not then it will go to the proceding code您可以使用 req() 函数,这将检查该文件是否存在,然后将转到处理代码

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

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

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