【问题标题】:How to mount google drive to R notebook in colab?如何在colab中将谷歌驱动器安装到R笔记本?
【发布时间】:2019-11-02 21:28:28
【问题描述】:

我在 colab 中有一个 R 笔记本,我想在其中读取保存在我的谷歌驱动器中的文件。

我似乎只找到诸如 “从 google.colab 导入驱动器 drive.mount('/content/drive')" 挂载驱动器。

但是,R 是否有代码可以执行此操作或其他替代方法?我真的很挣扎,非常感谢您的帮助!

【问题讨论】:

    标签: python jupyter-notebook jupyter google-colaboratory mount


    【解决方案1】:

    目前似乎没有任何机制可以在带有 R 内核的 colab 笔记本中安装谷歌驱动器。尽管可以使用一种解决方法来像在 pyhton 内核中一样正常安装 google 驱动器,并根据需要同时使用 python 和 r。请参阅this 答案,其中解释了如何一起运行 r 和 python。

    # activate R magic
    %load_ext rpy2.ipython
    
    %%R
    x <- 42
    print(x)
    

    【讨论】:

      【解决方案2】:

      开始使用python:

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

      然后加载 R Magic:

      %load_ext rpy2.ipython
      

      然后激活 R Magic 并加载您的数据:

      %%R
      url = ('/content/drive/myDrive/folder1/myfile.csv')
      dataset = read.csv(url)
      

      【讨论】:

        【解决方案3】:

        在 R 内核中挂载谷歌驱动器:

        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)
        }                                                                                    
        

        并验证谷歌驱动器

        drive_auth(use_oob = TRUE, cache = TRUE)
        

        【讨论】:

        • Google Colab 说找不到 R.utils
        • 当我从 colab.to/r 运行它并执行 drive_auth 时,我得到以下信息:错误:无法获取 Google 凭据。您是否在非交互式会话中运行 googledrive?考虑:* drive_deauth() 以防止尝试获取凭据。
        • 你快到了。选项/建议不在评论的范围内,但也许可以在这里查看github.com/tidyverse/googledrive/issues/276 或发布问题。
        • @Nosey 回答有效,但您必须通过更改 Python 的版本来检查第一个条件是否为 TRUE。例如,它在更改为 Python 3.7 后对我有用 :) file.exists("/usr/local/lib/python3.7/dist-packages/google/colab_ipython.py")
        • 为什么我们需要if (file.exists("/usr/local/lib/python3.6/dist-packages/google/colab_ipython.py")) 开始?我们不能在没有 if 的情况下运行那个内部块吗?
        猜你喜欢
        • 2023-03-31
        • 2018-10-14
        • 2018-07-31
        • 2021-06-13
        • 1970-01-01
        • 2020-11-28
        • 2021-01-08
        • 2021-03-25
        • 2018-07-19
        相关资源
        最近更新 更多