R How to read a file from google drive using R

后端 未结 2 1412
渐次进展
渐次进展 2020-12-31 16:08

I would like to read in R a dataset from google drive as the screenshot indicated.

Neither

url <- \"https://drive.google.com/file/d/1AiZda_1-2nwr         


        
相关标签:
2条回答
  • 2020-12-31 16:08

    Try

    temp <- tempfile(fileext = ".zip")
    download.file("https://drive.google.com/uc?authuser=0&id=1AiZda_1-2nwrxI8fLD0Y6e5rTg7aocv0&export=download",
      temp)
    out <- unzip(temp, exdir = tempdir())
    bank <- read.csv(out[14], sep = ";")
    str(bank)
    # 'data.frame': 4119 obs. of  21 variables:
     # $ age           : int  30 39 25 38 47 32 32 41 31 35 ...
     # $ job           : Factor w/ 12 levels "admin.","blue-collar",..: 2 8 8 8 1 8 1 3 8 2 ...
     # $ marital       : Factor w/ 4 levels "divorced","married",..: 2 3 2 2 2 3 3 2 1 2 ...
     # <snip>
    

    The URL should correspond to the URL that you use to download the file using your browser.

    As @Mako212 points out, you can also make use of the googledrive package, substituting drive_download for download.file:

    library(googledrive)
    temp <- tempfile(fileext = ".zip")
    dl <- drive_download(
      as_id("1AiZda_1-2nwrxI8fLD0Y6e5rTg7aocv0"), path = temp, overwrite = TRUE)
    out <- unzip(temp, exdir = tempdir())
    bank <- read.csv(out[14], sep = ";")
    
    0 讨论(0)
  • 2020-12-31 16:29
    • The google drive share link is not the direct file link, so 1. download.file 2. RCurl first method in accepted answer only download the web page showing the file, not file itself. You can edit the downloaded file and see it's a html file.

    • You can find out the actual direct link to file with this. With the direct link all the regular download methods will work.

    • For very detailed discussions about getting the direct link or downloading it, see this question.

    • Google drive api require client to sign in, so googledrive package also ask you to sign in google if not already signed in.

    0 讨论(0)
提交回复
热议问题