Reputation: 113
I have problems with the function read.table. I want to read a table from an url, and keep it in R as a dataframe. The url is: https://datanalytics.com/uploads/datos_treemap.txt
I have wrote this code:
library(RCurl)
a <- getURL('https://datanalytics.com/uploads/datos_treemap.txt')
b = read.table(a, sep="\t ", header = TRUE, nrows=3)
download.file("https://datanalytics.com/uploads/datos_treemap.txt","/mnt/M/Ana/R/datos_treemap.txt",method = c("wget"))
But I can't keep the data as a dataframe, and it results in the following error:
Error in file(file, "rt") : cannot open the connection
In addition: Warning message:
In file(file, "rt") : cannot open file...
No such file or directory
I have also tried to download the document as a txt, and keep it in mi PC. But the generate txt results in a vector instead of a table (all the results are in one unic row). The code I have wrote is:
download.file("https://datanalytics.com/uploads/datos_treemap.txt","/mnt/M/Ana/R/datos_treemap.txt",method = c("wget"))
What am I doing wrong?
Upvotes: 4
Views: 4018
Reputation: 11965
library(RCurl)
a <- getURL('https://datanalytics.com/uploads/datos_treemap.txt')
b <- read.table(text=a, header = TRUE)
Upvotes: 2
Reputation: 2206
Here another solution using rvest
instead of RCurl
. I do not want to judge which package is "better", just wanted to show an additional option, although, in your simple case rvest
seems to be more verbose and you need SelectorGadget to identify the desired node (please anybody correct me if I am wrong and the code can be shortened).
library(rvest)
table <- read_html("https://datanalytics.com/uploads/datos_treemap.txt") %>%
html_text("p") %>%
{ read.table(text = ., header = T) }
Upvotes: 1