How to mount google drive to R notebook in colab?

前端 未结 3 1799
慢半拍i
慢半拍i 2021-01-04 23:11

I have an R notebook in colab where I want to read a file which is saved in my google drive.

I only seem to find python code such as \"from google.colab import driv

相关标签:
3条回答
  • 2021-01-04 23:46

    Start using python:

    from google.colab import drive
    drive.mount('/content/drive')
    

    Then load the R Magic:

    %load_ext rpy2.ipython
    

    and then activate the R Magic and load your data:

    %%R
    url = ('/content/drive/myDrive/folder1/myfile.csv')
    dataset = read.csv(url)
    
    0 讨论(0)
  • 2021-01-04 23:57

    It seems there is no mechanism as of now to mount google drive in colab notebook with R kernel. Although a workaround can be used to have google drive mounted normally as in pyhton kernel and use both python and r based on the needs. See this answer which explains how r and python can be run together.

    # activate R magic
    %load_ext rpy2.ipython
    
    %%R
    x <- 42
    print(x)
    
    0 讨论(0)
  • 2021-01-04 23:57

    To mount google drive in an R kernel:

    install.packages("googledrive")
    library("googledrive")
    
    if (file.exists("/usr/local/lib/python3.6/dist-packages/google/colab_ipython.py")){
      install.packages("R.utils")
      library("R.utils")
      library("httr")
      my_check <- function() {return(TRUE)}
      reassignInPackage("is_interactive", pkgName = "httr", my_check)
      options(rlang_interactive=TRUE)
    }                                                                                    
    

    And authenticate google drive

    drive_auth(use_oob = TRUE, cache = TRUE)
    
    0 讨论(0)
提交回复
热议问题