我可以动态创建 r markdown 文档吗?
Can i create rmarkdown documents dynamicaly?
我想创建一个根据用户输入而不同的 rmarkdown 模板。在我的例子中,一个文档描述了你的数据集。如果某人有 20 个变量,则应该有 20 个标题包含这些变量的名称。然后它打开该模板以允许用户添加更多信息。
组合使用 sink() 是否可行?
更新一个更有效的例子
我的想法是有一组函数对每个变量执行一些汇总,
f.i。对于数字变量均值、中位数等,对于因子变量,因子概述。我可以写所有这些功能,我可以制作一个 rmarkdown 文档,但我真正想要的是这样的东西。
数据集一
letters numbers factors
a 10 orange
b 3 green
c 6 verydarkblue
带有 rmarkdown 文件
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]
而且如果变量个数不同,模板也会不同
我可能会从 knitr
包开始,它已经完成了您想要的一些功能。然后你创建一个输出类型设置为 "asis"
的代码块,这样告诉 knitr
将结果直接放在输出文件中而不添加任何标记,然后你的代码可以插入适当的标记,只需循环通过数据集,为每一列输出### columnname
,然后是相应的汇总信息。
代码块可能类似于:
```{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')
}
}
```
我想创建一个根据用户输入而不同的 rmarkdown 模板。在我的例子中,一个文档描述了你的数据集。如果某人有 20 个变量,则应该有 20 个标题包含这些变量的名称。然后它打开该模板以允许用户添加更多信息。
组合使用 sink() 是否可行?
更新一个更有效的例子
我的想法是有一组函数对每个变量执行一些汇总, f.i。对于数字变量均值、中位数等,对于因子变量,因子概述。我可以写所有这些功能,我可以制作一个 rmarkdown 文档,但我真正想要的是这样的东西。
数据集一
letters numbers factors
a 10 orange
b 3 green
c 6 verydarkblue
带有 rmarkdown 文件
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]
而且如果变量个数不同,模板也会不同
我可能会从 knitr
包开始,它已经完成了您想要的一些功能。然后你创建一个输出类型设置为 "asis"
的代码块,这样告诉 knitr
将结果直接放在输出文件中而不添加任何标记,然后你的代码可以插入适当的标记,只需循环通过数据集,为每一列输出### columnname
,然后是相应的汇总信息。
代码块可能类似于:
```{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')
}
}
```