# R Jupyter Notebook + RStan on Google Colab

**URL:** <https://discourse.mc-stan.org/t/r-jupyter-notebook-rstan-on-google-colab/6101>\
**Category:** RStan\
**Tags:** rstan\
**Created:** [October 19, 2018, 2:17pm UTC](https://discourse.mc-stan.org/t/r-jupyter-notebook-rstan-on-google-colab/6101 "2018-10-19T14:17:35Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thinking\_Machine](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/thinking_machine/32/4794_2.png) [@Thinking\_Machine](https://discourse.mc-stan.org/u/Thinking_Machine)\
**Post date:** [October 19, 2018, 2:17pm UTC](https://discourse.mc-stan.org/t/r-jupyter-notebook-rstan-on-google-colab/6101/1 "2018-10-19T14:17:35Z")

</div>

Hi every one,

I’ve created a simple notebook to support R and Rstan on Google Colab.

You just need to copy this notebook to your Google Drive.

[https://colab.research.google.com/drive/1xj\_aYLBBPX2oSQ1I4xp5\_YZiVhhpC1Ke](https://colab.research.google.com/drive/1xj_aYLBBPX2oSQ1I4xp5_YZiVhhpC1Ke)

I was inspired by

> **[Julia on Google Colab: Free GPU-Accelerated Shareable Notebooks](https://discourse.julialang.org/t/julia-on-google-colab-free-gpu-accelerated-shareable-notebooks/15319)**
>
> (Disclosure: While I work for Google, this is not an official Google product or announcement.) tl;dr: For Julia on Colab with GPUs, first open this notebook and run the cell (takes ~15-20 minutes), then open this one to start using Julia. ...

which supports Julia on Google Colab.

Having fun!

---

<div class="post-metadata">

**Author:** ![ahartikainen](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/ahartikainen/32/321_2.png) [@ahartikainen](https://discourse.mc-stan.org/u/ahartikainen)\
**Post date:** [October 19, 2018, 4:29pm UTC](https://discourse.mc-stan.org/t/r-jupyter-notebook-rstan-on-google-colab/6101/2 "2018-10-19T16:29:28Z")

</div>

It should work from github too?

> **[ahartikainen/PyStan\_Google\_Colab\_Demo](https://github.com/ahartikainen/PyStan_Google_Colab_Demo)**
>
> Speeding up demos with caching. Contribute to ahartikainen/PyStan\_Google\_Colab\_Demo development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![Thinking\_Machine](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/thinking_machine/32/4794_2.png) [@Thinking\_Machine](https://discourse.mc-stan.org/u/Thinking_Machine)\
**Post date:** [October 19, 2018, 4:50pm UTC](https://discourse.mc-stan.org/t/r-jupyter-notebook-rstan-on-google-colab/6101/3 "2018-10-19T16:50:11Z")

</div>

Yes It works on Github repo and Gist too.

---

<div class="post-metadata">

**Author:** ![Bob\_Carpenter](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bob_carpenter/32/9230_2.png) [@Bob\_Carpenter](https://discourse.mc-stan.org/u/Bob_Carpenter)\
**Post date:** [October 23, 2018, 1:10am UTC](https://discourse.mc-stan.org/t/r-jupyter-notebook-rstan-on-google-colab/6101/4 "2018-10-23T01:10:18Z")

</div>

Cool! Thanks for sharing. There’s also the version on RStudio that pulls up an RStudio session on the web rather than Jupyter. @andrewgelman has been looking for these to show new users without having to install Stan.

---

<div class="post-metadata">

**Author:** ![ali\_serin](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@ali\_serin](https://discourse.mc-stan.org/u/ali_serin)\
**Post date:** [January 17, 2019, 9:25pm UTC](https://discourse.mc-stan.org/t/r-jupyter-notebook-rstan-on-google-colab/6101/5 "2019-01-17T21:25:15Z")

</div>

I couln’t use library() function. I can install packages but I can’t use them. Do you know how to solve this?  
I believe using :: notation works. But I am trying to write something like, as(myMatrix, “realRatingMatrix”) and it can’t do this. recommenderlab::as() also not working.

---

<div class="post-metadata">

**Author:** ![Thinking\_Machine](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/thinking_machine/32/4794_2.png) [@Thinking\_Machine](https://discourse.mc-stan.org/u/Thinking_Machine)\
**Post date:** [January 19, 2019, 12:32pm UTC](https://discourse.mc-stan.org/t/r-jupyter-notebook-rstan-on-google-colab/6101/6 "2019-01-19T12:32:06Z")

</div>

Can you run the R code examples in my notebook successfully?

[https://colab.research.google.com/drive/1xj\_aYLBBPX2oSQ1I4xp5\_YZiVhhpC1Ke](https://colab.research.google.com/drive/1xj_aYLBBPX2oSQ1I4xp5_YZiVhhpC1Ke)

I used library() function in these examples and it seems to work correctly.

---

<div class="post-metadata">

**Author:** ![arthur](https://avatars.discourse-cdn.com/v4/letter/a/958977/32.png) [@arthur](https://discourse.mc-stan.org/u/arthur)\
**Post date:** [March 3, 2019, 11:19pm UTC](https://discourse.mc-stan.org/t/r-jupyter-notebook-rstan-on-google-colab/6101/7 "2019-03-03T23:19:55Z")

</div>

Hello Ari, I was unable to run the script on my Drive (sorry, newcomer on Colab).  
The error:

–2019-03-03 19:31:33-- [https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86\_64.sh](https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh)  
Resolving [repo.continuum.io](http://repo.continuum.io) ([repo.continuum.io](http://repo.continuum.io))… 104.18.200.79, 104.18.201.79, 2606:4700::6812:c84f, …  
Connecting to [repo.continuum.io](http://repo.continuum.io) ([repo.continuum.io](http://repo.continuum.io))|104.18.200.79|:443… connected.  
HTTP request sent, awaiting response… 200 OK  
Length: 69826864 (67M) [application/x-sh]  
Saving to: ‘Miniconda3-latest-Linux-x86\_64.sh.4’  
Miniconda3-latest-L 100%[===================\>] 66.59M 130MB/s in 0.5s  
2019-03-03 19:31:33 (130 MB/s) - ‘Miniconda3-latest-Linux-x86\_64.sh.4’ saved [69826864/69826864]  
mkdir: cannot create directory ‘/root/.R/’: File exists

Have any ideias on how to surpass it?  
Thanks for all the work done here!

---

<div class="post-metadata">

**Author:** ![Thinking\_Machine](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/thinking_machine/32/4794_2.png) [@Thinking\_Machine](https://discourse.mc-stan.org/u/Thinking_Machine)\
**Post date:** [March 4, 2019, 5:33am UTC](https://discourse.mc-stan.org/t/r-jupyter-notebook-rstan-on-google-colab/6101/8 "2019-03-04T05:33:18Z")

</div>

Hi,

You may first have to reset your runtime by going to menu **Runtime -\> Reset all runtimes**. After that, run **Setup code**. After 3 mins your runtime will be restarted. Now, go to **Runtime -\> Manage Session** , and terminate your Session. Reconnect and **skip** the Setup code, run the R code.

---

<div class="post-metadata">

**Author:** ![robbat1](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/robbat1/32/4791_2.png) [@robbat1](https://discourse.mc-stan.org/u/robbat1)\
**Post date:** [May 15, 2019, 3:51am UTC](https://discourse.mc-stan.org/t/r-jupyter-notebook-rstan-on-google-colab/6101/9 "2019-05-15T03:51:58Z")

</div>

Hi,

How could I install new packages like ggmap?

Thanks,

Rob

---

<div class="post-metadata">

**Author:** ![Thinking\_Machine](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/thinking_machine/32/4794_2.png) [@Thinking\_Machine](https://discourse.mc-stan.org/u/Thinking_Machine)\
**Post date:** [May 15, 2019, 9:44am UTC](https://discourse.mc-stan.org/t/r-jupyter-notebook-rstan-on-google-colab/6101/10 "2019-05-15T09:44:44Z")

</div>

Hi,

Recently, Google Colab had secretly supported R notebooks. We don’t need to install R and R jupyter kernel ourself anymore. All we need is selecting “ir” jupyter kernel. This hints that Google has the intention to fully support R in Colab in a near future.

Here is an example of a R notebook with rstan and ggmap installed:  
[https://colab.research.google.com/drive/1BYnnbqeyZAlYnxR9IHC8tpW07EpDeyKR](https://colab.research.google.com/drive/1BYnnbqeyZAlYnxR9IHC8tpW07EpDeyKR)

---

<div class="post-metadata">

**Author:** ![Vighnesh\_Bilgi](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/vighnesh_bilgi/32/5311_2.png) [@Vighnesh\_Bilgi](https://discourse.mc-stan.org/u/Vighnesh_Bilgi)\
**Post date:** [July 6, 2019, 3:47pm UTC](https://discourse.mc-stan.org/t/r-jupyter-notebook-rstan-on-google-colab/6101/11 "2019-07-06T15:47:26Z")

</div>

Hi,

I keep getting this error : mkdir: cannot create directory ‘/root/.R/’: File exists  
when I run the first code segment  
what does this mean?

---

<div class="post-metadata">

**Author:** ![Vighnesh\_Bilgi](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/vighnesh_bilgi/32/5311_2.png) [@Vighnesh\_Bilgi](https://discourse.mc-stan.org/u/Vighnesh_Bilgi)\
**Post date:** [July 6, 2019, 4:35pm UTC](https://discourse.mc-stan.org/t/r-jupyter-notebook-rstan-on-google-colab/6101/12 "2019-07-06T16:35:57Z")

</div>

Do you have a code that works for windows?

---

<div class="post-metadata">

**Author:** ![Thinking\_Machine](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/thinking_machine/32/4794_2.png) [@Thinking\_Machine](https://discourse.mc-stan.org/u/Thinking_Machine)\
**Post date:** [July 7, 2019, 3:25am UTC](https://discourse.mc-stan.org/t/r-jupyter-notebook-rstan-on-google-colab/6101/13 "2019-07-07T03:25:16Z")

</div>

You should use the new notebook [https://colab.research.google.com/drive/1BYnnbqeyZAlYnxR9IHC8tpW07EpDeyKR](https://colab.research.google.com/drive/1BYnnbqeyZAlYnxR9IHC8tpW07EpDeyKR)  
which is supported by Google servers.

The notebook runs on Google colab servers (which is free). I’m pretty sure that it will not work on Windows without a proper setup.

---

<div class="post-metadata">

**Author:** ![simona\_buscemi](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/simona_buscemi/32/5626_2.png) [@simona\_buscemi](https://discourse.mc-stan.org/u/simona_buscemi)\
**Post date:** [August 7, 2019, 4:55pm UTC](https://discourse.mc-stan.org/t/r-jupyter-notebook-rstan-on-google-colab/6101/14 "2019-08-07T16:55:32Z")

</div>

I’m using the notebook you created for R. It’s amazing. Everything works until i tried to install the package “rgl”, or better “ConsRank” which needs “rgl”.  
Could anyone help me somehow? Please, i’m working for my PhD results :(

This is what appears:

Warning message in install.packages(“rgl”, repos = “[http://cran.rstudio.com/](http://cran.rstudio.com/)”):  
“installation of package ‘rgl’ had non-zero exit status”Updating HTML index of packages in ‘.Library’  
Making ‘packages.html’ … done  
also installing the dependency ‘rgl’

Warning message in install.packages(“ConsRank”, repos = “[http://cran.rstudio.com/](http://cran.rstudio.com/)”):  
“installation of package ‘rgl’ had non-zero exit status”Warning message in install.packages(“ConsRank”, repos = “[http://cran.rstudio.com/](http://cran.rstudio.com/)”):  
“installation of package ‘ConsRank’ had non-zero exit status”Updating HTML index of packages in ‘.Library’  
Making ‘packages.html’ … done

---

<div class="post-metadata">

**Author:** ![Thinking\_Machine](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/thinking_machine/32/4794_2.png) [@Thinking\_Machine](https://discourse.mc-stan.org/u/Thinking_Machine)\
**Post date:** [August 8, 2019, 9:44am UTC](https://discourse.mc-stan.org/t/r-jupyter-notebook-rstan-on-google-colab/6101/15 "2019-08-08T09:44:25Z")

</div>

Hi,

The below 3 lines work for me (after 10 mins of running)

```
install.packages("rgl", repos = "http://cran.rstudio.com/")
install.packages("ConsRank", repos = "http://cran.rstudio.com/")
library("ConsRank")

```

My rule of thumb is that when something is wrong, reset your google colab instance.

---

<div class="post-metadata">

**Author:** ![simona\_buscemi](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/simona_buscemi/32/5626_2.png) [@simona\_buscemi](https://discourse.mc-stan.org/u/simona_buscemi)\
**Post date:** [August 8, 2019, 2:07pm UTC](https://discourse.mc-stan.org/t/r-jupyter-notebook-rstan-on-google-colab/6101/16 "2019-08-08T14:07:01Z")

</div>

> [@Thinking\_Machine](#):
>
> install.packages(“rgl”, repos = “[http://cran.rstudio.com/](http://cran.rstudio.com/)”) install.packages(“ConsRank”, repos = “[http://cran.rstudio.com/](http://cran.rstudio.com/)”) library(“ConsRank”)

 ![07](https://canada1.discourse-cdn.com/flex030/uploads/mc_stan/original/2X/4/42f7d19d9fc2e08c7512a80529c5a8fee57e7e25.png)

---

<div class="post-metadata">

**Author:** ![Thinking\_Machine](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/thinking_machine/32/4794_2.png) [@Thinking\_Machine](https://discourse.mc-stan.org/u/Thinking_Machine)\
**Post date:** [August 9, 2019, 12:47pm UTC](https://discourse.mc-stan.org/t/r-jupyter-notebook-rstan-on-google-colab/6101/17 "2019-08-09T12:47:14Z")

</div>

I run the commands at the beginning of the notebook. This could solve your problem.

---

<div class="post-metadata">

**Author:** ![simona\_buscemi](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/simona_buscemi/32/5626_2.png) [@simona\_buscemi](https://discourse.mc-stan.org/u/simona_buscemi)\
**Post date:** [August 9, 2019, 2:02pm UTC](https://discourse.mc-stan.org/t/r-jupyter-notebook-rstan-on-google-colab/6101/18 "2019-08-09T14:02:28Z")

</div>

![35](https://canada1.discourse-cdn.com/flex030/uploads/mc_stan/original/2X/8/8ad9d310d6b9c626ed58f1eade436bc0da424fad.png)  
I tried too but nothing changes. Is my problem in your opinion due to the fact i’m using a MAC Os? I guess there is a problem in managing the directory of the libraries, i have the same problem as you can see even in installing the library “ggmap”. I’m getting crazy

---

<div class="post-metadata">

**Author:** ![Thinking\_Machine](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/thinking_machine/32/4794_2.png) [@Thinking\_Machine](https://discourse.mc-stan.org/u/Thinking_Machine)\
**Post date:** [August 9, 2019, 2:22pm UTC](https://discourse.mc-stan.org/t/r-jupyter-notebook-rstan-on-google-colab/6101/19 "2019-08-09T14:22:27Z")

</div>

Your notebook is running on Google server so really nothing related to your computer.

Please clone this notebook as an example:

[https://colab.research.google.com/drive/1ou6GKwmEq9Jj2xt2j-t8M\_m8WGJWDv2i](https://colab.research.google.com/drive/1ou6GKwmEq9Jj2xt2j-t8M_m8WGJWDv2i)

**Before running it, don’t forget to reset the server at the menu Runtime -\> Reset all runtimes…**

---

<div class="post-metadata">

**Author:** ![Apeksha](https://avatars.discourse-cdn.com/v4/letter/a/8baadc/32.png) [@Apeksha](https://discourse.mc-stan.org/u/Apeksha)\
**Post date:** [September 20, 2019, 4:08am UTC](https://discourse.mc-stan.org/t/r-jupyter-notebook-rstan-on-google-colab/6101/20 "2019-09-20T04:08:47Z")

</div>

Hi,  
I am trying to run the set up code provided to run R on it. But my sessions are crashing again and again. I have reset the runtime session but still it is not running.

 ![Capture](https://canada1.discourse-cdn.com/flex030/uploads/mc_stan/original/2X/3/31c048faff02990faa4ce66fc007bb0b90d38e79.png)

[Next page](https://discourse.mc-stan.org/t/r-jupyter-notebook-rstan-on-google-colab/6101.md?page=2)
