如何有条件地夸大/包括一系列本身包含内联代码的文本?

发布于 2025-02-01 04:38:57 字数 843 浏览 3 评论 0原文

我正在尝试创建一个R-Markdown文档的目的是自动报告我们学校中的笔试结果。它包括结果的两个数据可视化(参数化以考虑不同受试者和考试类型之间的差异)。

我的代码中有一个部分的目的是确定结果来自多少个单独的类

length(levels(data$class))

,并自动执行独立样本t检验或ANOVA来测试类之间的差异,将整个考试得分作为依赖多变的。

代码的这一部分正常。但是,随后是一段实际的文本,我也想参数化以自动报告所执行的任何测试的结果。它使用文本中的参数,如以下内容:

The results of the `r ifelse(length(levels(fulldata$Osztály)) > 2, "One-way ANOVA", " t-test")` were `r ifelse(p_val > 0.05, "non-significant", "significant")`

这可以正常工作,但是,我不确定如何处理所有结果来自单个类的情况。 P_VAL是在负责选择要执行测试的IF statement内部创建的全局变量,如果结果来自单个类,则它通过逻辑(由于未执行测试)而落下,并且没有生成P值,因此当它到达参数化文本时,r会引发错误。

是否可以在if statement上显示出实际的R-MAR​​KDOWN文本? 整个代码

print(cat(.......))

我使用使用白色边框的HTML文档中显示了 ,但看起来很糟糕,并且与文档的其余部分不一致。我基本上正在寻找诸如eval =长度(级别(class))&gt之类的东西。 2,仅适用于R-MAR​​KDOWN文件中的文本块。这可以做吗?

I am trying to create an R-markdown document the aim of which is to automate reporting the written exam results in our school. It includes both data visualization for the results (parametrized to take into account the differences between different subjects and exam types).

There is a section in my code the aim of which is to determine how many separate classes the results come from

length(levels(data$class))

and automatically perform either an independent samples t-test or an ANOVA to test for differences between classes, using the overall exam score as the dependent variable.

This part of the code works fine. However, it is followed by a stretch of actual text that I would also like to parametrize to automatically report the results of whatever test has been performed. It uses parameters in the text, like the following:

The results of the `r ifelse(length(levels(fulldata$Osztály)) > 2, "One-way ANOVA", " t-test")` were `r ifelse(p_val > 0.05, "non-significant", "significant")`

This works fine, however, I'm not sure how to handle the case where all results come from a single class. p_val is a global variable created inside the if-statement responsible for choosing the test to perform, and if the results come from a single class, it falls through the logic (since no test is performed) and no p-value is generated, so R throws an error when it gets to the parametrized stretch of text.

Is it possible to make displaying the actual R-Markdown text conditional on an if-statement? I rewrote the entire code using

print(cat(.......))

but it is displayed in the HTML document with a white border, which looks bad, and is inconsistent with the rest of the document. I'm basically looking for something like eval = length(levels(Class)) > 2, only for the text block inside the R-Markdown file. Is this possible to do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷默言语 2025-02-08 04:38:57

我将在R块中创建您的文本,然后在级别上更改IT。在块中:

f <- factor(c("a", "a", "a")) # replace with your factor variable 
sentence <- ""
if (length(levels(f)) > 1) {
  p_val <- .01 # and your p-value
  test <- ifelse(length(levels(f)) > 2, "One-way ANOVA", " t-test")
  test_result <- ifelse(p_val > 0.05, "non-significant", "significant")
  sentence <- paste0("The results of the ", test, " were ", test_result, ". ")
}

然后在文本中:

Here are the first results. `r sentence`The other results follow.

将打印,具体取决于级别:

这是第一个结果。其他结果如下。

这是第一个结果。 t检验的结果很重要。其他结果如下。

这是第一个结果。单向方差分析的结果是
重要的。其他结果如下。

I'd create your text in an R chunk and change it conditional on the levels. In the chunk:

f <- factor(c("a", "a", "a")) # replace with your factor variable 
sentence <- ""
if (length(levels(f)) > 1) {
  p_val <- .01 # and your p-value
  test <- ifelse(length(levels(f)) > 2, "One-way ANOVA", " t-test")
  test_result <- ifelse(p_val > 0.05, "non-significant", "significant")
  sentence <- paste0("The results of the ", test, " were ", test_result, ". ")
}

Then in the text:

Here are the first results. `r sentence`The other results follow.

Which will print, depending on the levels:

Here are the first results. The other results follow.

Here are the first results. The results of the t-test were significant. The other results follow.

Here are the first results. The results of the One-way ANOVA were
significant. The other results follow.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文