{"id":6280,"date":"2023-07-19T20:31:31","date_gmt":"2023-07-19T18:31:31","guid":{"rendered":"https:\/\/www.thediar.com\/?p=6280"},"modified":"2024-02-02T15:17:04","modified_gmt":"2024-02-02T14:17:04","slug":"google-colab-drive-r","status":"publish","type":"post","link":"https:\/\/www.thediar.com\/en\/blog\/google-colab-drive-r\/","title":{"rendered":"How to use .R files on Google Colab?"},"content":{"rendered":"<p>If you don&#8217;t know, on Google Colab you can have free notebooks for programming on the cloud.<\/p>\n<p>All you need is a desire to learn and, of course, a Google account.<\/p>\n<p>The main advantages are:<\/p>\n<ul>\n<li>You start programming without wasting time on configurations.<\/li>\n<li>You can use Google&#8217;s free (or paid) resources instead of your own computer.<\/li>\n<li>Sharing your notebooks is as easy as sharing a Google Drive document.<\/li>\n<\/ul>\n<h3>Is Google Colab only for Python programming?<\/h3>\n<p>No. You can program in Python and R on Google Colab.<\/p>\n<p>The truth is that the default runtime environment in Google Colab is Python 3 and although we can select R as the runtime environment for our notebook, I don&#8217;t recommend it. Read on \ud83d\ude42<\/p>\n<h3>How do I use R in Google Colab?<\/h3>\n<p>You can also program in R using the Python 3 runtime environment. This way in the same notebook you can run both, R or Python.<\/p>\n<p>The first step is to install the rpy2 Python package with a properly working version. Yes, there are recent versions where it fails and using version 3.5.1 solves the problems.<\/p>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-6281\" src=\"https:\/\/i0.wp.com\/www.thediar.com\/wp-content\/uploads\/Analizando-la-tasa-paro-con-paquetes-de-tidyverse-en-Google-Colab-Colaboratory.png?resize=612%2C192&#038;ssl=1\" alt=\"rpy2.ipython\" width=\"612\" height=\"192\" srcset=\"https:\/\/i0.wp.com\/www.thediar.com\/wp-content\/uploads\/Analizando-la-tasa-paro-con-paquetes-de-tidyverse-en-Google-Colab-Colaboratory.png?w=628&amp;ssl=1 628w, https:\/\/i0.wp.com\/www.thediar.com\/wp-content\/uploads\/Analizando-la-tasa-paro-con-paquetes-de-tidyverse-en-Google-Colab-Colaboratory.png?resize=300%2C94&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.thediar.com\/wp-content\/uploads\/Analizando-la-tasa-paro-con-paquetes-de-tidyverse-en-Google-Colab-Colaboratory.png?resize=520%2C163&amp;ssl=1 520w, https:\/\/i0.wp.com\/www.thediar.com\/wp-content\/uploads\/Analizando-la-tasa-paro-con-paquetes-de-tidyverse-en-Google-Colab-Colaboratory.png?resize=360%2C113&amp;ssl=1 360w, https:\/\/i0.wp.com\/www.thediar.com\/wp-content\/uploads\/Analizando-la-tasa-paro-con-paquetes-de-tidyverse-en-Google-Colab-Colaboratory.png?resize=250%2C78&amp;ssl=1 250w, https:\/\/i0.wp.com\/www.thediar.com\/wp-content\/uploads\/Analizando-la-tasa-paro-con-paquetes-de-tidyverse-en-Google-Colab-Colaboratory.png?resize=100%2C31&amp;ssl=1 100w\" sizes=\"auto, (max-width: 612px) 100vw, 612px\" \/><\/p>\n<p>Once this is done, remember to include <strong>%%R<\/strong> as the first line in every code cell where you want to execute code in R.<\/p>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-6283\" src=\"https:\/\/i0.wp.com\/www.thediar.com\/wp-content\/uploads\/magic-r-rpy2.png?resize=628%2C207&#038;ssl=1\" alt=\"\" width=\"628\" height=\"207\" srcset=\"https:\/\/i0.wp.com\/www.thediar.com\/wp-content\/uploads\/magic-r-rpy2.png?w=1143&amp;ssl=1 1143w, https:\/\/i0.wp.com\/www.thediar.com\/wp-content\/uploads\/magic-r-rpy2.png?resize=300%2C99&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.thediar.com\/wp-content\/uploads\/magic-r-rpy2.png?resize=1024%2C338&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.thediar.com\/wp-content\/uploads\/magic-r-rpy2.png?resize=768%2C253&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.thediar.com\/wp-content\/uploads\/magic-r-rpy2.png?resize=700%2C231&amp;ssl=1 700w, https:\/\/i0.wp.com\/www.thediar.com\/wp-content\/uploads\/magic-r-rpy2.png?resize=520%2C172&amp;ssl=1 520w, https:\/\/i0.wp.com\/www.thediar.com\/wp-content\/uploads\/magic-r-rpy2.png?resize=360%2C119&amp;ssl=1 360w, https:\/\/i0.wp.com\/www.thediar.com\/wp-content\/uploads\/magic-r-rpy2.png?resize=250%2C82&amp;ssl=1 250w, https:\/\/i0.wp.com\/www.thediar.com\/wp-content\/uploads\/magic-r-rpy2.png?resize=100%2C33&amp;ssl=1 100w\" sizes=\"auto, (max-width: 628px) 100vw, 628px\" \/><\/p>\n<h3>How to load an R file from Google Drive on my Google Colab notebook?<\/h3>\n<p>Well, this, among other reasons, is the reason why on Google Colab it is preferable to use the Python 3 runtime environment type with the magic %%R IPython command instead of using the R runtime environment directly.<\/p>\n<p>In order to connect a Google Colab notebook to Google Drive we have to use Python 3 code. Once connected, we can now use code in R.<\/p>\n<p>Here is an <a href=\"https:\/\/colab.research.google.com\/drive\/1zgfGLWQBhOvh5nz_4xFlMW3YJZFCamfd\">example<\/a>, in which we connect to Google Drive, create an R directory and save a hello_world.R file which is then executed.<\/p>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-6284\" src=\"https:\/\/i0.wp.com\/www.thediar.com\/wp-content\/uploads\/fichero-r-google-colab-google-drive.png?resize=481%2C492&#038;ssl=1\" alt=\"\" width=\"481\" height=\"492\" srcset=\"https:\/\/i0.wp.com\/www.thediar.com\/wp-content\/uploads\/fichero-r-google-colab-google-drive.png?w=743&amp;ssl=1 743w, https:\/\/i0.wp.com\/www.thediar.com\/wp-content\/uploads\/fichero-r-google-colab-google-drive.png?resize=293%2C300&amp;ssl=1 293w, https:\/\/i0.wp.com\/www.thediar.com\/wp-content\/uploads\/fichero-r-google-colab-google-drive.png?resize=700%2C716&amp;ssl=1 700w, https:\/\/i0.wp.com\/www.thediar.com\/wp-content\/uploads\/fichero-r-google-colab-google-drive.png?resize=520%2C532&amp;ssl=1 520w, https:\/\/i0.wp.com\/www.thediar.com\/wp-content\/uploads\/fichero-r-google-colab-google-drive.png?resize=360%2C368&amp;ssl=1 360w, https:\/\/i0.wp.com\/www.thediar.com\/wp-content\/uploads\/fichero-r-google-colab-google-drive.png?resize=250%2C256&amp;ssl=1 250w, https:\/\/i0.wp.com\/www.thediar.com\/wp-content\/uploads\/fichero-r-google-colab-google-drive.png?resize=100%2C102&amp;ssl=1 100w, https:\/\/i0.wp.com\/www.thediar.com\/wp-content\/uploads\/fichero-r-google-colab-google-drive.png?resize=50%2C50&amp;ssl=1 50w\" sizes=\"auto, (max-width: 481px) 100vw, 481px\" \/><\/p>\n<h3>How do I save the changes now?<\/h3>\n<p>If you have edited the notebook I shared with you, you will see that the changes cannot be saved. To do so, save a copy to your Google Drive account.<\/p>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-6285\" src=\"https:\/\/i0.wp.com\/www.thediar.com\/wp-content\/uploads\/google-colab-guardar-drive.png?resize=494%2C233&#038;ssl=1\" alt=\"\" width=\"494\" height=\"233\" srcset=\"https:\/\/i0.wp.com\/www.thediar.com\/wp-content\/uploads\/google-colab-guardar-drive.png?w=1274&amp;ssl=1 1274w, https:\/\/i0.wp.com\/www.thediar.com\/wp-content\/uploads\/google-colab-guardar-drive.png?resize=300%2C142&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.thediar.com\/wp-content\/uploads\/google-colab-guardar-drive.png?resize=1024%2C483&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.thediar.com\/wp-content\/uploads\/google-colab-guardar-drive.png?resize=768%2C362&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.thediar.com\/wp-content\/uploads\/google-colab-guardar-drive.png?resize=700%2C330&amp;ssl=1 700w, https:\/\/i0.wp.com\/www.thediar.com\/wp-content\/uploads\/google-colab-guardar-drive.png?resize=520%2C245&amp;ssl=1 520w, https:\/\/i0.wp.com\/www.thediar.com\/wp-content\/uploads\/google-colab-guardar-drive.png?resize=360%2C170&amp;ssl=1 360w, https:\/\/i0.wp.com\/www.thediar.com\/wp-content\/uploads\/google-colab-guardar-drive.png?resize=250%2C118&amp;ssl=1 250w, https:\/\/i0.wp.com\/www.thediar.com\/wp-content\/uploads\/google-colab-guardar-drive.png?resize=100%2C47&amp;ssl=1 100w\" sizes=\"auto, (max-width: 494px) 100vw, 494px\" \/><\/p>\n<h3>Should I use Google Colab or not?<\/h3>\n<p>One point in favor of using Google Colab is that it already has many packages pre-installed. Yes, the tidyverse packages are useful but if you use RStudio or VS Code on your computer, you have to install them on your computer.<\/p>\n<p>Although for professional environments I would recommend using RStudio or VS Code and many more things to <a href=\"https:\/\/raps-with-r.dev\/\">build reproducible processes in R<\/a>, Google Colab notebooks may have their audience.<\/p>\n<h3>And now what?<\/h3>\n<p>Well, just to say that there are more Google Colab tricks that are left unexplained and I could publish more in the future if it is a topic of interest. Do you want to share your own tricks? We are all ears.<\/p>","protected":false},"excerpt":{"rendered":"<p>If you don&#8217;t know, on Google Colab you can have free notebooks for programming on the cloud. All you need is a desire to learn and, of course, a Google account. The main advantages are: You start programming without wasting [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":6303,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-6280","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog","clearfix"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/www.thediar.com\/wp-content\/uploads\/google-colab.png?fit=275%2C183&ssl=1","jetpack_shortlink":"https:\/\/wp.me\/p8d1gX-1Di","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.thediar.com\/en\/wp-json\/wp\/v2\/posts\/6280","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.thediar.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.thediar.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.thediar.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.thediar.com\/en\/wp-json\/wp\/v2\/comments?post=6280"}],"version-history":[{"count":4,"href":"https:\/\/www.thediar.com\/en\/wp-json\/wp\/v2\/posts\/6280\/revisions"}],"predecessor-version":[{"id":6289,"href":"https:\/\/www.thediar.com\/en\/wp-json\/wp\/v2\/posts\/6280\/revisions\/6289"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.thediar.com\/en\/wp-json\/wp\/v2\/media\/6303"}],"wp:attachment":[{"href":"https:\/\/www.thediar.com\/en\/wp-json\/wp\/v2\/media?parent=6280"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.thediar.com\/en\/wp-json\/wp\/v2\/categories?post=6280"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.thediar.com\/en\/wp-json\/wp\/v2\/tags?post=6280"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}