Reputation: 317
I would like to create a rmarkdown template that is different depending on the input of the user. In my case a document describing your dataset. If someone has 20 variables, there should be 20 headings with the name of those variables. It then opens that template to allow a user to add some more information.
I do not mean parameterized reports.
Is that possible with a combination of sink() ?
My idea is to have a set of functions that perform some summary per variable, f.i. for numeric variables mean, median etc and for factor variables, an overview of factors. I can write all those functions, and I can make a rmarkdown document, but what I would really like is something like this.
dataset one
letters numbers factors
a 10 orange
b 3 green
c 6 verydarkblue
With a rmarkdown file
This dataset has 3 variables with some properties
*information for author*
add information about where you found the data what the properties
are and add some background information to the variables.
\newpage
### letters
letters has [some code that executes summary function for letters]
### numbers
numbers has [some code for numeric variables]
And If the number of variables are different, the template would be different
Upvotes: 0
Views: 69
Reputation: 49660
I would probably start with the knitr
package, which will already do some of what you want. Then you create a code block with the output type set to "asis"
so that tells knitr
to put the results directly in the output file without adding any markup, then your code can insert the appropriate markup, just loop through the data set and output ### columnname
for each column, then the appropriate summary information.
The code block might look something like:
```{r, results="asis"}
for( i in seq_len(ncol(dataset)) ) {
cat("### ", names(dataset)[i], "\n\n")
if(class(dataset[[i]]) == 'numeric') {
cat(mean(dataset[[i]]),'\n')
} else if(class(dataset[[i]])=='factor') {
print(table(datset[[i]]))
} else {
cat('Something Else\n\n')
}
}
```
Upvotes: 1