From cd8079dc88612dfb9ff70654498910be4122e4ce Mon Sep 17 00:00:00 2001 From: Fernando Mayer <fernandomayer@gmail.com> Date: Fri, 28 Aug 2015 17:05:32 -0300 Subject: [PATCH] Create function to run all chunks in a Rmd file --- R/runAllChunks.R | 34 ++++++++++++++++++++++++++++++++++ man/runAllChunks.Rd | 31 +++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 R/runAllChunks.R create mode 100644 man/runAllChunks.Rd diff --git a/R/runAllChunks.R b/R/runAllChunks.R new file mode 100644 index 0000000..cc7ff57 --- /dev/null +++ b/R/runAllChunks.R @@ -0,0 +1,34 @@ +#' @title Run all chunks in a Rmd file +#' +#' @name runAllChunks +#' +#' @description This function was developed to run all chunks in a knitr +#' Rmd (R markdown) file at once. Mainly for exploring and debugging +#' purposes. +#' +#' @param Rmd the name of the Rmd file. +#' +#' @param envir the environment in which the chunks will be +#' evaluated. By default it is the GlobalEnv. +#' +#' @references This function was based on this +#' \href{http://stackoverflow.com/questions/24753969/knitr-run-all-chunks-in-an-rmarkdown-document}{SO +#' thread}. +#' +#' @return Objects created in the chunks from the Rmd file. +#' +#' @import knitr +#' +#' @author Fernando Mayer, \email{fernando.mayer@@ufpr.br} +#' +#' @export +runAllChunks <- function(Rmd, envir = globalenv()){ + if (!requireNamespace("knitr", quietly = TRUE)){ + stop("`knitr` needed for this function to work. Please install it.", + call. = FALSE) + } + tempR <- tempfile(tmpdir = ".", fileext = ".R") + on.exit(unlink(tempR)) + purl(Rmd, output = tempR) + sys.source(tempR, envir = envir) +} diff --git a/man/runAllChunks.Rd b/man/runAllChunks.Rd new file mode 100644 index 0000000..3602646 --- /dev/null +++ b/man/runAllChunks.Rd @@ -0,0 +1,31 @@ +% Generated by roxygen2 (4.1.1): do not edit by hand +% Please edit documentation in R/runAllChunks.R +\name{runAllChunks} +\alias{runAllChunks} +\title{Run all chunks in a Rmd file} +\usage{ +runAllChunks(Rmd, envir = globalenv()) +} +\arguments{ +\item{Rmd}{the name of the Rmd file.} + +\item{envir}{the environment in which the chunks will be +evaluated. By default it is the GlobalEnv.} +} +\value{ +Objects created in the chunks from the Rmd file. +} +\description{ +This function was developed to run all chunks in a knitr +Rmd (R markdown) file at once. Mainly for exploring and debugging +purposes. +} +\author{ +Fernando Mayer, \email{fernando.mayer@ufpr.br} +} +\references{ +This function was based on this + \href{http://stackoverflow.com/questions/24753969/knitr-run-all-chunks-in-an-rmarkdown-document}{SO + thread}. +} + -- GitLab