簡體   English   中英

帶有空表的閃亮 R renderDataTable

[英]Shiny R renderDataTable with null table

如果數據存在,僅以閃亮的方式呈現數據表的最佳方法是什么? 現在,我收到以下錯誤,因為我告訴 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 <- .... 代碼放在 filedata <- ... 函數中 read.csv... 行之后,但這也不起作用。 我還應該在這里做什么?

請使用req() ,如下圖

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

您可以使用 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