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.

6
  • Is it possible for you to show us the first few lines of the file? Oct 14, 2015 at 20:40
  • @ Richard: original file uploaded to Drive has column names var1, var2,var3,...ect. Is that sufficient?
    – remi
    Oct 14, 2015 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 2s are fields values; but there are only two column headers defined. Ergo: error..
    – lukeA
    Oct 14, 2015 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, 2015 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, 2015 at 21:46

1 Answer 1

19

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))
3
  • @ lukeA: thanks again. If any idea why curl doesn't work, please share.
    – remi
    Oct 15, 2015 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, 2015 at 10:25
  • @ lukeA: thanks again again :-). This solved some problems.
    – remi
    Oct 19, 2015 at 19:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.