Stream: jupyterlab-hub

Topic: R kernel?


view this post on Zulip Matt Long (Sep 10 2021 at 17:02):

Does anyone know how to build an R kernel, preferably using conda, that's available in JupyterLab (jupyterhub)?

view this post on Zulip Jared Baker (Sep 10 2021 at 17:49):

I can probably dig up some instructions for it. I used conda to install the one on JupyterHub, but was told that R doesn't get much traction here at NCAR, so only update the installation when requested.

view this post on Zulip Matt Long (Sep 10 2021 at 18:47):

Thanks @Jared Baker!

I think I am able to build an R environment with conda—but am unsure how to get JLab on the hub to "see" it, i.e. via whatever the nb_conda_kernels equivalent is.

I am not much of an R user myself, but there is a small contingent of users at NCAR and it could be nice to document how to construct replete kernels. I tried your default R kernel, but apparently it lack library('ncdf4') which I presume precludes reading netCDF files.

view this post on Zulip Jared Baker (Sep 10 2021 at 19:03):

Part of the installation is opening up the R console basically and install the spec file which is basically a json blob if I remember right on how to launch the kernel and such.

IRkernel::installspec(name = 'ir$MAJOR$MINOR, displayname = 'R $VERSION')

here is the json spec for the installed IRkernel specs:

{
  "argv": ["/ncar/usr/jupyterhub/envs/r-3.5.1/lib/R/bin/R", "--slave", "-e", "IRkernel::main()", "--args", "{connection_file}"],
  "display_name": "R 3.5.1",
  "language": "R"
}
bash-4.2$ cat kernel.js
const cmd_key = /Mac/.test(navigator.platform) ? 'Cmd' : 'Ctrl'

const edit_actions = [
        {
                name: 'R Assign',
                shortcut: 'Alt--',
                icon: 'fa-long-arrow-left',
                help: 'R: Inserts the left-assign operator (<-)',
                handler(cm) {
                        cm.replaceSelection(' <- ')
                },
        },
        {
                name: 'R Pipe',
                shortcut: `Shift-${cmd_key}-M`,
                icon: 'fa-angle-right',
                help: 'R: Inserts the magrittr pipe operator (%>%)',
                handler(cm) {
                        cm.replaceSelection(' %>% ')
                },
        },
        {
                name: 'R Help',
                shortcut: 'F1',
                icon: 'fa-book',
                help: 'R: Shows the manpage for the item under the cursor',
                handler(cm, cell) {
                        const {anchor, head} = cm.findWordAt(cm.getCursor())
                        const word = cm.getRange(anchor, head)

                        const callbacks = cell.get_callbacks()
                        const options = {silent: false, store_history: false, stop_on_error: true}
                        cell.last_msg_id = cell.notebook.kernel.execute(`help(\`${word}\`)`, callbacks, options)
                },
        },
]

const prefix = 'irkernel'

function add_edit_shortcut(notebook, actions, keyboard_manager, edit_action) {
        const {name, shortcut, icon, help, handler} = edit_action

        const action = {
                icon, help,
                help_index : 'zz',
                handler: () => {
                        const cell = notebook.get_selected_cell()
                        handler(cell.code_mirror, cell)
                },
        }

        const full_name = actions.register(action, name, prefix)

        Jupyter.keyboard_manager.edit_shortcuts.add_shortcut(shortcut, full_name)
}

function render_math(pager, html) {
        if (!html) return
        const $container = pager.pager_element.find('#pager-container')
        $container.find('p[style="text-align: center;"]').map((i, e) =>
                e.outerHTML = `\\[${e.querySelector('i').innerHTML}\\]`)
        $container.find('i').map((i, e) =>
                e.outerHTML = `\\(${e.innerHTML}\\)`)
        MathJax.Hub.Queue(['Typeset', MathJax.Hub, $container[0]])
}

define(['base/js/namespace'], ({
        notebook,
        actions,
        keyboard_manager,
        pager,
}) => ({
        onload() {
                edit_actions.forEach(a => add_edit_shortcut(notebook, actions, keyboard_manager, a))

                pager.events.on('open_with_text.Pager', (event, {data: {'text/html': html}}) =>
                        render_math(pager, html))
        },
}))```

view this post on Zulip Matt Long (Sep 10 2021 at 19:11):

thanks...

view this post on Zulip Matt Long (Sep 10 2021 at 19:11):

I'll try to play around with this info

view this post on Zulip Thomas Hauser (Sep 10 2021 at 19:30):

@Matt Long my unsolicited advice is to let R users use Rstudio. I've taught parallel R a few times at workshops and my experience with using an R kernel in Jupyterlab was that most learners were hating it because they were used to Rstudio.

view this post on Zulip Matt Long (Sep 10 2021 at 19:33):

Sounds like good advice—though the etymology of "Jupyter" is "a reference to the three core programming languages supported by Jupyter, which are Julia, Python and R." So I was under the naive impression that R should be easily supported. Also, I don't use R, but my collaborator does—and I was hoping to be able to build collections of notebooks in both Python and R.

view this post on Zulip Cassandra Olenick (May 26 2022 at 18:16):

I realize this thread ended a long time ago. I'm an Rstudio user and would like to utilize Rstudio in JupyterLab, I was wondering if anyone knows if this is possible or how to do it. I have JupyterLab installed locally and use the JupyterHub supported at NCAR

view this post on Zulip Matt Long (May 26 2022 at 18:41):

I have never used Rstudio, but my understanding is that it provides an interactive computing environment like Jupyter Notebooks—so you can use one or the other. It is possible use R in a Jupyter Notebook and the NCAR JupyterHub includes an R kernel.

I was able to build my own R kernel using this environment file and the following conda command

conda env create -f environment-R.yml

This approach enables using R packages not included in the default kernel, though I don't know how extensive the conda support for R is.

view this post on Zulip Jared Baker (May 27 2022 at 23:37):

We just updated our R kernel to a newer version. If this is insufficient please ping me as I’d like to have a conversation of this doesn’t meet your needs. @Cassandra Olenick


Last updated: May 16 2025 at 17:14 UTC