<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Setup on Research Tool</title>
    <link>/tags/setup/</link>
    <description>Recent content in Setup on Research Tool</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en</language>
    <managingEditor>cahoover@gmail.com (Christopher Hoover)</managingEditor>
    <webMaster>cahoover@gmail.com (Christopher Hoover)</webMaster>
    <lastBuildDate>Wed, 19 Feb 2025 00:00:00 +0000</lastBuildDate><atom:link href="/tags/setup/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>Adding an R kernel to Jupyter Lab</title>
      <link>/posts/adding-an-r-kernel-to-jupyter-lab/</link>
      <pubDate>Wed, 19 Feb 2025 00:00:00 +0000</pubDate>
      <author>cahoover@gmail.com (Christopher Hoover)</author>
      <guid>/posts/adding-an-r-kernel-to-jupyter-lab/</guid>
      <description>&lt;p&gt;Being bilingual has been shown to increase people&amp;rsquo;s ability to focus and concentrate, solve problems, and have better mental flexibility.&lt;/p&gt;
&lt;p&gt;It&amp;rsquo;s also important that data scientists limit themselves to relying on a single programming language for data management, visualization, modeling, and deployment.&lt;/p&gt;
&lt;p&gt;JupyterLab is a web-based interactive development environment for data analysis, scientific computing, and predictive modeling.&lt;/p&gt;
&lt;h2 id=&#34;step-1---install-anaconda&#34;&gt;Step 1 - Install Anaconda&lt;/h2&gt;
&lt;p&gt;First we need to download and install the Anaconda from the official website.&lt;/p&gt;</description>
      <content>&lt;p&gt;Being bilingual has been shown to increase people&amp;rsquo;s ability to focus and concentrate, solve problems, and have better mental flexibility.&lt;/p&gt;
&lt;p&gt;It&amp;rsquo;s also important that data scientists limit themselves to relying on a single programming language for data management, visualization, modeling, and deployment.&lt;/p&gt;
&lt;p&gt;JupyterLab is a web-based interactive development environment for data analysis, scientific computing, and predictive modeling.&lt;/p&gt;
&lt;h2 id=&#34;step-1---install-anaconda&#34;&gt;Step 1 - Install Anaconda&lt;/h2&gt;
&lt;p&gt;First we need to download and install the Anaconda from the official website.&lt;/p&gt;
&lt;p&gt;After downloading and installing Anaconda, open a new macOS Terminal and make sure everything was installed correctly by entering the following commands:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;$ Jupyter Lab
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This should open your web browser displaying the JupyterLab interface. The Anaconda environment has sections for opening a Python Kernel in a Jupyter Notebook or the Console.&lt;/p&gt;
&lt;p&gt;Python is run via Jupyter&amp;rsquo;s kernel. Under the Other section, there are also options for starting a bash shell session, creating new text or markdown files, or getting contextual help.&lt;/p&gt;
&lt;h2 id=&#34;step-2---install-the-necessary-r-packages&#34;&gt;Step 2 - Install the necessary R packages&lt;/h2&gt;
&lt;p&gt;There are nine packages we need to install to setup the R Kernel in the Jupyter Lab environment:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;repr&lt;/strong&gt;: String and binary representations of objects for several formats mime types.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;IRdisplay&lt;/strong&gt;: An interface to the rich display capabilities of Jupyter front-ends.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;evaluate&lt;/strong&gt;: Parsing and evaluation tools that make it easy to recreate the command line behavior of R.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;crayon&lt;/strong&gt;: Colored terminal output on terminals that support ANSI color and highlight codes.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;pbdZMQ&lt;/strong&gt;: An R package providing a simplified interface to ZeroMQ with a focus on client/server programming frameworks.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;devtools&lt;/strong&gt;: Collection of package development tools.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;uuid&lt;/strong&gt;: Tools for generating and handling of UUIDs (Universally Unique Identifiers).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;digest&lt;/strong&gt;: Provides hash function summaries for GNU R objects.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;IRkernel&lt;/strong&gt;: R kernel for Jupyter&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;These packages can be installed by running the code below in the R console:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# install from CRAN&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;install.packages&lt;/span&gt;(&lt;span style=&#34;color:#a6e22e&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;repr&amp;#39;&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;IRdisplay&amp;#39;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                   &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;evaluate&amp;#39;&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;crayon&amp;#39;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                   &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;pbdZMQ&amp;#39;&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;devtools&amp;#39;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                   &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;uuid&amp;#39;&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;digest&amp;#39;&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# from github&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;devtools&lt;span style=&#34;color:#f92672&#34;&gt;::&lt;/span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;install_github&lt;/span&gt;(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;IRkernel/IRkernel&amp;#39;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;After we&amp;rsquo;ve installed these packages, we need to find our version of R on our local machine. On macOS, it is usually located in the &lt;code&gt;/Library/Frameworks&lt;/code&gt; folder.&lt;/p&gt;
&lt;p&gt;We use the pwd command in Terminal to show where the application is, and then use &lt;code&gt;open R&lt;/code&gt; to launch a session of R in the Terminal.&lt;/p&gt;
&lt;p&gt;Once in the R session, run:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;IRkernel&lt;span style=&#34;color:#f92672&#34;&gt;::&lt;/span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;installspec&lt;/span&gt;()
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# [InstallKernelSpec] Installed kernelspec ir in /Users/mjfrigaard/Library/Jupyter/kernels/ir&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;IRkernel&lt;span style=&#34;color:#f92672&#34;&gt;::&lt;/span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;installspec&lt;/span&gt;(user &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;FALSE&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# [InstallKernelSpec] Installed kernelspec ir in /usr/local/share/jupyter/kernels/ir&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;After these have been installed, we need to close the JupyterLab environment by exiting out of the browser. We will also need to close the Jupyter Kernel in the Terminal by clicking on &lt;code&gt;ctrl + c&lt;/code&gt; and then &lt;code&gt;y&lt;/code&gt;.&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;[I 08:22:46.480 LabApp] interrupted
Serving notebooks from local directory: /Users/path/to/project/
0 active kernels
The Jupyter Notebook is running at:
http://localhost:8888/?token=de9b3fdc50a633411e67c9596f5090e5f0d4d18bcac6c860
 or http://127.0.0.1:8888/?token=de9b3fdc50a633411e67c9596f5090e5f0d4d18bcac6c860
Shutdown this notebook server (y/[n])? y
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Start another JupyterLab session and you should see the option for an R Kernel under the Notebook and Console sections.&lt;/p&gt;
&lt;h2 id=&#34;step-3---launch-an-r-kernel-from-jupyter&#34;&gt;Step 3 - Launch an R Kernel from Jupyter&lt;/h2&gt;
&lt;p&gt;Now we want to launch the R Kernel and enter some R code to make sure everything is working. We can test the R Kernel in a Notebook, because Jupyter notebooks give us the ability to insert R code, markdown, or plain text code cells.&lt;/p&gt;
&lt;h3 id=&#34;running-r-from-jupyter-notebook&#34;&gt;Running R from Jupyter Notebook&lt;/h3&gt;
&lt;p&gt;We now have an R Kernel running in Jupyter Lab. The code below loads the popular tidyverse package from RStudio:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;suppressPackageStartupMessages&lt;/span&gt;(&lt;span style=&#34;color:#a6e22e&#34;&gt;library&lt;/span&gt;(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;tidyverse&amp;#34;&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;tidyverse&lt;span style=&#34;color:#f92672&#34;&gt;::&lt;/span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;tidyverse_logo&lt;/span&gt;()
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;We can write in markdown or code, just like the Python environment:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;diamonds &lt;span style=&#34;color:#f92672&#34;&gt;%&amp;gt;%&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  ggplot2&lt;span style=&#34;color:#f92672&#34;&gt;::&lt;/span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;ggplot&lt;/span&gt;(&lt;span style=&#34;color:#a6e22e&#34;&gt;aes&lt;/span&gt;(x &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; carat, y &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; price, color &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; cut)) &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    ggplot2&lt;span style=&#34;color:#f92672&#34;&gt;::&lt;/span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;geom_point&lt;/span&gt;() &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      ggplot2&lt;span style=&#34;color:#f92672&#34;&gt;::&lt;/span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;geom_smooth&lt;/span&gt;()
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;#&amp;gt;  `geom_smooth()` using method = &amp;#39;gam&amp;#39; and formula &amp;#39;y ~ s(x, bs = &amp;#34;cs&amp;#34;)&amp;#39;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;</content>
    </item>
    
  </channel>
</rss>
