Skip to content

Commit

Permalink
Merge pull request #39 from mwouts/v0.5.4
Browse files Browse the repository at this point in the history
V0.5.4
  • Loading branch information
mwouts committed Aug 24, 2018
2 parents 1fa808b + 2e5f6c0 commit 29e9b7d
Show file tree
Hide file tree
Showing 12 changed files with 410 additions and 25 deletions.
9 changes: 9 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ Release History
dev
+++

0.5.4 (2018-08-24)
+++++++++++++++++++

**Improvements**

- R to Rmd conversion compares well to knitr::spin (#26)
- Increased coverage to 98%


0.5.3 (2018-08-22)
+++++++++++++++++++

Expand Down
2 changes: 1 addition & 1 deletion binder/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
nbrmd>=0.5.3
nbrmd>=0.5.4
plotly
matplotlib
pandas
8 changes: 4 additions & 4 deletions nbrmd/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ def cli_nbrmd(args=None):
return parser.parse_args(args)


def nbrmd():
def nbrmd(args=None):
"""
Entry point for the nbrmd script
:return:
"""
args = cli_nbrmd()
args = cli_nbrmd(args)
convert(args.notebooks, args.in_place, args.preserve_outputs, True)


Expand Down Expand Up @@ -122,10 +122,10 @@ def cli_nbsrc(args=None):
return parser.parse_args(args)


def nbsrc():
def nbsrc(args=None):
"""
Entry point for the nbsrc script
:return:
"""
args = cli_nbsrc()
args = cli_nbsrc(args)
convert(args.notebooks, args.in_place, args.preserve_outputs, False)
6 changes: 6 additions & 0 deletions nbrmd/nbrmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ def to_notebook(self, text, **kwargs):

if self.ext == '.Rmd':
find_main_language(metadata, cells)
elif self.ext == '.R':
if not (metadata.get('main_language') or
metadata.get('language_info', {})):
metadata['main_language'] = 'R'

nb = new_notebook(cells=cells, metadata=metadata)
return nb
Expand Down Expand Up @@ -140,6 +144,8 @@ def writes(self, nb, **kwargs):
default_language = (nb.metadata.get('main_language') or
nb.metadata.get('language_info', {})
.get('name', 'R'))
if nb.metadata.get('main_language') == 'R':
del nb.metadata['main_language']
else:
default_language = get_default_language(nb)

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

setup(
name='nbrmd',
version='0.5.3',
version='0.5.4',
author='Marc Wouts',
author_email='marc.wouts@gmail.com',
description='Jupyter notebooks as R markdown, Python or R scripts',
Expand Down
54 changes: 54 additions & 0 deletions tests/knitr-spin.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#' The below derives from
#' https://github.com/yihui/knitr/blob/master/inst/examples/knitr-spin.R
#'
#' This is a special R script which can be used to generate a report. You can
#' write normal text in roxygen comments.
#'
#' First we set up some options (you do not have to do this):

#+ setup, include=FALSE
library(knitr)
opts_chunk$set(fig.path = 'figure/silk-')

#' The report begins here.

#+ test-a, cache=FALSE
# boring examples as usual
set.seed(123)
x = rnorm(5)
mean(x)

#' You can not use here the special syntax {{code}} to embed inline expressions, e.g.
{{mean(x) + 2}}
#' is the mean of x plus 2.
#' The code itself may contain braces, but these are not checked. Thus,
#' perfectly valid (though very strange) R code such as `{{2 + 3}} - {{4 - 5}}`
#' can lead to errors because `2 + 3}} - {{4 - 5` will be treated as inline code.
#'
#' Now we continue writing the report. We can draw plots as well.

#+ test-b, fig.height=5, fig.width=5
par(mar = c(4, 4, .1, .1)); plot(x)

#' Actually you do not have to write chunk options, in which case knitr will use
#' default options. For example, the code below has no options attached:

var(x)
quantile(x)

#' And you can also write two chunks successively like this:

#+ test-chisq5
sum(x^2) # chi-square distribution with df 5
#+ test-chisq4
sum((x - mean(x))^2) # df is 4 now

#' Done. Call spin('knitr-spin.R') to make silk from sow's ear now and knit a
#' lovely purse.

# /* you can write comments between /* and */ like C comments (the preceding #
# is optional)
Sys.sleep(60)
# */

# /* there is no inline comment; you have to write block comments */
65 changes: 65 additions & 0 deletions tests/knitr-spin.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
The below derives from
https://github.com/yihui/knitr/blob/master/inst/examples/knitr-spin.R

This is a special R script which can be used to generate a report. You can
write normal text in roxygen comments.

First we set up some options (you do not have to do this):

```{r setup, include=FALSE}
library(knitr)
opts_chunk$set(fig.path = 'figure/silk-')
```

The report begins here.

```{r test-a, cache=FALSE}
# boring examples as usual
set.seed(123)
x = rnorm(5)
mean(x)
```

You can not use here the special syntax {{code}} to embed inline expressions, e.g.
```{r}
{{mean(x) + 2}}
```
is the mean of x plus 2.
The code itself may contain braces, but these are not checked. Thus,
perfectly valid (though very strange) R code such as `{{2 + 3}} - {{4 - 5}}`
can lead to errors because `2 + 3}} - {{4 - 5` will be treated as inline code.

Now we continue writing the report. We can draw plots as well.

```{r test-b, fig.height=5, fig.width=5}
par(mar = c(4, 4, .1, .1)); plot(x)
```

Actually you do not have to write chunk options, in which case knitr will use
default options. For example, the code below has no options attached:

```{r}
var(x)
quantile(x)
```

And you can also write two chunks successively like this:

```{r test-chisq5}
sum(x^2) # chi-square distribution with df 5
```
```{r test-chisq4}
sum((x - mean(x))^2) # df is 4 now
```

Done. Call spin('knitr-spin.R') to make silk from sow's ear now and knit a
lovely purse.

```{r}
# /* you can write comments between /* and */ like C comments (the preceding #
# is optional)
Sys.sleep(60)
# */
# /* there is no inline comment; you have to write block comments */
```
172 changes: 172 additions & 0 deletions tests/mirror/knitr-spin.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The below derives from\n",
"https://github.com/yihui/knitr/blob/master/inst/examples/knitr-spin.R\n",
"\n",
"This is a special R script which can be used to generate a report. You can\n",
"write normal text in roxygen comments.\n",
"\n",
"First we set up some options (you do not have to do this):"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"hide_output": true,
"name": "setup"
},
"outputs": [],
"source": [
"library(knitr)\n",
"opts_chunk$set(fig.path = 'figure/silk-')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The report begins here."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cache": false,
"name": "test-a"
},
"outputs": [],
"source": [
"# boring examples as usual\n",
"set.seed(123)\n",
"x = rnorm(5)\n",
"mean(x)"
]
},
{
"cell_type": "markdown",
"metadata": {
"noskipline": true
},
"source": [
"You can not use here the special syntax {{code}} to embed inline expressions, e.g."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"noskipline": true
},
"outputs": [],
"source": [
"{{mean(x) + 2}}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"is the mean of x plus 2.\n",
"The code itself may contain braces, but these are not checked. Thus,\n",
"perfectly valid (though very strange) R code such as `{{2 + 3}} - {{4 - 5}}`\n",
"can lead to errors because `2 + 3}} - {{4 - 5` will be treated as inline code.\n",
"\n",
"Now we continue writing the report. We can draw plots as well."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"fig.height": 5,
"fig.width": 5,
"name": "test-b"
},
"outputs": [],
"source": [
"par(mar = c(4, 4, .1, .1)); plot(x)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Actually you do not have to write chunk options, in which case knitr will use\n",
"default options. For example, the code below has no options attached:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"var(x)\n",
"quantile(x)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"And you can also write two chunks successively like this:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"name": "test-chisq5",
"noskipline": true
},
"outputs": [],
"source": [
"sum(x^2) # chi-square distribution with df 5"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"name": "test-chisq4"
},
"outputs": [],
"source": [
"sum((x - mean(x))^2) # df is 4 now"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Done. Call spin('knitr-spin.R') to make silk from sow's ear now and knit a\n",
"lovely purse."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# /* you can write comments between /* and */ like C comments (the preceding #\n",
"# is optional)\n",
"Sys.sleep(60)\n",
"# */\n",
"\n",
"# /* there is no inline comment; you have to write block comments */"
]
}
],
"metadata": {
"main_language": "R"
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading

0 comments on commit 29e9b7d

Please sign in to comment.