Using knitr to create complex dynamic documents

The following is a minimal reproducible example (RE) - my attempt is to find out how I can use knitr to create complex dynamic documents, where here β€œcomplex” refers not to the elements of the document and their layout, but to the non-linear logic of the basic fragments of R. Although provided by RE and its results show that a solution based on this approach may work well, I would like to know : 1) this is the correct approach to using knitr for such situations; 2) are there any optimizations that can be made to improve the approach; 3) what are the alternative approaches that can reduce the granularity of code fragments.

Download source package EDA (file "reEDA.R"):

 ## @knitr CleanEnv rm(list = ls(all.names = TRUE)) ## @knitr LoadPackages library(psych) library(ggplot2) ## @knitr PrepareData set.seed(100) # for reproducibility data(diamonds, package='ggplot2') # use built-in data ## @knitr PerformEDA generatePlot <- function (df, colName) { df <- df df$var <- df[[colName]] g <- ggplot(data.frame(df)) + scale_fill_continuous("Density", low="#56B1F7", high="#132B43") + scale_x_log10("Diamond Price [log10]") + scale_y_continuous("Density") + geom_histogram(aes(x = var, y = ..density.., fill = ..density..), binwidth = 0.01) return (g) } performEDA <- function (data) { d_var <- paste0("d_", deparse(substitute(data))) assign(d_var, describe(data), envir = .GlobalEnv) for (colName in names(data)) { if (is.numeric(data[[colName]]) || is.factor(data[[colName]])) { t_var <- paste0("t_", colName) assign(t_var, summary(data[[colName]]), envir = .GlobalEnv) g_var <- paste0("g_", colName) assign(g_var, generatePlot(data, colName), envir = .GlobalEnv) } } } performEDA(diamonds) 

EDA R Report Markdown Document (file "reEDA.Rmd"):

 ```{r KnitrSetup, echo=FALSE, include=FALSE} library(knitr) opts_knit$set(progress = TRUE, verbose = TRUE) opts_chunk$set( echo = FALSE, include = FALSE, tidy = FALSE, warning = FALSE, comment=NA ) ``` ```{r ReadChunksEDA, cache=FALSE} read_chunk('reEDA.R') ``` ```{r CleanEnv} ``` ```{r LoadPackages} ``` ```{r PrepareData} ``` Narrative: Data description ```{r PerformEDA} ``` Narrative: Intro to EDA results Let look at summary descriptive statistics for our dataset ```{r DescriptiveDataset, include=TRUE} print(d_diamonds) ``` Now, let examine each variable of interest individually. Varible Price is ... Decriptive statistics for 'Price': ```{r DescriptivePrice, include=TRUE} print(t_price) ``` Finally, let examine price distribution across the dataset visually: ```{r VisualPrice, include=TRUE, fig.align='center'} print(g_price) ``` 

The result can be found here:

http://rpubs.com/abrpubs/eda1

+2
r reproducible-research knitr modularity r-markdown
source share
1 answer

I do not understand what nonlinearity is in this code; perhaps because the example (thanks for that, by the way) is small enough to demonstrate the code, but not large enough to demonstrate its concern.

In particular, I do not understand the reason for the performEDA function. Why not include this functionality in markdowns? It would seem easier and more understandable to read. (This is not verified ...)

 Let look at summary descriptive statistics for our dataset ```{r DescriptiveDataset, include=TRUE} print(describe(diamonds)) ``` Now, let examine each variable of interest individually. Varible Price is ... Decriptive statistics for 'Price': ```{r DescriptivePrice, include=TRUE} print(summary(data[["Price"]])) ``` Finally, let examine price distribution across the dataset visually: ```{r VisualPrice, include=TRUE, fig.align='center'} print(generatePlot(data, "Price")) ``` 

It looks like you were about to show graphs for all variables; are you perhaps looking for a loop there?

In addition, this would not change the functionality, but it would be much more within R's idiom to have performEDA to return the list with the information it created, rather than assign it to the global environment. It took me a while to figure out what the code did, since these new variables were not defined by anything.

+2
source share

All Articles