You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: book/r-config.qmd
+20Lines changed: 20 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -14,6 +14,26 @@ import os
14
14
print(os.environ["PATH"])
15
15
```
16
16
17
+
## Installing R packages
18
+
19
+
There is a user directory specified by default in the user's home directory. If this is persistent, then packages installed using
20
+
```
21
+
install.packages()
22
+
```
23
+
will by default be installed there and will be persistent.
24
+
25
+
The 2nd and 3rd paths on `.libPaths()` are in the `/usr` directory and will be recreated each time the Jupyter Hub is restarted and thus any package installed there by the user will disappear.
26
+
27
+
However, this means that if you are installing R package in a Docker image, they will by default go to the `/home/jovyan` user library and that will get wiped out in a Jupyter Hub where the user home is persistent since whatever is in `/home` during the Docker build will be replaced by the user home directory. In a Docker build, make sure to use
or use the helper script plus a `install.R` file in your Docker file:
32
+
```
33
+
COPY . /tmp2/
34
+
RUN /pyrocket_scripts/install-r-packages.sh /tmp2/install.R
35
+
```
36
+
17
37
## Using R in Jupyter Lab
18
38
19
39
In Jupyter Lab, you select a R kernel from the upper right. You can then use R code in the notebook. It will use the R installation in py-rocket with all the preloaded libraries.
Copy file name to clipboardExpand all lines: book/r-packages.qmd
+25-8Lines changed: 25 additions & 8 deletions
Original file line number
Diff line number
Diff line change
@@ -1,13 +1,26 @@
1
1
# R packages
2
2
3
-
To install extra R packages, use `install.R`. This is treated as an R script which is run. For example, you can have a script like
3
+
To install extra R packages in your Docker image, use `install.R` and `Rscript` in your Docker file.
4
+
5
+
```
6
+
COPY install.R /tmp/install.R
7
+
RUN Rscript /tmp/install.R
8
+
```
9
+
Make sure to install to `"${R_HOME}/site-library"` since by default `install.packages()` will install to the user library in `/home` and that will be replaced with the user home directory in Jupyter Hub with a persistent home directory.
in your Dockerfile. The extra code with a temporary `R_PROFILE` makes sure everything is installed to `"${R_HOME}/site-library"` and that the PATH does not have conda on it, which would break the needed linux installs.
47
+
* Use [r2u](https://github.com/eddelbuettel/r2u) which has Ubuntu binaries with all the dependencies included.
Copy file name to clipboardExpand all lines: docs/r-config.html
+31-18Lines changed: 31 additions & 18 deletions
Original file line number
Diff line number
Diff line change
@@ -192,14 +192,15 @@
192
192
<h2id="toc-title">Table of contents</h2>
193
193
194
194
<ul>
195
-
<li><ahref="#using-r-in-jupyter-lab" id="toc-using-r-in-jupyter-lab" class="nav-link active" data-scroll-target="#using-r-in-jupyter-lab"><spanclass="header-section-number">8.1</span> Using R in Jupyter Lab</a></li>
196
-
<li><ahref="#using-python-in-r-rstudio-or-jupyter-lab-with-r-kernel" id="toc-using-python-in-r-rstudio-or-jupyter-lab-with-r-kernel" class="nav-link" data-scroll-target="#using-python-in-r-rstudio-or-jupyter-lab-with-r-kernel"><spanclass="header-section-number">8.2</span> Using Python in R (RStudio or Jupyter Lab with R kernel)</a>
195
+
<li><ahref="#installing-r-packages" id="toc-installing-r-packages" class="nav-link active" data-scroll-target="#installing-r-packages"><spanclass="header-section-number">8.1</span> Installing R packages</a></li>
196
+
<li><ahref="#using-r-in-jupyter-lab" id="toc-using-r-in-jupyter-lab" class="nav-link" data-scroll-target="#using-r-in-jupyter-lab"><spanclass="header-section-number">8.2</span> Using R in Jupyter Lab</a></li>
197
+
<li><ahref="#using-python-in-r-rstudio-or-jupyter-lab-with-r-kernel" id="toc-using-python-in-r-rstudio-or-jupyter-lab-with-r-kernel" class="nav-link" data-scroll-target="#using-python-in-r-rstudio-or-jupyter-lab-with-r-kernel"><spanclass="header-section-number">8.3</span> Using Python in R (RStudio or Jupyter Lab with R kernel)</a>
<li><ahref="#using-a-conda-environment" id="toc-using-a-conda-environment" class="nav-link" data-scroll-target="#using-a-conda-environment"><spanclass="header-section-number">8.2.2</span> Using a conda environment</a></li>
<li><ahref="#using-a-conda-environment" id="toc-using-a-conda-environment" class="nav-link" data-scroll-target="#using-a-conda-environment"><spanclass="header-section-number">8.3.2</span> Using a conda environment</a></li>
200
201
</ul></li>
201
-
<li><ahref="#dealing-with-ssl-mismatch-errors" id="toc-dealing-with-ssl-mismatch-errors" class="nav-link" data-scroll-target="#dealing-with-ssl-mismatch-errors"><spanclass="header-section-number">8.3</span> Dealing with SSL mismatch errors</a></li>
<h2data-number="8.1" class="anchored" data-anchor-id="using-r-in-jupyter-lab"><spanclass="header-section-number">8.1</span> Using R in Jupyter Lab</h2>
<h2data-number="8.1" class="anchored" data-anchor-id="installing-r-packages"><spanclass="header-section-number">8.1</span> Installing R packages</h2>
238
+
<p>There is a user directory specified by default in the user’s home directory. If this is persistent, then packages installed using</p>
239
+
<pre><code>install.packages()</code></pre>
240
+
<p>will by default be installed there and will be persistent.</p>
241
+
<p>The 2nd and 3rd paths on <code>.libPaths()</code> are in the <code>/usr</code> directory and will be recreated each time the Jupyter Hub is restarted and thus any package installed there by the user will disappear.</p>
242
+
<p>However, this means that if you are installing R package in a Docker image, they will by default go to the <code>/home/jovyan</code> user library and that will get wiped out in a Jupyter Hub where the user home is persistent since whatever is in <code>/home</code> during the Docker build will be replaced by the user home directory. In a Docker build, make sure to use</p>
<h2data-number="8.2" class="anchored" data-anchor-id="using-r-in-jupyter-lab"><spanclass="header-section-number">8.2</span> Using R in Jupyter Lab</h2>
237
250
<p>In Jupyter Lab, you select a R kernel from the upper right. You can then use R code in the notebook. It will use the R installation in py-rocket with all the preloaded libraries.</p>
<h2data-number="8.2" class="anchored" data-anchor-id="using-python-in-r-rstudio-or-jupyter-lab-with-r-kernel"><spanclass="header-section-number">8.2</span> Using Python in R (RStudio or Jupyter Lab with R kernel)</h2>
<h2data-number="8.3" class="anchored" data-anchor-id="using-python-in-r-rstudio-or-jupyter-lab-with-r-kernel"><spanclass="header-section-number">8.3</span> Using Python in R (RStudio or Jupyter Lab with R kernel)</h2>
241
254
<p>The following behavior is specific to R, not the GUI (RStudio or Jupyter Lab with R kernel) that you are using to interact with it.</p>
<p>To use Python, you use the <code>reticulate</code> library. If you only need a handful of Python packages, it will simplify things if you use <code>py_require()</code>. Like this</p>
245
258
<pre><code>library(reticulate)
246
259
py_require("xarray")</code></pre>
@@ -249,8 +262,8 @@ <h3 data-number="8.2.1" class="anchored" data-anchor-id="py_require"><span class
249
262
<pre><code>rm ~/.cache/R/reticulate</code></pre>
250
263
<p>in a terminal to get reticulate to allow me to use <code>use_conda("notebook")</code> in another R session.</p>
<h3data-number="8.2.2" class="anchored" data-anchor-id="using-a-conda-environment"><spanclass="header-section-number">8.2.2</span> Using a conda environment</h3>
<h3data-number="8.3.2" class="anchored" data-anchor-id="using-a-conda-environment"><spanclass="header-section-number">8.3.2</span> Using a conda environment</h3>
254
267
<p>You can also use the conda environment with reticulate with all the pre-installed packages.</p>
<p>When we use a conda environment, the PATH is altered so that the conda environment directory appears first on the PATH. Any R packages that need a particular system package that is also in conda (like GDAL) are likely to throw mis-match errors.</p>
<h2data-number="8.4" class="anchored" data-anchor-id="dealing-with-ssl-mismatch-errors"><spanclass="header-section-number">8.4</span> Dealing with SSL mismatch errors</h2>
270
283
<p>When you use reticulate in R, use <code>use_condaenv()</code> and call a function that needs to download data, you are liable to get a OpenSSL mismatch error. py-rocket solves this by adding this to</p>
<p>to <code>/etc/rstudio/rserver.conf</code>. This let’s R know where to look for SSL links and hopefully doesn’t break R packages. Make sure that <code>.Renviron</code> does not set <code>LD_LIBRARY_PATH</code> or this solution will not work. I don’t know why but it breaks.</p>
<p>How is the R kernel created so that it shows up in Jupyter Lab? You don’t need to install R into the conda environment since it already is in the image. We just need to use <code>IRkernel</code> R package to register the kernel with jupyter.</p>
277
290
<pre><code>Rscript - <<-"EOF"
278
291
install.packages('IRkernel', lib = .Library) # install in system library
0 commit comments