Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jobdiogenes/235620928c84e604c6e56211ccf681f0 to your computer and use it in GitHub Desktop.
Save jobdiogenes/235620928c84e604c6e56211ccf681f0 to your computer and use it in GitHub Desktop.
Demo of use Google Drive/Sheets in R with Google Colaboratory.ipynb
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"kernelspec": {
"display_name": "R",
"language": "R",
"name": "ir"
},
"language_info": {
"codemirror_mode": "r",
"file_extension": ".r",
"mimetype": "text/x-r-source",
"name": "R",
"pygments_lexer": "r",
"version": "3.3.1"
},
"colab": {
"name": "Demo of use Google Drive/Sheets in R with Google Colaboratory.ipynb",
"private_outputs": true,
"provenance": [],
"collapsed_sections": [],
"include_colab_link": true
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/jobdiogenes/235620928c84e604c6e56211ccf681f0/demo-of-use-google-drive-sheets-in-r-with-google-colaboratory.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "h4bn_uPeLgJR"
},
"source": [
"Testing"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "HXnKjvsf9qnJ"
},
"source": [
"This is a small Demo on how to get access to Google Drive and Google Sheets, using R under Colab.\n",
"\n",
"It is one of methods that could be used to mitigate the issue with R libraries *googledrive* and *googlesheets4* while asking to access. \n",
"\n",
"We hope that in future, Google give us a similar experience like in Python. Where python package \"colab\" that give acces to Drive and Sheets, are available without need to load.\n",
"\n",
"The current issue come from httr package while call to is_interacive() which always return 'FALSE'.\n",
"\n",
"In this method, is_interactive() function are redefined in the way that allways return TRUE, if are running in Google Colab.\n",
"\n",
"# ATENTION: \n",
"This is a hack while using Colab Cloud Kernel. If you are using Local Kernel connect. Local kernel could work, I did not test yet to know."
]
},
{
"cell_type": "code",
"metadata": {
"id": "V2b5LRqVCoKc"
},
"source": [
"# Check if is running in Colab and redefine is_interactive()\n",
"if (file.exists(\"/usr/local/lib/python3.7/dist-packages/google/colab/_ipython.py\")) {\n",
" install.packages(\"R.utils\")\n",
" library(\"R.utils\")\n",
" library(\"httr\")\n",
" my_check <- function() {return(TRUE)}\n",
" reassignInPackage(\"is_interactive\", pkgName = \"httr\", my_check) \n",
" options(rlang_interactive=TRUE)\n",
"}"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "B5SO4XkqXA2n"
},
"source": [
"# If ts not available we need to install . put other packages if you need.\n",
"packages <- c(\"googledrive\", \"googlesheets4\")\n",
"if (length(setdiff(packages, rownames(installed.packages()))) > 0) {\n",
" install.packages(setdiff(packages, rownames(installed.packages()))) \n",
"} "
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "p1G5pFbUXWBS"
},
"source": [
"library(\"googledrive\")\n",
"library(\"googlesheets4\")"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "M5qbBfJn3fc4"
},
"source": [
"# call authentication forcing interactive login and save in cache. \n",
"drive_auth(use_oob = TRUE, cache = TRUE)"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "aEcze9V69K36"
},
"source": [
"# Find Google Sheets anf get the id of the first sheet \n",
"Sheets <- drive_find(type = \"spreadsheet\")\n",
"first_id <- Sheets$id[1]"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "nYYct6Xc9cUe"
},
"source": [
"# reuse token to Sheet Auth. \n",
"sheets_auth(token = drive_token())"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "feyBuPT49jzF"
},
"source": [
"# show data\n",
"sheets_read(first_id)"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "jWQlMTaGHNF4"
},
"source": [
""
],
"execution_count": null,
"outputs": []
}
]
}
@nacnudus
Copy link

This stopped working, and can be fixed by doing the same for rlang as for httr.

# Check if is running in Colab and redefine is_interactive()
if (file.exists("/usr/local/lib/python3.6/dist-packages/google/colab/_ipython.py")) {
  install.packages("R.utils")
  library(R.utils)
  library(httr)
  library(rlang)
  my_check <- function() {return(TRUE)}
  reassignInPackage("is_interactive", pkgName = "httr", my_check) 
  reassignInPackage("is_interactive", pkgName = "rlang", my_check) 
}

@jobdiogenes
Copy link
Author

@nacnudus Thanks for your reply.

I saw that stop working some days ago, then
I add options(rlang_interactive=TRUE), which solve too, but I forgot to push the correction to gist.
They have change httr code in some way that now need more hacks. :(

@ClaudiuPapasteri
Copy link

@jobdiogenes Have been using your solution for some time. Thank you for it. I hate to say it but it's broken again.

@jobdiogenes
Copy link
Author

@ClaudiuPapasteri, this stop working because Google update to python 3.7. I just update. And now are working again.
If you perhaps use the code only online in Colab, you could remove if (file.exists( .... ))

Thanks to report

@gsekn
Copy link

gsekn commented Mar 15, 2021

seems to work in online colab if you just change 3.6 to 3.7
if (file.exists("/usr/local/lib/python3.7/dist-packages/google/colab/_ipython.py")) {

but someone with greater skill should confirm.

@monitarb
Copy link

In my case neither /usr/local/lib/python3.6/dist-packages/google nor /usr/local/lib/python3.7/dist-packages/google folder existed.
However running the original code without if (file.exists(....)) worked for me!

@jobdiogenes
Copy link
Author

jobdiogenes commented Apr 13, 2021 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment