有了这段代码,我可以在我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 100 automatically at the start of my shiny app.

我认为这是可能的,还是我必须重新考虑我的策略?

推荐答案

server中,我们可以使用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)

R相关问答推荐

使用gggplot 2在R中正确表示翻转堆叠条形图中的数据

geom_raster不适用于x比例中超过2,15的值

在ComplexHeatmap中,如何更改anno_barplot()标题的Angular ?

使用R中的gt对R中的html rmarkdown文件进行条件格式设置表的单元格

使用R中的Shapetime裁剪格栅文件

ggplot2中的X轴显示数值,单位为百,而不是十

为什么在ggplot2中添加geom_text这么慢?

如何得到R中唯一的组合群?

当我们有多个特殊字符时,使用gsub删除名称和代码'

如何将SAS数据集的列名和列标签同时包含在r中GT表的表首?

跨列查找多个时间报告

为左表中的所有行使用值Fill滚动左连接

基于Key->Value数据帧的基因子集相关性提取

调换行/列并将第一行(原始数据帧的第一列)提升为标题的Tidyr类似功能?

从R中发出的咕噜声中的BUG?

如何在R中改变fviz_pca_biplot中圆的边界线的 colored颜色 ?

在R中,如何从一系列具有索引名的变量快速创建数据帧?

仅当后续值与特定值匹配时,才在列中回填Nas

在不对R中的变量分组的情况下取两行的平均值

删除在R中的write.table()函数期间创建的附加行