~hrbrmstr/markdowntemplates

30d95385677ee922cb20af0f8ed6f95b5d1e5344 — boB Rudis 1 year, 1 month ago 10ace29
running of to_jupyter() notebooks is now optional
6 files changed, 77 insertions(+), 26 deletions(-)

M DESCRIPTION
M NAMESPACE
M R/ipynb.r
M R/markdowntemplates-package.R
M README.Rmd
M README.md
M DESCRIPTION => DESCRIPTION +4 -3
@@ 1,8 1,8 @@
Package: markdowntemplates
Type: Package
Title: Alternate R Markdown Templates
Version: 0.4.1
Date: 2018-05-31
Version: 0.5.0
Date: 2019-01-08
Authors@R: c(
    person("Bob", "Rudis", email = "bob@rud.is", role = c("aut", "cre"), 
           comment = c(ORCID = "0000-0001-5670-2640")),


@@ 28,6 28,7 @@ Imports:
    knitr,
    rmarkdown,
    htmltools,
    tools
    tools,
    yaml
Encoding: UTF-8
RoxygenNote: 6.1.1

M NAMESPACE => NAMESPACE +1 -0
@@ 17,4 17,5 @@ export(to_jupyter)
import(htmltools)
import(knitr)
import(rmarkdown)
import(yaml)
importFrom(tools,file_path_sans_ext)

M R/ipynb.r => R/ipynb.r +35 -11
@@ 12,7 12,7 @@
#' \preformatted{---
#' title: "ggplot2 example"
#' knit: markdowntemplates::to_jupyter
#' output: md_document
#' run: true
#' ---
#'
#' ## Introduction to ggplot2


@@ 78,21 78,46 @@ to_jupyter <- function(inputFile, encoding) {

    output_file <-  sprintf("%s.%s", tools::file_path_sans_ext(inputFile), "ipynb")

    tf <- tempfile(fileext=".Rmd")
    tf1 <- tempfile(fileext=".Rmd")
    on.exit(unlink(tf1), add=TRUE)

    message(" - parsing & stripping YAML header")

    message(" - stripping YAML header")
    tmp <- readLines(inputFile)
    yaml_end <- which(grepl("^---", tmp))[2]
    writeLines(tmp[(yaml_end+1):length(tmp)], tf)

    yaml_bits <- which(grepl("^---", tmp))

    # yaml
    cfg <- read_yaml(text=paste0(tmp[yaml_bits[1]:yaml_bits[2]], collapse="\n"))

    args <- c(tf1, "--knit") # "--rmagic", "--run")

    if ((length(cfg$run) == 0) || (cfg$run == "true")) {
      message(" - notebook will be executed")
      args <- c(args, "--run")
    } else {
      message(" - notebook will not be executed")
    }

    rblocks <- grep("^```\\{r", tmp[(yaml_bits[2]+1):length(tmp)], value=TRUE)
    rblocks <- grep('engine', rblocks, invert = TRUE) # assume engine spec is python

    has_r_blocks <- length(rblocks) > 0

    if (has_r_blocks){
      message(" - R code blocks detected; notebook will include '%load_ext rpy2.ipython'")
      args <- c(args, "--rmagic")
    }

    # Rmd
    writeLines(tmp[(yaml_bits[2]+1):length(tmp)], tf1)

    message(" - running notedown...")
    system2(cmd, args=c(tf, "--knit", "--rmagic", "--run"), stdout=output_file)
    system2(cmd, args=args, stdout = output_file, wait = TRUE)

    message("Completed conversion.")
    Sys.sleep(0.5)

    unlink(tf)

    message(sprintf("\nOutput file is at: [%s]\n", output_file))

    cmd <- Sys.which("jupyter")


@@ 101,13 126,12 @@ to_jupyter <- function(inputFile, encoding) {
    out <- system2(cmd, args=c("nbconvert", "--to html", output_file, "--output", html_file),
                   stdout=TRUE, stderr=TRUE, wait=TRUE)

    message(sprintf("\nHTML preview is at: [%s]\n", html_file))
    message(sprintf("HTML preview is at: [%s]\n", html_file))

    utils::browseURL(html_file)

    output_file
    invisible(output_file)

  }


}

M R/markdowntemplates-package.R => R/markdowntemplates-package.R +1 -1
@@ 47,6 47,6 @@
#' @name markdowntemplates
#' @docType package
#' @author Bob Rudis (bob@rud.is)
#' @import rmarkdown knitr htmltools
#' @import rmarkdown knitr htmltools yaml
#' @importFrom tools file_path_sans_ext
NULL

M README.Rmd => README.Rmd +19 -6
@@ 28,15 28,28 @@ When you use RStudio to create a new R Markdown document, select "From Template"

- `to_jupyter()` : uses `notedown` to make an `ipynb` out of an Rmd

### Installation
If you create an R markdown (Rmd) document with the following YAML header:

Since the package is not on CRAN yet, you have to install it from GitHub.
    ---
    knit: markdowntemplates::to_jupyter
    run: false
    ---

```
if (!("devtools" %in% installed.packages())){
  install.packages("devtools")
}
The `to_jupyter()` engine will be used and [`notedown`](https://github.com/aaren/notedown) will be used to convert the notebook to a Jupyter (ipynb) notebook. The `run` parameter is optional. If not present or set to `true` the notebook will be executed as it is converted to a Jupyter notebook. The engine is also now smart enough to detect the absence of pure R code chunks and avoid including of:

    %load_ext rpy2.ipython
    
at the top of the notebook.

### Installation

Since the package is not on CRAN yet, you have to install it devtools-style

```{r eval=FALSE}
devtools::install_git("https://sr.ht/~hrbrmstr/markdowntemplates")
# OR
devtools::install_git("https://gitlab.com/hrbrmstr/markdowntemplates")
# OR
devtools::install_github("hrbrmstr/markdowntemplates")
```


M README.md => README.md +17 -5
@@ 27,16 27,28 @@ When you use RStudio to create a new R Markdown document, select "From Template"
### knit engine replacements

- `to_jupyter()` : uses `notedown` to make an `ipynb` out of an Rmd
If you create an R markdown (Rmd) document with the following YAML header:

    ---
    knit: markdowntemplates::to_jupyter
    run: false
    ---

The `to_jupyter()` engine will be used and [`notedown`](https://github.com/aaren/notedown) will be used to convert the notebook to a Jupyter (ipynb) notebook. The `run` parameter is optional. If not present or set to `true` the notebook will be executed as it is converted to a Jupyter notebook. The engine is also now smart enough to detect the absence of pure R code chunks and avoid including of:

    %load_ext rpy2.ipython
    
at the top of the notebook.

### Installation

Since the package is not on CRAN yet, you have to install it from GitHub.
Since the package is not on CRAN yet, you have to install it devtools-style

```
if (!("devtools" %in% installed.packages())){
  install.packages("devtools")
}

devtools::install_git("https://sr.ht/~hrbrmstr/markdowntemplates")
# OR
devtools::install_git("https://gitlab.com/hrbrmstr/markdowntemplates")
# OR
devtools::install_github("hrbrmstr/markdowntemplates")
```