R get()中已上载文件的名称

R get()中已上载文件的名称,r,shiny,reactive,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) { cs

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

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)
库(闪亮)

ui您必须从
input$x
中的
name
字段中提取
basename
x
,因为您的
inputId
被称为
x

将此添加到服务器部件:

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)
  }
})

您必须从
input$x
中的
name
字段中提取
basename
x
,因为您的
inputId
被称为
x

将此添加到服务器部件:

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)
  }
})