Shiny R renderDataTable พร้อมตาราง null

Sep 08 2020

วิธีใดเป็นวิธีที่ดีที่สุดในการแสดงผลตารางข้อมูลแบบเงาถ้ามีข้อมูลอยู่เท่านั้น ตอนนี้ฉันได้รับข้อผิดพลาดต่อไปนี้เพราะฉันบอกให้มันแสดงตารางข้อมูลแม้ว่ามันจะเป็น 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()
})

ฉันได้ลองใส่เอาท์พุท $ filetable <- .... โค้ดหลังบรรทัด read.csv ... ในฟังก์ชัน filata <- ... ฉันควรทำอะไรที่นี่อีก

คำตอบ

1 Gourab Sep 08 2020 at 09:09

คุณสามารถใช้ฟังก์ชั่น req () ซึ่งจะตรวจสอบว่าไฟล์มีอยู่หรือไม่จากนั้นจะไปที่รหัสขั้นตอน

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

โปรดใช้req()ตามที่แสดงด้านล่าง

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