简体   繁体   English

r使用basename()获得文件名

[英]r shiny using basename() to get the file name

I am building a web app for users to upload file and store the data in PostgreSQL every month; 我正在构建一个Web应用程序,供用户每月上传文件并将数据存储在PostgreSQL中; I want to specify and extract the month from the file name uploaded; 我想从上传的文件名中指定并提取月份; something like "Utilization_summary_201511.csv". 类似于“ Utilization_summary_201511.csv”。

However, I am getting trouble getting the file name, I tried 2 ways below but both in vain. 但是,我在获取文件名时遇到了麻烦,我尝试了以下两种方法,但都徒劳。 In server.R 在server.R中

1. Use read.csv 1.使用read.csv

  filename<-renderText({
     inFile <- input$file1

      if (is.null(inFile))
        return(NULL)
     file<-read.csv(inFile$datapath, header=TRUE ,sep=",") 
     name<-basename(file)
     name
 })

2. Combine file.choose() and read.csv 2.合并file.choose()和read.csv

   filename<-renderText({

     inFile <- input$file1
     if (is.null(inFile))
       return(NULL)

     filename<-file.choose()
     data <- read.csv(filename, header=TRUE ,sep=",", skip=1)
     name<-basename(filename)
     name 
   })

and in ui.R: 在ui.R中:

 textOutput("filename")

It should be not so hard, and I've been trying to coming out solution for few days, thanks in advance for any ideas and suggestions. 它应该没有那么难,而且我已经尝试了几天的解决方案,在此先感谢您的任何想法和建议。

You might want to try inFile$name, rather than inFile$datapath. 您可能要尝试使用inFile $ name,而不是inFile $ datapath。 According to the shiny fileInput documentation : 根据闪亮的fileInput文档

name The filename provided by the web browser. 名称Web浏览器提供的文件名。

datapath The path to a temp file that contains the data that was uploaded. datapath到包含已上传数据的临时文件的路径。

Good luck! 祝好运!

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

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