简体   繁体   English

csv 在 r shiny 应用程序开始时上传文件(准占位符文件)

[英]csv file upload at start of r shiny app (quasi placeholder file)

With this code I can upload a csv file in my shiny app.使用此代码,我可以在我的 shiny 应用程序中上传一个 csv 文件。

library(shiny)

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      fileInput("file1", "Choose CSV File", accept = ".csv"),
      checkboxInput("header", "Header", TRUE)
    ),
    mainPanel(
      tableOutput("contents")
    )
  )
)

server <- function(input, output) {
  output$contents <- renderTable({
    file <- input$file1
    ext <- tools::file_ext(file$datapath)
    
    req(file)
    validate(need(ext == "csv", "Please upload a csv file"))
    
    read.csv(file$datapath, header = input$header)
  })
}

shinyApp(ui, server)

I would like to load a placeholder csv file say df.csv automatically at the start of my shiny app.我想在我的 shiny 应用程序开始时自动加载一个占位符 csv 文件,比如df.csv

I this possible or do I have rethink my strategy?这是可能的还是我已经重新考虑了我的策略?

In the server , we may use if/else to create the placeholder .csv while loadingserver中,我们可以在加载时使用if/else创建占位符.csv

server <- function(input, output) {
  output$contents <- renderTable({
    
    if(is.null(input$file1)) {
      
       dat <- read.csv(file.path(getwd(), "df.csv"))
    } else {
    file <- input$file1
    ext <- tools::file_ext(file$datapath)
    
    req(file)
      validate(need(ext == "csv", "Please upload a csv file"))
    
    dat <- read.csv(file$datapath, header = input$header)
    }
    
    dat
    
    })
}

shinyApp(ui, server)

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

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