从函数内返回文本和内联 r 降价 [英] Return both text and inline r markdown from within a function

查看:44
本文介绍了从函数内返回文本和内联 r 降价的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用途:

install.packages(bookdown")图书馆(预订)

GitHub:https://github.com/MartinJLambert/r-markdown_function_test

鉴于我需要在同一文档中多次重现这些值,我创建了一个函数来计算简单的 ANOVA 并确定 F、df、p 和 n 统计量,以及一个基于星号的显着性指标p 值.

---输出:bookdown::pdf_document2---```{r 包含 = FALSE}# 用于计算和显示 ANOVA 统计结果的函数func_aov_stats <- 函数(input_df, input_var, input_factor) {aov_tmp <- aov(input_var ~ input_factor, input_df)anova_tmp <- anova(aov_tmp)temp_signif <- if(anova_tmp[1,5] < 0.001){print(***")}否则 if(anova_tmp[1,5] <0.01){print(**")}否则 if(anova_tmp[1,5] <0.05){print(*")}否则{打印(")}粘贴(anova_tmp[1,1],anova_tmp[1,4],anova_tmp[1,5],temp_signif,anova_tmp[2,1]+2)}```r func_aov_stats(mtcars, mtcars$mpg, mtcars$cyl)`

这很简单,编织它正是我想要它做的.

<块引用>

1 79.5610275293349 6.11268714258098e-10 *** 32

但是,单单数字是没有用的,所以我想将其报告为一串文本.一些类似的东西:

<块引用>

方差分析:F(df=anova_tmp[1,1]) = anova_tmp[1,4], p= anova_tmp[1,5] temp_signif, n = anova_tmp[2,1]+2

我想简单地将内联 r-markdown 粘贴到函数中:

paste("ANOVA: F~(df=`r anova_tmp[1,1]`)~ = `r anova_tmp[1,4]`, p = `r paste(anova_tmp[1,5]] temp_signif)`, n = `r anova_tmp[2,1]+2`")

但我明白了:

<块引用>

方差分析:F(df=r anova_tmp[1,1]) = r anova_tmp[1,4], p = r anova_tmp[1,5] temp_signif, n = r anova_tmp[2,1]+2

至少 Markdown 格式有效,但它显然没有像希望的那样粘贴 'r' 组件.

作用是,如果我在函数之外手动写出,在markdown文档的其他地方:

```{r outside_of_function, include= FALSE}aov_tmp <- aov(mpg ~ cyl, mtcars)anova_tmp <- anova(aov_tmp)temp_signif <- if(anova_tmp[1,5] < 0.001){print("***")} else if(anova_tmp[1,5] < 0.01){print("**"))} else if(anova_tmp[1,5] <0.05){print("*")} else {print("")}``方差分析:F~(df=`r anova_tmp[1,1]`)~ = `r anova_tmp[1,4]`, p = `r paste(anova_tmp[1,5], temp_signif)`, n = `ranova_tmp[2,1]+2`

<块引用>

方差分析:F(df=1) = 79.5610275,p = 6.11268714258098e-10 ***,n = 32

所以问题出在函数本身.虽然它似乎确实能够生成格式,但 'r' 代码的计算似乎需要一些超出我理解的东西.

解决方案

解决方案是:paste0().

完整代码、更新且功能齐全:

---输出:bookdown::pdf_document2---```{r 包含 = FALSE}# 用于计算和显示 ANOVA 统计结果的函数func_aov_stats <- 函数(input_df, input_var, input_factor) {aov_tmp <- aov(input_var ~ input_factor, input_df)anova_tmp <- anova(aov_tmp)temp_signif <- if(anova_tmp[1,5] < 0.001){print(***")}否则 if(anova_tmp[1,5] <0.01){print(**")}否则 if(anova_tmp[1,5] <0.05){print(*")}否则{打印(")}paste0("ANOVA: F~(df=",anova_tmp[1,1],")~= ",anova_tmp[1,4],", p = ",anova_tmp[1,5]," ",temp_signif," n = ",anova_tmp[2,1]+2)}```r func_aov_stats(mtcars, mtcars$mpg, mtcars$cyl)`

Uses:

install.packages("bookdown")
library(bookdown)

GitHub: https://github.com/MartinJLambert/r-markdown_function_test

Given that I need to reproduce these values multiple times within the same document, I have created a function that calculates a simple ANOVA and determines the F, df, p and n statistics, as well as an asterix indicator for significance based on the p-value.

---
  output:
    bookdown::pdf_document2
---
```{r include= FALSE}
# function for calculating and displaying statistics results from an ANOVA
func_aov_stats <- function(input_df, input_var, input_factor) {
  aov_tmp <- aov(input_var ~ input_factor, input_df)
  anova_tmp <- anova(aov_tmp)
  
  temp_signif <- if(anova_tmp[1,5] < 0.001){print("***")}
    else if(anova_tmp[1,5] < 0.01){print("**")}
    else if(anova_tmp[1,5] <0.05){print("*")}
    else {print("")}
  
  paste(anova_tmp[1,1], anova_tmp[1,4], anova_tmp[1,5], temp_signif, anova_tmp[2,1]+2)
}
```

`r func_aov_stats(mtcars, mtcars$mpg, mtcars$cyl)`

This is simple enough and knitting this does exactly what I want it to do.

1 79.5610275293349 6.11268714258098e-10 *** 32

However, numbers alone are kinda useless, so I would like to report it as a string of text. Something along these lines:

ANOVA: F(df=anova_tmp[1,1]) = anova_tmp[1,4], p = anova_tmp[1,5] temp_signif, n = anova_tmp[2,1]+2

I was thinking of simply pasting the inline r-markdown inside the function:

paste("ANOVA: F~(df=`r anova_tmp[1,1]`)~ = `r anova_tmp[1,4]`, p = `r paste(anova_tmp[1,5] temp_signif)`, n = `r anova_tmp[2,1]+2`")

But I get this:

ANOVA: F(df=r anova_tmp[1,1]) = r anova_tmp[1,4], p = r anova_tmp[1,5] temp_signif, n = r anova_tmp[2,1]+2

At least the markdown formatting worked, but it obviously doesn't paste the 'r' components as hoped.

What does work, is if I write it out manually outside of the function, elsewhere in the markdown document:

```{r outside_of_function, include= FALSE}
aov_tmp <- aov(mpg ~ cyl, mtcars)
anova_tmp <- anova(aov_tmp)

temp_signif <- if(anova_tmp[1,5] < 0.001){print("***")} else if(anova_tmp[1,5] < 0.01){print("**")} else if(anova_tmp[1,5] <0.05){print("*")} else {print("")}
```

ANOVA: F~(df=`r anova_tmp[1,1]`)~ = `r anova_tmp[1,4]`, p = `r paste(anova_tmp[1,5], temp_signif)`, n = `r anova_tmp[2,1]+2`

ANOVA: F(df=1) = 79.5610275, p = 6.11268714258098e-10 ***, n = 32

So the issue is within the function itself. While it does seem to be able to produce the formatting, the computation of the 'r' code seems to require something beyond my understanding.

解决方案

The solution is: paste0().

Full code, updated and fully functioning:

---
  output:
    bookdown::pdf_document2
---
```{r include = FALSE}
# function for calculating and displaying statistics results from an ANOVA
func_aov_stats <- function(input_df, input_var, input_factor) {
  aov_tmp <- aov(input_var ~ input_factor, input_df)
  anova_tmp <- anova(aov_tmp)
  
  temp_signif <- if(anova_tmp[1,5] < 0.001){print("***")}
  else if(anova_tmp[1,5] < 0.01){print("**")}
  else if(anova_tmp[1,5] <0.05){print("*")}
  else {print("")}
  
  paste0("ANOVA: F~(df=",anova_tmp[1,1],")~= ",anova_tmp[1,4],", p = ",anova_tmp[1,5]," ",temp_signif," n = ",anova_tmp[2,1]+2)
}
```

`r func_aov_stats(mtcars, mtcars$mpg, mtcars$cyl)`

这篇关于从函数内返回文本和内联 r 降价的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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