如何有条件地夸大/包括一系列本身包含内联代码的文本?
我正在尝试创建一个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-MARKDOWN文本? 整个代码
print(cat(.......))
我使用使用白色边框的HTML文档中显示了 ,但看起来很糟糕,并且与文档的其余部分不一致。我基本上正在寻找诸如eval =长度(级别(class))&gt之类的东西。 2,仅适用于R-MARKDOWN文件中的文本块。这可以做吗?
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我将在R块中创建您的文本,然后在级别上更改IT。在块中:
然后在文本中:
将打印,具体取决于级别:
I'd create your text in an R chunk and change it conditional on the levels. In the chunk:
Then in the text:
Which will print, depending on the levels: