12

I am trying to access (read into R) a .csv file hosted on Google Drive (NOT a Drive spreadsheet) -- having set file permission to 'publicly shareable'.

So based on the shareable URL:

sURL <-"https://drive.google.com/file....view?pli=1"

I have been trying to read in using:

library(curl)
x <- curl(sURL)
data <- read.csv(x)

I'm getting this error message:

Error in read.table(file = file, header = header, sep = sep, quote = quote, : more columns than column names

Any idea what the complaint is about? Thanks guys.

UseR10085
  • 7,120
  • 3
  • 24
  • 54
remi
  • 781
  • 2
  • 13
  • 22
  • Is it possible for you to show us the first few lines of the file? – Rich Scriven Oct 14 '15 at 20:40
  • @ Richard: original file uploaded to Drive has column names `var1`, `var2`,`var3`,...ect. Is that sufficient? – remi Oct 14 '15 at 20:46
  • The complaint says, well, there are more column names than column fields. Like in `read.csv(text = "col1,col2\n1,2,2,2")`, where `1` is the row name for row #1 and the 3 `2`s are fields values; but there are only two column headers defined. Ergo: error.. – lukeA Oct 14 '15 at 20:59
  • @lukeA: the local .csv is sound (columns/value all in order). The error msg appears only when reading the same .csv doc from the web (Google Drive). – remi Oct 14 '15 at 21:11
  • @ lukeA: Thanks so much. This is what was needed: parse the `id` from the URL then paste it to the address with `..&export=download`. So `curl` is not needed. Guess will have to regex to automate the process. – remi Oct 14 '15 at 21:46
  • I posted it as an answer so that you can mark this question as closed, if it solved your problem. – lukeA Oct 15 '15 at 07:30

1 Answers1

20

You could try it like this

id <- "0B-wuZ2XMFIBUd09Ob0pKVkRzQTA" # google file ID
read.csv(sprintf("https://docs.google.com/uc?id=%s&export=download", id))
lukeA
  • 53,097
  • 5
  • 97
  • 100
  • @ lukeA: thanks again. If any idea why `curl` doesn't work, please share. – remi Oct 15 '15 at 10:14
  • 1
    `curl` works like this: `library(curl); id <- "0B-wuZ2XMFIBUd09Ob0pKVkRzQTA"; sURL <- sprintf("https://docs.google.com/uc?id=%s&export=download", id); con <- curl(sURL); read.csv(con)`. It returns a connection, not a text file. – lukeA Oct 15 '15 at 10:25
  • @ lukeA: thanks again again :-). This solved some problems. – remi Oct 19 '15 at 19:46