R从受密码保护的URL中导入CSV文件-导入.BAT文件 [英] R Import - CSV file from password protected URL - in .BAT file

查看:96
本文介绍了R从受密码保护的URL中导入CSV文件-导入.BAT文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的-这就是我想要做的.

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

您可以执行以下操作:

syscomand>-"wget -O/home/.../file.csv --user = userid --password ='password'

系统(系统命令)

在R中将CSV下载到硬盘驱动器上的某个位置,然后使用read.csv()抓取CSV

in R to download the CSV to a location on your hard drive, then grab the CSV using read.csv()

以这种方式进行操作使我对问题的潜在根本原因有了更多的了解.当system(syscommand)运行时,我得到以下输出:

Doing it this way gave me some more insight into the potential root cause of the problem. While the system(syscommand) is running, I get the following output:

连接到www.website.com(www.website.com)| ip.ad.re.ss |:80 ...已连接.

Connecting to www.website.com (www.website.com)|ip.ad.re.ss|:80... connected.

HTTP请求已发送,正在等待响应... 401未经授权

HTTP request sent, awaiting response... 401 Unauthorized

重新使用与www.weburl.com:80的现有连接.

Reusing existing connection to www.weburl.com:80.

HTTP请求已发送,正在等待响应... 200 OK

HTTP request sent, awaiting response... 200 OK

不确定为什么必须两次发送请求?为何我第一次尝试401 Unauthorized?

Not sure why it has to send the request twice? And why I'm getting a 401 Unauthorized the first try?

这篇关于R从受密码保护的URL中导入CSV文件-导入.BAT文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆