From 645e5f2279b7e286679a61b60f6047b2fae71eca Mon Sep 17 00:00:00 2001 From: Ben Marwick Date: Sun, 3 Sep 2017 18:20:11 -0700 Subject: [PATCH] add 'rmd_to_knit' arg to use_docker to that the dockerfile is aware of paper.Rmd location, following discussion in #38 --- Dockerfile | 7 ++++--- R/hello.R | 44 +++++++++++++++++++++++++++------------ README.Rmd | 5 +++-- inst/templates/Dockerfile | 5 +++-- man/use_analysis.Rd | 2 +- man/use_dockerfile.Rd | 10 ++++++++- 6 files changed, 51 insertions(+), 22 deletions(-) diff --git a/Dockerfile b/Dockerfile index 1a99876..e25ca54 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,8 +1,8 @@ # get the base image, the rocker/verse has R, RStudio and pandoc -FROM rocker/verse:3.4.0 +FROM rocker/verse:3.4.1 # required -MAINTAINER Your Name +MAINTAINER Ben Marwick COPY . /rrtools @@ -17,5 +17,6 @@ RUN . /etc/environment \ # build this compendium package && R -e "devtools::install('/rrtools', dep=TRUE)" \ - # render the manuscript into a docx + # render the manuscript into a docx, you'll need to edit this if you've + # customised the location and name of your main Rmd file && R -e "rmarkdown::render('/rrtools/analysis/paper/paper.Rmd')" diff --git a/R/hello.R b/R/hello.R index 1a33824..edb7987 100644 --- a/R/hello.R +++ b/R/hello.R @@ -193,7 +193,7 @@ use_circleci <- function(pkg = ".", browse = interactive(), docker_hub = TRUE) { #' @param data forwarded to \code{whisker::whisker.render} #' @param open_data should git track the files in the data directory? #' @export -use_analysis <- function(pkg = ".", location = "top_level", template = 'paper.Rmd', data = list(), open_data = TRUE) { +use_analysis <- function(pkg = ".", location = "top_level", template = 'paper.Rmd', data = list(), data_in_git = TRUE) { pkg <- as.package(pkg) pkg$Rmd <- TRUE gh <- github_info(pkg$path) @@ -228,7 +228,7 @@ use_analysis <- function(pkg = ".", location = "top_level", template = 'paper.Rm template) ) - if (!open_data) use_git_ignore("*/data/*") + if (!data_in_git) use_git_ignore("*/data/*") message("Next: \n", " * Write your article/paper/thesis in Rmd file(s) in analysis/paper/", "\n", @@ -251,36 +251,56 @@ invisible(TRUE) #' #' @param pkg defaults to the package in the current working directory #' @param rocker chr, the rocker image to base this container on +#' @param rmd_to_knit, chr, path to the Rmd file to render in the Docker +#' container, relative to the top level of the compendium +#' (i.e. "analysis/paper/paper.Rmd"). There's no need to specify this if your Rmd +#' to render is at "analysis/paper/paper.Rmd", "vignettes/paper/paper.Rmd" or +#' "inst/paper/paper.Rmd". If you have a custom directory structure, and a custom +#' file name for the Rmd file, you can specify that file path and name here so +#' Docker can find the file to render in the container.B #' #' @import utils devtools #' @export -use_dockerfile <- function(pkg = ".", rocker = "verse") { + +use_dockerfile <- function(pkg = ".", rocker = "verse", rmd_to_knit = "path_to_rmd") { pkg <- as.package(pkg) # get R version for rocker/r-ver si <- utils::sessionInfo() r_version <- paste0(si$R.version$major, ".", si$R.version$minor) + # get path to Rmd file to knit + if(rmd_to_knit == "path_to_rmd"){ + dir_list <- list.dirs() + paper_dir <- dir_list[grep(pattern = "/paper", dir_list)] + rmd_path <- regmatches(paper_dir, regexpr("analysis|vignettes|inst", paper_dir)) + rmd_path <- file.path(rmd_path, "paper/paper.Rmd") + } else { + rmd_path <- rmd_to_knit + } + + + # assign variables for whisker gh <- github_info(pkg$path) gh$r_version <- r_version gh$rocker <- rocker + gh$rmd_path <- rmd_path use_template("Dockerfile", - "Dockerfile", - ignore = TRUE, - pkg = pkg, - data = gh, - open = TRUE, - out_path = "") + "Dockerfile", + ignore = TRUE, + pkg = pkg, + data = gh, + open = TRUE, + out_path = "") message("Next: \n", " * Edit the dockerfile with your name & email", "\n", " * Edit the dockerfile to include system dependencies, such as linux libraries that are needed by the R packages you're using", "\n", - " * Edit the last line of the dockerfile to specify which Rmd should be rendered in the Docker container", "\n" ) + " * Check the last line of the dockerfile to specify which Rmd should be rendered in the Docker container, edit if necessary", "\n" ) invisible(TRUE) } - #' Creates skeleton README files #' #' @description \itemize{ @@ -326,7 +346,6 @@ use_readme_rmd <- function(pkg = ".") { pkg = pkg) } - message("* Rendering README.Rmd to README.md for GitHub.") rmarkdown::render("README.Rmd", output_format = NULL) @@ -336,7 +355,6 @@ use_readme_rmd <- function(pkg = ".") { message("* Adding instructions to contributors.") use_contributing() - invisible(TRUE) } diff --git a/README.Rmd b/README.Rmd index a1c639f..62c7e94 100644 --- a/README.Rmd +++ b/README.Rmd @@ -71,7 +71,7 @@ To create a reproducible research compendium using the rrtools approach, follow #### 5. `rrtools::use_analysis()` -- this has three `location` options: create a top-level `analysis/` directory, create an `inst/` directory (so that all the sub-directories are available after the package is installed), or create a `vingettes/` directory (and automatically update the `DESCRIPTION`). The default is a top-level `analysis/`. +- this function has three `location = ` options: create a top-level `analysis/` directory, create an `inst/` directory (so that all the sub-directories are available after the package is installed), or create a `vingettes/` directory (and automatically update the `DESCRIPTION`). The default is a top-level `analysis/`. - for each option, the contents of the sub-directories are the same, with the following (using the default `analysis/` for example): ``` @@ -99,7 +99,8 @@ analysis/ - the `references.bib` file has just one item to demonstrate the format. It is ready to insert more reference details. - you can replace the supplied `csl` file with a different citation style from - we recommend using the [citr addin](https://github.com/crsh/citr) and [Zotero](https://www.zotero.org/) to efficiently insert citations while writing in an Rmd file -- remember that the `Imports:` field in the `DESCRIPTION` file must include the names of all packages used in analysis documents (e.g. `paper.Rmd`) +- remember that the `Imports:` field in the `DESCRIPTION` file must include the names of all packages used in analysis documents (e.g. `paper.Rmd`). We have a helper function `rrtools::add_dependencies_to_description()` that will scan the Rmd file, identify libraries used in there, and add them to the `DESCRIPTION` file. +- this function has an `data_in_git =` argument, which is `TRUE` by defail. If set to `FALSE` you will exclude files in the `data/` directory from being tracked by git and prevent them from appearing on GitHub. You should set `data_in_git = FALSE` if your data files are large (>100 mb is the limit for GitHub) or you do not want to make the data files publicly accessible on GitHub. #### 6. `rrtools::use_dockerfile()` diff --git a/inst/templates/Dockerfile b/inst/templates/Dockerfile index 03eba1e..0e60a61 100644 --- a/inst/templates/Dockerfile +++ b/inst/templates/Dockerfile @@ -17,5 +17,6 @@ RUN . /etc/environment \ # build this compendium package && R -e "devtools::install('/{{{repo}}}', dep=TRUE)" \ - # render the manuscript into a docx - && R -e "rmarkdown::render('/{{{repo}}}/analysis/paper/paper.Rmd')" + # render the manuscript into a docx, you'll need to edit this if you've + # customised the location and name of your main Rmd file + && R -e "rmarkdown::render('/{{{repo}}}/{{{rmd_path}}}')" diff --git a/man/use_analysis.Rd b/man/use_analysis.Rd index 6c65cc8..2eaa7bc 100644 --- a/man/use_analysis.Rd +++ b/man/use_analysis.Rd @@ -6,7 +6,7 @@ \title{Adds an analysis directory (and sub-directories), and an Rmd file ready to write} \usage{ use_analysis(pkg = ".", location = "top_level", template = "paper.Rmd", - data = list(), open_data = TRUE) + data = list(), data_in_git = TRUE) } \arguments{ \item{pkg}{defaults to the package in the current working directory} diff --git a/man/use_dockerfile.Rd b/man/use_dockerfile.Rd index e1285f9..94341d1 100644 --- a/man/use_dockerfile.Rd +++ b/man/use_dockerfile.Rd @@ -4,12 +4,20 @@ \alias{use_dockerfile} \title{Add a Dockerfile} \usage{ -use_dockerfile(pkg = ".", rocker = "verse") +use_dockerfile(pkg = ".", rocker = "verse", rmd_to_knit = "path_to_rmd") } \arguments{ \item{pkg}{defaults to the package in the current working directory} \item{rocker}{chr, the rocker image to base this container on} + +\item{rmd_to_knit, }{chr, path to the Rmd file to render in the Docker +container, relative to the top level of the compendium +(i.e. "analysis/paper/paper.Rmd"). There's no need to specify this if your Rmd +to render is at "analysis/paper/paper.Rmd", "vignettes/paper/paper.Rmd" or +"inst/paper/paper.Rmd". If you have a custom directory structure, and a custom +file name for the Rmd file, you can specify that file path and name here so +Docker can find the file to render in the container.B} } \description{ This will create a basic \file{Dockerfile} based on rocker/verse