R Markdown将函数嵌入到一个简单的表格中 [英] R Markdown embedding a function into a simple table

查看:156
本文介绍了R Markdown将函数嵌入到一个简单的表格中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 R 的新手,我非常喜欢 R Markdown的灵活性来创建报表。



我的问题是我想用随机数生成函数创建我的表。我希望简单的表包含字符串标题和以下函数:

 > (x,数字= 2)}的函数,运算< -function(x){
+ x< -runif(n = 1,min = 13.5,max = 15.5)

它不允许我使用这种方法创建表?

 ```{r} 
字符串|字符串|字符串
------- | ------ | ------
ran(x)| ran(x)| ran(x)
```

我的最终目标是用简单的统计数据创建实践工作表,这些统计数据会有所不同,但在有限的整数范围内 - 所以我可以通过关于平均数,中位数等的一些想法来提出后续问题。 。



任何帮助都将不胜感激。

这种方法可以自动生成大部分报告,并减少您需要输入的代码量。对于初学者,您可以将其变成



如果您想为 x 和/或 n 使用不同的值,请打开一个单独的R脚本文件并键入如下所示:

  rmarkdown :: render(Worksheet.Rmd,
params = list(x = c( 2,4,6,8),
n = 5),
output_file =Worksheet.html)

在上面的代码中, render 函数编译我们刚刚创建的rmarkdown文档,但使用新的 x n 值,并将输出保存到名为 Worksheet.html 的文件中。 (我假设我们已经将rmarkdown文档保存到一个名为 Worksheet.Rmd 的文件中。)以下是输出内容:





当然,您也可以添加参数, runif 函数,而不是将它们硬编码为13.5和15.5。



如果要创建多个工作表,每个都有不同的 x 值,您可以将 render 放入一个循环中:



pre $ df = expand.grid(1:3,5:6,10:11)

for(i in 1:nrow(df )){
rmarkdown :: render(Worksheet.Rmd,
params = list(x = unlist(df [i,]),n = 10),
output_file = paste0(粘贴(unlist(df [i,]),collapse =_),。html))
}


I'm new to R and I'm really liking the flexibility of R Markdown to create reports.

My problem is that I want to use a random number generating function I've created my tables. I want simple tables to include string headers and the following function:

> ran<-function(x){
+     x<-runif(n=1, min=13.5,max=15.5)
+     round(x, digits=2)}.

It won't allow me to create a table using this method?

```{r}
String   |String   |String
-------|------|------
ran(x)|ran(x)|ran(x)
```

My ultimate goal is to create practice worksheets with simple statistics that will be different but within a bounded integer range - so I can ask follow-up questions with some idea of the mean, median etc.

Any help would be greatly appreciated.

解决方案

Here is an approach that automates much of the report generation and reduces the amount of code you need to type. For starters, you can turn this into a parameterized report, which would make it easier to create worksheets using different values of x. Here's an example:

In your rmarkdown document you would declare parameters x and n in the yaml header. n is the number of random values you want to produce for each value of x. The x and n values in the yaml header are just the defaults knitr uses if no other values are input when you render the report:

---
output: html_document
params: 
  x: !r c(1,5,10)
  n: 10
---

Then, in the same rmarkdown document you would have the text and code for your worksheet. You access the parameters x and n with params$x and params$n, respectively.

For example, the rest of the rmarkdown document could look like the code below. We put x into a list called x_vals with named elements, so that the resulting column names in the output will be the names of the list elements. We feed that list to sapply to get a column of n random values for each value of x. The whole sapply statement is wrapped in kable, which produces a table in rmarkdown format.

```{r, include=FALSE}
library(knitr)
```

```{r, echo=FALSE}
# Create a named list of the x values that we passed into this document
x_vals = as.list(setNames(params$x, paste0("x=", params$x)))

kable(sapply(x_vals, function(i) round(runif(params$n, 13.5, 15.5) + i, 2)))
```

You can now click the "knit" button and it will produce a table using the default parameter values:

If instead you want to use different values for x and/or n, open a separate R script file and type the following:

rmarkdown::render("Worksheet.Rmd", 
                  params = list(x = c(2,4,6,8), 
                                n = 5),
                  output_file="Worksheet.html")

In the code above, the render function compiles the rmarkdown document we just created, but with new x and n values, and saves the output to a file called Worksheet.html. (I've assumed that we've saved the rmarkdown document to a file called Worksheet.Rmd.) Here's what the output looks like:

You can also, of course, add parameters for the lower and upper limits of the runif function, rather than hard-coding them as 13.5 and 15.5.

If you want to create several worksheets, each with different x values, you can put render in a loop:

df = expand.grid(1:3,5:6,10:11)

for (i in 1:nrow(df)) {
  rmarkdown::render("Worksheet.Rmd", 
                    params = list(x=unlist(df[i,]), n=10),
                    output_file=paste0(paste(unlist(df[i,]),collapse="_"),".html"))
}

这篇关于R Markdown将函数嵌入到一个简单的表格中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆