我对下面的代码片段有一个简单的问题。
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)
Run Code Online (Sandbox Code Playgroud)
我想要的是像 get() 这样的函数来打印上传的 csv 文件的名称。在下一步中,我想创建一个列表(名为“list”),其中上传的文件作为其第一个对象和文件名。因此,如果上传的文件的名称是“squirrel.csv”并且我调用 list$squirrel.csv 我想查看该表。
PoG*_*bas 10
您必须从(因为您的被称为)中的字段basename中提取。nameinput$xxinputIdx
将其添加到服务器部分:
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)
}
})
Run Code Online (Sandbox Code Playgroud)
在 ui 部分添加以下行来显示文件名:
textOutput("my_csv_name")
Run Code Online (Sandbox Code Playgroud)