get()闪亮的已上传文件的名称

时间:2018-09-18 11:11:45

标签: r shiny reactive

关于下面的代码片段,我有一个简单的问题。

library(shiny)
ui <- fluidPage(

  fileInput("x", "upload file", accept = c(
    "text/csv",
    "text/comma-seperated-values, text/plain",
    ".csv")),

  tableOutput("my_csv")
)

server <- function(input, output) {


  csv <- reactive({
    inFile <- input$x
    if (is.null(inFile))
      return(NULL)
    df<- read.csv2(inFile$datapath, header=T)
    return(df)
  })

  output$my_csv <- renderTable({
    validate(need(!is.null(csv()),'no file yet.'))
      csv()
  })
}


shinyApp(ui, server)

我想要的是类似get()的函数来打印上载的csv文件的名称。 在下一步中,我想创建一个列表(名为“列表”),并将上载的文件作为文件名的第一个对象。 因此,如果上载的文件名为“ squirrel.csv”,并且我调用list $ squirrel.csv,我想查看该表。

1 个答案:

答案 0 :(得分:2)

您必须从basename的{​​{1}}字段中提取nameinput$x,因为您的x被称为inputId)。

将此添加到服务器部分:

x

在ui部分中,添加以下行以显示文件名:

output$my_csv_name <- renderText({
  # Test if file is selected
  if (!is.null(input$x$datapath)) {
      # Extract file name (additionally remove file extension using sub)
      return(sub(".csv$", "", basename(input$x$name)))
  } else {
      return(NULL)
  }
})
相关问题