R从受密码保护的URL中导入CSV文件-导入.BAT文件 [英] R Import - CSV file from password protected URL - in .BAT file
问题描述
好的-这就是我想要做的.
Okay - so here is what I'm trying to do.
我已经尝试导入此受密码保护的CSV文件.
I've got this password protected CSV file I'm trying to import into R.
我可以使用以下命令将其导入:
I can import it fine using:
read.csv()
read.csv()
当我在RStudio中运行代码时,一切都可以完美运行.
and when I run my code in RStudio everything works perfect.
但是,当我尝试使用批处理文件(Windows .bat)运行.R文件时,它不起作用.我想使用.BAT文件,以便可以设置计划的任务来每天早上运行我的代码.
However, when I try and run my .R file using a batch file (windows .bat) it doesn't work. I want to use the .BAT file so that I can set up a scheduled task to run my code every morning.
这是我的.BAT文件:
Here is my .BAT file:
"E:\ R-3.0.2 \ bin \ x64 \ R.exe" CMD批处理"E:\ Control Files \ download_data.R""E:\ Control Files \ DailyEmail.txt"
"E:\R-3.0.2\bin\x64\R.exe" CMD BATCH "E:\Control Files\download_data.R" "E:\Control Files\DailyEmail.txt"
这是我的.R文件:
url<-" http://username:password@www.url.csv "
数据<-read.csv(URL,skip = 1)
data <- read.csv(url, skip=1)
**注意,我已经在代码中输入了用户名/密码和CSV的确切位置.我在这里使用了通用的东西,因为这是与工作相关的,发布用户名和密码可能不被接受.
** note, I've put my username/password and the exact location of the CSV in my code. I've used generic stuff here, as this is work related and posting usernames and passwords is probably frowned upon.
正如我所说的,当我在RStudio中使用该代码时,它可以正常工作.但是当我使用.BAT文件时失败.
As I've said, this code works fine when I use it in RStudio. But fails when I use the .BAT file.
我收到以下错误消息:
download.file(URL,"E:/data/data.csv")中的错误: 无法打开URL"网站URL " 另外:警告消息: 在download.file(url,"E:/data/data.csv")中: 无法解析用户名" 执行停止
Error in download.file(url, "E:/data/data.csv") : cannot open URL 'websiteurl' In addition: Warning message: In download.file(url, "E:/data/data.csv") : unable to resolve 'username' Execution halted
网站网址上方的
**是上方的http(我无法发布链接) 显然,.BAT的用户名/密码有问题吗?有什么想法吗?
** above websiteurl is the http above (I can't post links) So obviously, the .BAT is having trouble with the username/password? Any thoughts?
*编辑*
到目前为止,我已经在Linux上尝试过了.以为窗户可能在耍傻瓜.
I've gone so far as trying this on Linux. Thinking maybe windows was playing silly bugger.
仅在终端上,我运行Rscript -e"download_data.r"并获得与Windows完全相同的错误消息.因此,我怀疑这可能是在哪里获取数据的问题?提供程序可以从命令行阻止数据,而不是从Rstudio阻止数据吗?
Just from the terminal, I run Rscript -e "download_data.r" and get the EXACT same error message as I did in Windows. So I suspect this may be a problem with where I'm getting the data? Could the provider be blocking data from the command line, but not from with Rstudio?
推荐答案
因此,我找到了一个解决方案,该解决方案对大多数人来说可能不是最实用的,但对我有用.
So, I've found a solution, which is likely not the most practical for most people, but works for me.
我所做的就是将我的项目迁移到Linux系统上.无论如何,在Linux上运行日常脚本更加容易.
What I did was migrated my project over to a Linux system. Running daily scripts, is easier on Linux anyways.
该解决方案利用了Linux中的"wget"功能.
The solution makes use of the "wget" function in linux.
您可以直接在Shell脚本中运行wget,也可以使用R中的system()函数来运行wget.
You can either run the wget right in your shell script, or make use of the system() function in R to run the wget.
代码如下:
wget -O/home/user/.../file.csv --user = userid --password ='密码' http://www.url.com/file.csv
您可以执行以下操作: