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

Optional arguments in R magic are lost in translation #111

Closed
capelastegui opened this issue Oct 24, 2018 · 11 comments
Closed

Optional arguments in R magic are lost in translation #111

capelastegui opened this issue Oct 24, 2018 · 11 comments
Assignees
Milestone

Comments

@capelastegui
Copy link

When using cell magics in my jupyter notebook, I have noticed that optional arguments sometimes are lost when translating to .py files. This happens with the R magic, and may also be an issue with other language-related magics.

Normal magics with arguments work fine:

%%testmagic -my_parameter x
print('hello world')

This gets converted to

# %%testmagic -my_parameter x
print('hello world')

However, the %%R magic is handled differently, and the parameter is lost:

%%R -my_parameter x
print('hello world')

This gets converted to

# + {"active": "ipynb", "language": "R"}
# print('hello world')
# -

Note the missing parameter.

I have run a quick test on jupytext.magics, and that seems to be working as intended - the problem appears because the %%R magic is handled elsewhere:

from jupytext.magics import comment_magic, uncomment_magic, unesc
comment_magic(['%%R', '%%R -my_parameter x'])

I'm using jupytext 0.8.3, jupyter 1.0.0, python 2.7.11

@mwouts mwouts self-assigned this Oct 24, 2018
@mwouts
Copy link
Owner

mwouts commented Oct 24, 2018

Hello @capelastegui , thanks for reporting! It's nice to know that people do actually mix languages within a single notebook - I like this.

Sure, I agree with your report, the arguments for language magics are (currently) lost in the round trip conversion. We will fix this.

May I ask you if you could share a simple and short notebook with an example? You could either

  • contribute the notebook at tests/notebooks/ipynb_py using a pull request (a few tests will fail, due to the issue you are reporting, this is expected),
  • send it to me per email,
  • or attach it here as a txt file.

@mwouts
Copy link
Owner

mwouts commented Oct 25, 2018

@capelastegui , I have integrated your sample notebook ! Now conversion of magic %%R cells with arguments work well when converting to

I have not tested the conversion to R scripts (your notebook is classified among the python notebooks, due to its python kernel...). Please let me know if there is an issue there.

The new release candidate should not create any more the active metadata. I also expect that it will fix #114 . Could you please try and confirm (you may have to remove that metadata first by hand) ? Thanks !

pip install jupytext==0.8.4rc1

@capelastegui
Copy link
Author

Just installed the latest RC and tried it with an old notebook with the active metadata. Everything ran smoothly. As far as I can tell, both this issue and #114 are fixed in that release.

My use cases just deal with notebook conversion to python, so I can't speak about any errors that might come up when converting to the R format. I may try that in the near future, though - I'll let you know how that goes.

Many thanks for the quick fix, and for this great tool - it's making my work on jupyter so much easier.

@mwouts
Copy link
Owner

mwouts commented Oct 29, 2018

Thanks @capelastegui for the feedback!
I have just published v0.8.4 with the latest corrections.

@mwouts mwouts closed this as completed Oct 29, 2018
@stefanuddenberg
Copy link

I'm having this exact issue with jupytext 0.8.6. It seems to be specific to the %%R cell magic on round-trip conversion. No matter what I do, whenever I reopen the file, the arguments are gone.

@mwouts mwouts reopened this Feb 16, 2019
@mwouts
Copy link
Owner

mwouts commented Feb 16, 2019

Hello @stefanuddenberg , thanks for letting us know. Do you think you could provide a minimal reproducible example, i.e. a short notebook with just one cell, that shows the problem? I would be interested in seeing both the notebook, and the problematic text representation.

You can either email me the two files, or prepare a pull request on branch 1.0.0 with the notebook added into the folder tests\notebooks\ipynb_py. The notebook should have R_magic in the name. Running the tests (which will reproduce your issue and are expected to fail) will contribute the text representations.

@mwouts
Copy link
Owner

mwouts commented Feb 16, 2019

Or even simpler: just quote here the content of one cell showing the issue, plus its text representation... Thanks.

@stefanuddenberg
Copy link

Setup:

%load_ext rpy2.ipython
import pandas as pd

df = pd.DataFrame(
    {
        "Letter": ["a", "a", "a", "b", "b", "b", "c", "c", "c"],
        "X": [4, 3, 5, 2, 1, 7, 7, 5, 9],
        "Y": [0, 4, 3, 6, 7, 10, 11, 9, 13],
        "Z": [1, 2, 3, 1, 2, 3, 1, 2, 3],
    }
)

Original problematic cell in .ipynb (arguments get stripped here on reloading notebook):

%%R -i df
library("ggplot2")
ggplot(data = df) + geom_point(aes(x = X, y = Y, color = Letter, size = Z))

Cell as output in the py:percent format:

# %% {"magic_args": "-i df", "language": "R"}
# library("ggplot2")
# ggplot(data = df) + geom_point(aes(x = X, y = Y, color = Letter, size = Z))

Cell as output to .md format (encapsulated in an ```R block):

library("ggplot2")
ggplot(data = df) + geom_point(aes(x = X, y = Y, color = Letter, size = Z))

@mwouts
Copy link
Owner

mwouts commented Feb 19, 2019

Thanks @stefanuddenberg, you have provided all I need to reproduce this.

Just out of curiosity: are you exporting to both the percent and md formats at the same time? Also, have you tried the .Rmd format, which supports cell metadata on code cells (unlike md)?

@mwouts
Copy link
Owner

mwouts commented Feb 23, 2019

@stefanuddenberg , I have added your notebook to the test suite at 75f9011.

The test pass as expected for the light, percent, hydrogen and Rmd formats. As expected, they don't pass for the md format, which cannot contain cell metadata.

So my understanding is that you issue is caused by using md as the text format here. Please switch to Rmd, and let us know if that solves your issue.

@mwouts
Copy link
Owner

mwouts commented Apr 14, 2019

Starting from version 1.1.0, metadata are also accepted in the markdown representation of notebooks. This should solve this issue.

@mwouts mwouts closed this as completed Apr 14, 2019
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

3 participants