简体   繁体   English

如何从 R 中的 url 读取 csv 文件?

[英]How to read a csv-file from an url in R?

I'm rather new to R and I could use your assistance.我对 R 比较陌生,我可以使用您的帮助。

I am currently trying to import a csv file from an URL.我目前正在尝试从 URL 导入 csv 文件。 I have found many examples.我发现了很多例子。 Unfortunately, none of them work for my problem.不幸的是,它们都不能解决我的问题。

Specifically, I wanted to import the file from this url: https://www.bfs.admin.ch/bfsstatic/dam/assets/15324797/master具体来说,我想从这个 url 导入文件: https://www.bfs.admin.ch/bfsstatic/dam/assets/15324797/master

I tried these few lines of code:我尝试了这几行代码:

temp <- tempfile()

download.file("https://www.bfs.admin.ch/bfsstatic/dam/assets/15324797/master",temp, mode =  "w")

data<- read_csv2("je-b-03.03.02.05.csv")

and also this:还有这个:

data <- read_csv2(url("https://www.bfs.admin.ch/bfsstatic/dam/assets/15324797/master"))

But that didn't work either.但这也没有用。 On the other hand, when I paste the url into my browser, the file is downloaded automatically.另一方面,当我将 url 粘贴到浏览器中时,文件会自动下载。

I would appreciate any help.我将不胜感激任何帮助。 Thanks谢谢

df <- read.csv2(file = url("https://www.bfs.admin.ch/bfsstatic/dam/assets/15324797/master"))

Maybe this helps to begin with:也许这有助于开始:

data <- read.csv(url("https://www.bfs.admin.ch/bfsstatic/dam/assets/15324797/master"))

Check ?read.csv for details.查看?read.csv了解详情。

I've found vroom to be the best option for this type of task:我发现vroom是此类任务的最佳选择:

install.packages("vroom")
library(vroom)
data <- vroom("https://www.bfs.admin.ch/bfsstatic/dam/assets/15324797/master")
loc.url <- "https://www.bfs.admin.ch/bfsstatic/dam/assets/15324797/master"

data <- read.csv2(loc.url)

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

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