Skip to content

Instantly share code, notes, and snippets.

@ramnathv
Last active July 13, 2018 22:12
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ramnathv/7284926 to your computer and use it in GitHub Desktop.
Save ramnathv/7284926 to your computer and use it in GitHub Desktop.

This is a short demo of how to use brew and knitr in combination with each other to get the best of the templating facilities in brew and the literate programming functions in knitr. The main idea is to write a function brew_knit

# Preprocess template using brew and then run knit on the output
brew_knit <- function(template, params, ...){
 brew::brew(template, envir = list2env(params))
 input = gsub(".Rnwe", '.Rnw', template)
 knitr::knit(input)
}

brew_knit first expands the template using params passed to it. The expanded template is then run through knit to produce the final output. One can pass further parameters to knit using the ... argument. Moreover, this function can be generalized to allow different markup document formats.

Shown below is a an example taken from Prof. Frank Harrell's mail on the knitr mailing list. The template is doc.Rnwe which is expanded to doc.Rnw and then knit to doc.tex.

UPDATE: At times, you might want to inherit the paramter list from the global environment (suggestion by @baptiste). Although undesirable IMHO from a reproducibility perspective, it can be done by tweaking the brew_knit function

# Preprocess template using brew and then run knit on the output
brew_knit2 <- function(template, params, envir = parent.frame(), ...){
 if (missing(params)){
   myenvir = envir
 } else {
   myenvir = list2env(params)
 }
 brew::brew(template, envir = myenvir)
 input = gsub(".Rnwe", '.Rnw', template)
 knitr::knit(input)
}
# Preprocess template using brew and then run knit on the output
brew_knit <- function(template, ...){
brew(template, envir = list2env(list(...)))
input = gsub(".Rnwe", '.Rnw', template)
knit(input)
}
# Example Application
brew_knit('doc.Rnwe', params = list(
v = c("hip", "lumbar", "femur", "forearm"),
oth1 = c("lumbar", "hip", "hip", "hip"),
oth2 = c("femur", "femur", "lumbar", "lumbar"),
oth3 = c("forearm", "forearm", "forearm", "femur")
))
\section{Analysis of Hip Bone Mineral Density}
\subsection{26w BMD and Baseline Predictors}
In this section I model 26w interpolated BMD using baseline BMD (all four measures, not just the target measure), baseline weight, age,sex, race, \$t\$-score stratum, and treatment. I first fit a saturatedmodel with respect to all of the continuous predictors, using restricted cubic splines with 5 knots, then potentially use partial \$\chi^2\$ (blinded to nonlinearity components) to reassign degrees of freedom.
<< hip-sat26,results="asis", eval = F>>=
f <- ols(hip26 ~ sex*rcs(hip0,5) +
rcs(lumbar0,5) + rcs(femur0,5) +
rcs(forearm0,5) + sex*rcs(wt0, 5) + trtp +
rcs(age, 5) + race + sex + blppar + bltscgrp, data=d)
print(f, coefs = FALSE, latex = TRUE)
lan(f)
@
The global test for nonlinearity is nowhere close to being significant, so all nonlinear effects will be dropped.
\section{Analysis of Lumbar Bone Mineral Density}
\subsection{26w BMD and Baseline Predictors}
In this section I model 26w interpolated BMD using baseline BMD (all four measures, not just the target measure), baseline weight, age,sex, race, \$t\$-score stratum, and treatment. I first fit a saturatedmodel with respect to all of the continuous predictors, using restricted cubic splines with 5 knots, then potentially use partial \$\chi^2\$ (blinded to nonlinearity components) to reassign degrees of freedom.
<< lumbar-sat26,results="asis", eval = F>>=
f <- ols(lumbar26 ~ sex*rcs(lumbar0,5) +
rcs(hip0,5) + rcs(femur0,5) +
rcs(forearm0,5) + sex*rcs(wt0, 5) + trtp +
rcs(age, 5) + race + sex + blppar + bltscgrp, data=d)
print(f, coefs = FALSE, latex = TRUE)
lan(f)
@
The global test for nonlinearity is nowhere close to being significant, so all nonlinear effects will be dropped.
\section{Analysis of Femur Bone Mineral Density}
\subsection{26w BMD and Baseline Predictors}
In this section I model 26w interpolated BMD using baseline BMD (all four measures, not just the target measure), baseline weight, age,sex, race, \$t\$-score stratum, and treatment. I first fit a saturatedmodel with respect to all of the continuous predictors, using restricted cubic splines with 5 knots, then potentially use partial \$\chi^2\$ (blinded to nonlinearity components) to reassign degrees of freedom.
<< femur-sat26,results="asis", eval = F>>=
f <- ols(femur26 ~ sex*rcs(femur0,5) +
rcs(hip0,5) + rcs(lumbar0,5) +
rcs(forearm0,5) + sex*rcs(wt0, 5) + trtp +
rcs(age, 5) + race + sex + blppar + bltscgrp, data=d)
print(f, coefs = FALSE, latex = TRUE)
lan(f)
@
The global test for nonlinearity is nowhere close to being significant, so all nonlinear effects will be dropped.
\section{Analysis of Forearm Bone Mineral Density}
\subsection{26w BMD and Baseline Predictors}
In this section I model 26w interpolated BMD using baseline BMD (all four measures, not just the target measure), baseline weight, age,sex, race, \$t\$-score stratum, and treatment. I first fit a saturatedmodel with respect to all of the continuous predictors, using restricted cubic splines with 5 knots, then potentially use partial \$\chi^2\$ (blinded to nonlinearity components) to reassign degrees of freedom.
<< forearm-sat26,results="asis", eval = F>>=
f <- ols(forearm26 ~ sex*rcs(forearm0,5) +
rcs(hip0,5) + rcs(lumbar0,5) +
rcs(femur0,5) + sex*rcs(wt0, 5) + trtp +
rcs(age, 5) + race + sex + blppar + bltscgrp, data=d)
print(f, coefs = FALSE, latex = TRUE)
lan(f)
@
The global test for nonlinearity is nowhere close to being significant, so all nonlinear effects will be dropped.
<% for (i in 1:4) { -%>
<% title = gsub('^([a-z])', '\\U\\1', v[i], perl = T) %>
\section{Analysis of <%= title %> Bone Mineral Density}
\subsection{26w BMD and Baseline Predictors}
In this section I model 26w interpolated BMD using baseline BMD (all four measures, not just the target measure), baseline weight, age,sex, race, \$t\$-score stratum, and treatment. I first fit a saturatedmodel with respect to all of the continuous predictors, using restricted cubic splines with 5 knots, then potentially use partial \$\chi^2\$ (blinded to nonlinearity components) to reassign degrees of freedom.
<< <%= v[i] %>-sat26,results="asis", eval = F>>=
f <- ols(<%= v[i] %>26 ~ sex*rcs(<%= v[i] %>0,5) +
rcs(<%= oth1[i] %>0,5) + rcs(<%= oth2[i] %>0,5) +
rcs(<%= oth3[i] %>0,5) + sex*rcs(wt0, 5) + trtp +
rcs(age, 5) + race + sex + blppar + bltscgrp, data=d)
print(f, coefs = FALSE, latex = TRUE)
lan(f)
@
The global test for nonlinearity is nowhere close to being significant, so all nonlinear effects will be dropped.
<% } %>
\section{Analysis of Hip Bone Mineral Density}
\subsection{26w BMD and Baseline Predictors}
In this section I model 26w interpolated BMD using baseline BMD (all four measures, not just the target measure), baseline weight, age,sex, race, \$t\$-score stratum, and treatment. I first fit a saturatedmodel with respect to all of the continuous predictors, using restricted cubic splines with 5 knots, then potentially use partial \$\chi^2\$ (blinded to nonlinearity components) to reassign degrees of freedom.
\begin{kframe}
\begin{alltt}
\hlstd{f} \hlkwb{<-} \hlkwd{ols}\hlstd{(hip26} \hlopt{~} \hlstd{sex} \hlopt{*} \hlkwd{rcs}\hlstd{(hip0,} \hlnum{5}\hlstd{)} \hlopt{+} \hlkwd{rcs}\hlstd{(lumbar0,} \hlnum{5}\hlstd{)} \hlopt{+} \hlkwd{rcs}\hlstd{(femur0,} \hlnum{5}\hlstd{)} \hlopt{+} \hlkwd{rcs}\hlstd{(forearm0,}
\hlnum{5}\hlstd{)} \hlopt{+} \hlstd{sex} \hlopt{*} \hlkwd{rcs}\hlstd{(wt0,} \hlnum{5}\hlstd{)} \hlopt{+} \hlstd{trtp} \hlopt{+} \hlkwd{rcs}\hlstd{(age,} \hlnum{5}\hlstd{)} \hlopt{+} \hlstd{race} \hlopt{+} \hlstd{sex} \hlopt{+} \hlstd{blppar} \hlopt{+} \hlstd{bltscgrp,}
\hlkwc{data} \hlstd{= d)}
\hlkwd{print}\hlstd{(f,} \hlkwc{coefs} \hlstd{=} \hlnum{FALSE}\hlstd{,} \hlkwc{latex} \hlstd{=} \hlnum{TRUE}\hlstd{)}
\hlkwd{lan}\hlstd{(f)}
\end{alltt}
\end{kframe}
The global test for nonlinearity is nowhere close to being significant, so all nonlinear effects will be dropped.
\section{Analysis of Lumbar Bone Mineral Density}
\subsection{26w BMD and Baseline Predictors}
In this section I model 26w interpolated BMD using baseline BMD (all four measures, not just the target measure), baseline weight, age,sex, race, \$t\$-score stratum, and treatment. I first fit a saturatedmodel with respect to all of the continuous predictors, using restricted cubic splines with 5 knots, then potentially use partial \$\chi^2\$ (blinded to nonlinearity components) to reassign degrees of freedom.
\begin{kframe}
\begin{alltt}
\hlstd{f} \hlkwb{<-} \hlkwd{ols}\hlstd{(lumbar26} \hlopt{~} \hlstd{sex} \hlopt{*} \hlkwd{rcs}\hlstd{(lumbar0,} \hlnum{5}\hlstd{)} \hlopt{+} \hlkwd{rcs}\hlstd{(hip0,} \hlnum{5}\hlstd{)} \hlopt{+} \hlkwd{rcs}\hlstd{(femur0,} \hlnum{5}\hlstd{)} \hlopt{+}
\hlkwd{rcs}\hlstd{(forearm0,} \hlnum{5}\hlstd{)} \hlopt{+} \hlstd{sex} \hlopt{*} \hlkwd{rcs}\hlstd{(wt0,} \hlnum{5}\hlstd{)} \hlopt{+} \hlstd{trtp} \hlopt{+} \hlkwd{rcs}\hlstd{(age,} \hlnum{5}\hlstd{)} \hlopt{+} \hlstd{race} \hlopt{+} \hlstd{sex} \hlopt{+}
\hlstd{blppar} \hlopt{+} \hlstd{bltscgrp,} \hlkwc{data} \hlstd{= d)}
\hlkwd{print}\hlstd{(f,} \hlkwc{coefs} \hlstd{=} \hlnum{FALSE}\hlstd{,} \hlkwc{latex} \hlstd{=} \hlnum{TRUE}\hlstd{)}
\hlkwd{lan}\hlstd{(f)}
\end{alltt}
\end{kframe}
The global test for nonlinearity is nowhere close to being significant, so all nonlinear effects will be dropped.
\section{Analysis of Femur Bone Mineral Density}
\subsection{26w BMD and Baseline Predictors}
In this section I model 26w interpolated BMD using baseline BMD (all four measures, not just the target measure), baseline weight, age,sex, race, \$t\$-score stratum, and treatment. I first fit a saturatedmodel with respect to all of the continuous predictors, using restricted cubic splines with 5 knots, then potentially use partial \$\chi^2\$ (blinded to nonlinearity components) to reassign degrees of freedom.
\begin{kframe}
\begin{alltt}
\hlstd{f} \hlkwb{<-} \hlkwd{ols}\hlstd{(femur26} \hlopt{~} \hlstd{sex} \hlopt{*} \hlkwd{rcs}\hlstd{(femur0,} \hlnum{5}\hlstd{)} \hlopt{+} \hlkwd{rcs}\hlstd{(hip0,} \hlnum{5}\hlstd{)} \hlopt{+} \hlkwd{rcs}\hlstd{(lumbar0,} \hlnum{5}\hlstd{)} \hlopt{+} \hlkwd{rcs}\hlstd{(forearm0,}
\hlnum{5}\hlstd{)} \hlopt{+} \hlstd{sex} \hlopt{*} \hlkwd{rcs}\hlstd{(wt0,} \hlnum{5}\hlstd{)} \hlopt{+} \hlstd{trtp} \hlopt{+} \hlkwd{rcs}\hlstd{(age,} \hlnum{5}\hlstd{)} \hlopt{+} \hlstd{race} \hlopt{+} \hlstd{sex} \hlopt{+} \hlstd{blppar} \hlopt{+} \hlstd{bltscgrp,}
\hlkwc{data} \hlstd{= d)}
\hlkwd{print}\hlstd{(f,} \hlkwc{coefs} \hlstd{=} \hlnum{FALSE}\hlstd{,} \hlkwc{latex} \hlstd{=} \hlnum{TRUE}\hlstd{)}
\hlkwd{lan}\hlstd{(f)}
\end{alltt}
\end{kframe}
The global test for nonlinearity is nowhere close to being significant, so all nonlinear effects will be dropped.
\section{Analysis of Forearm Bone Mineral Density}
\subsection{26w BMD and Baseline Predictors}
In this section I model 26w interpolated BMD using baseline BMD (all four measures, not just the target measure), baseline weight, age,sex, race, \$t\$-score stratum, and treatment. I first fit a saturatedmodel with respect to all of the continuous predictors, using restricted cubic splines with 5 knots, then potentially use partial \$\chi^2\$ (blinded to nonlinearity components) to reassign degrees of freedom.
\begin{kframe}
\begin{alltt}
\hlstd{f} \hlkwb{<-} \hlkwd{ols}\hlstd{(forearm26} \hlopt{~} \hlstd{sex} \hlopt{*} \hlkwd{rcs}\hlstd{(forearm0,} \hlnum{5}\hlstd{)} \hlopt{+} \hlkwd{rcs}\hlstd{(hip0,} \hlnum{5}\hlstd{)} \hlopt{+} \hlkwd{rcs}\hlstd{(lumbar0,} \hlnum{5}\hlstd{)} \hlopt{+}
\hlkwd{rcs}\hlstd{(femur0,} \hlnum{5}\hlstd{)} \hlopt{+} \hlstd{sex} \hlopt{*} \hlkwd{rcs}\hlstd{(wt0,} \hlnum{5}\hlstd{)} \hlopt{+} \hlstd{trtp} \hlopt{+} \hlkwd{rcs}\hlstd{(age,} \hlnum{5}\hlstd{)} \hlopt{+} \hlstd{race} \hlopt{+} \hlstd{sex} \hlopt{+} \hlstd{blppar} \hlopt{+}
\hlstd{bltscgrp,} \hlkwc{data} \hlstd{= d)}
\hlkwd{print}\hlstd{(f,} \hlkwc{coefs} \hlstd{=} \hlnum{FALSE}\hlstd{,} \hlkwc{latex} \hlstd{=} \hlnum{TRUE}\hlstd{)}
\hlkwd{lan}\hlstd{(f)}
\end{alltt}
\end{kframe}
The global test for nonlinearity is nowhere close to being significant, so all nonlinear effects will be dropped.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment