stats_noob
stats_noob

Reputation: 5897

R: "read" a txt file from a URL

I am interested to see if it is possible to directly load (into R) txt file from a url instead of downloading it and then importing it.

I came across this post over here: Access a URL and read Data with R

Apparently it is possible to do this with csv files. Is it possible to do the same thing for txt files?

I tried to adapt the code from this stackoverflow post for the following website: https://mantas.info/wp/wp-content/uploads/simple_esn/MackeyGlass_t17.txt

myfile <- read.txt(url("https://mantas.info/wp/wp-content/uploads/simple_esn/MackeyGlass_t17.txt"))

But it does not seem to be working.

Am I doing something wrong? Or is is not possible to directly load a txt file from a url into R?

Thanks

Upvotes: 2

Views: 8725

Answers (2)

akrun
akrun

Reputation: 887078

The issue is that read.txt doesn't exist.

?read.txt

No documentation for ‘read.txt’ in specified packages and libraries: you could try ‘??read.txt’

It should be read.table (no need of any packages)

myfile <- read.table(url("https://mantas.info/wp/wp-content/uploads/simple_esn/MackeyGlass_t17.txt"))

-output

dim(myfile)
#[1] 10000     1
head(myfile)
#           V1
#1  0.12206400
#2  0.04719682
#3 -0.01833035
#4 -0.08194958
#5 -0.13422158
#6 -0.18542675

Upvotes: 4

Duck
Duck

Reputation: 39595

Try with data.table function fread() (one of the fastest functions for loading data):

library(data.table)
#Load
myfile <- fread("https://mantas.info/wp/wp-content/uploads/simple_esn/MackeyGlass_t17.txt")

Output (some rows):

head(myfile)
            V1
1:  0.12206400
2:  0.04719682
3: -0.01833035
4: -0.08194958
5: -0.13422158
6: -0.18542675

You can wrap it as dataframe using as.data.frame() or:

#Load 2
myfile <- fread("https://mantas.info/wp/wp-content/uploads/simple_esn/MackeyGlass_t17.txt",data.table = F)

Upvotes: 2

Related Questions