Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong kernel for R files #120

Closed
kiendang opened this issue Nov 10, 2018 · 5 comments
Closed

Wrong kernel for R files #120

kiendang opened this issue Nov 10, 2018 · 5 comments

Comments

@kiendang
Copy link
Contributor

Issue

I'm not sure whether this is a jupyter or jupytext problem but jupyter doesn't set the correct kernel for .R files for me.

Steps to reproduce:

  • Create a blank .R file
  • Open the file in jupyter notebook

Problem

The kernel is set to the default kernel python3, not the one for R.

{
  "jupytext": {
    "metadata_filter": {
      "notebook": {
        "additional": [],
        "excluded": "all"
      },
      "cells": {
        "additional": [],
        "excluded": "all"
      }
    },
    "main_language": "R",
    "text_representation": {
      "extension": ".R",
      "format_name": "spin"
    }
  },
  "kernelspec": {
    "name": "python3",
    "display_name": "Python 3",
    "language": "python"
  },
  "language_info": {
    "name": "python",
    "version": "3.6.5",
    "mimetype": "text/x-python",
    "codemirror_mode": {
      "name": "ipython",
      "version": 3
    },
    "pygments_lexer": "ipython3",
    "nbconvert_exporter": "python",
    "file_extension": ".py"
  }
}

I have to change the kernel to R manually. This is the metadata after setting the correct kernel:

{
  "jupytext": {
    "metadata_filter": {
      "notebook": {
        "additional": [],
        "excluded": "all"
      },
      "cells": {
        "additional": [],
        "excluded": "all"
      }
    },
    "main_language": "R",
    "text_representation": {
      "extension": ".R",
      "format_name": "spin"
    }
  },
  "kernelspec": {
    "name": "ir",
    "display_name": "R",
    "language": "R"
  },
  "language_info": {
    "name": "R",
    "codemirror_mode": "r",
    "pygments_lexer": "r",
    "mimetype": "text/x-r-source",
    "file_extension": ".r",
    "version": "3.5.1"
  }
}

Is it possible to automatically use the R kernel for .R files?

@mwouts
Copy link
Owner

mwouts commented Nov 10, 2018

Hello @kiendang , thanks for reporting. And it's great to know that people are also using Jupytext for R files !

Jupyter always sets the default kernel when none is present in the document, as currently Jupytext does not set up any kernel by itself.

I agree that it would be better if Jupytext could search for a kernel with the matching language... I will investigate this when time permits (please leave the issue open till that). If you knew, in python, how to get an enumeration of available kernels, that would be a great start already.

I have another side remark: I see that your metadata filter excludes all the notebook metadata. Is that done on purpose? Note that, if you wanted to remove the filter on notebook metadata, you could temporarily prepare the R kernel yourself by appending the YAML representation for your kernel. You will obtain that representation by just replacing "excluded": "all" by "excluded": [] in the metadata, and saving again.

@kiendang
Copy link
Contributor Author

Thanks for the response! After thinking a bit more about this, I realize this might not be that big of an issue. I have multiple python kernels for different virtualenvs. So most of the time I have to manually set the correct kernel anyway.

That said it would be nice if jupytext can automatically pick 1 default kernel for each language. Would be useful for coding outside of a project and for R users who usually only have 1 R environment and therefore 1 R kernel.

I would be happy to help with this. Actually I did try to solve this first before opening the issue but didn't know where to start. I will try your suggestion. I guess the aim is to get an enumeration of available kernels then for each extension find the first kernel on the list with the correct language?

Yes the metadata thing is on purpose. I usually use jupytext as an editor with an interpreter for writing scripts and most of the time don't want to save metadata with the file.

@mwouts
Copy link
Owner

mwouts commented Nov 11, 2018

You're welcome! Thanks for your answers as well, that's very interesting for me to better know how people are using the tool.

I see that one can list all the Jupyter kernels in bash with jupyter kernelspec list. The corresponding Python module seems to be jupyter_kernel.kernelspec.

I did a quick experiment with the module, which indeed provides the list of kernels, and the information on the language. What is needed now is to find how one builds the kernelspec and language_info dictionnaries from a given kernel specification.

from jupyter_client import kernelspec
kernelspec.find_kernel_specs()
{'python3': 'C:\\Users\\Marc\\AppData\\Roaming\\jupyter\\kernels\\python3'}
py3 = kernelspec.get_kernel_spec('python3')
py3.language
'python'
py3.to_dict()
{'argv': ['C:\\Users\\Marc\\Anaconda3\\envs\\python3\\python.exe',
  '-m',
  'ipykernel_launcher',
  '-f',
  '{connection_file}'],
 'env': {},
 'display_name': 'Python 3',
 'language': 'python',
 'interrupt_mode': 'signal',
 'metadata': {}}

@mwouts
Copy link
Owner

mwouts commented Nov 13, 2018

Hello @kiendang , the latest release (0.8.5) provides an implementation for this. I have tested it locally with both R and bash languages.

@mwouts mwouts closed this as completed Nov 13, 2018
@kiendang
Copy link
Contributor Author

Great! Thanks a lot! I'll check it out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants