有条件地在rmarkDown中包含带有R对象的HTML表?

发布于 2025-02-06 22:06:11 字数 1693 浏览 2 评论 0原文

我正在研究一个自动生成一系列动态报告,并使用RmarkDown生成一系列动态报告。大多数报告都包含一个关于单个问题的表,尽管有几种案例有多个问题(请参见下文)。由于报告是动态生成的,因此HTML表的内容取决于R对象。

我如何设置我的rmarkDown html以有条件地评估并显示第二个表(通过诸如“ eval = has_two_qs”之类的参数或其他内容)?我已经尝试使用“ ASIS”或“ ECHO”更改块选项,但是它并没有触发R对象(例如,从字面上显示了R q2_text - 不是在该中指定的实际Q2_TEXT参数)。我还尝试通过在DIV内部包含数据存储来直接显示HTML表的部分,但这也不是我想要的地方。

示例(按预期工作,所有报告均具有Q1):

<table border=1 frame=hsides rules=rows>  
  <tr>
    <td>Q1</td>
    <td>`r q1_text`</td>
  </tr>
  <tr>
    <td>Q1 key</td>
    <td>`r q1_answers`</td>
  </tr>
  <tr>
    <td> Q1 percent correct</td>
    <td>`r q1_pct_correct`</td>
  </tr>
</table>

我很难遇到的部分(引号中的选项失败,因为我不知道如何通过Markdown在此处显示它们):

# ```(asis, echo = has_two_qs) # 
<table border=1 frame=hsides rules=rows>  
  <tr>
    <td>Q2</td>
    <td>`r q2_text`</td>
  </tr>
  <tr>
    <td>Q2 key</td>
    <td>`r q2_answers`</td>
  </tr>
  <tr>
    <td>Q2 percent correct</td>
    <td>`r q2_pct_correct`</td>
  </tr>
</table>

也不起作用:

<div data-show-if="has_two_qs">
<table border=1 frame=hsides rules=rows>  
  <tr>
    <td>Q2</td>
    <td>`r q2_text`</td>
  </tr>
  <tr>
    <td>Q2 key</td>
    <td>`r q2_answers`</td>
  </tr>
  <tr>
    <td> Q2 percent correct</td>
    <td>`r q2_pct_correct`</td>
  </tr>
</table>
</div>

I'm working on a automatically generating a series of dynamic reports with Rmarkdown. The majority of the reports include a table on a single question, although there are several cases with multiple questions (see below). Because the reports are dynamically generated, the content for the html table depends on an r object.

How can I set up my rmarkdown html to conditionally evaluate and show the second table when appropriate (via an argument like "eval = has_two_qs" or something)? I've tried changing the chunk options using "asis" or "echo", but then it doesn't trigger the r objects (eg r q2_text is shown literally - not the actual q2_text as specified in the params). I've also tried directly showing sections of the html table by including data-show-if inside of a div, but that's not getting me where I want either.

Example (works as expected, all reports have Q1):

<table border=1 frame=hsides rules=rows>  
  <tr>
    <td>Q1</td>
    <td>`r q1_text`</td>
  </tr>
  <tr>
    <td>Q1 key</td>
    <td>`r q1_answers`</td>
  </tr>
  <tr>
    <td> Q1 percent correct</td>
    <td>`r q1_pct_correct`</td>
  </tr>
</table>

The part I'm having difficulty with (with failed options in quotes, as I don't know how to get them to show via markdown here):

# ```(asis, echo = has_two_qs) # 
<table border=1 frame=hsides rules=rows>  
  <tr>
    <td>Q2</td>
    <td>`r q2_text`</td>
  </tr>
  <tr>
    <td>Q2 key</td>
    <td>`r q2_answers`</td>
  </tr>
  <tr>
    <td>Q2 percent correct</td>
    <td>`r q2_pct_correct`</td>
  </tr>
</table>

Also doesn't work:

<div data-show-if="has_two_qs">
<table border=1 frame=hsides rules=rows>  
  <tr>
    <td>Q2</td>
    <td>`r q2_text`</td>
  </tr>
  <tr>
    <td>Q2 key</td>
    <td>`r q2_answers`</td>
  </tr>
  <tr>
    <td> Q2 percent correct</td>
    <td>`r q2_pct_correct`</td>
  </tr>
</table>
</div>

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

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

发布评论

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

评论(2

情仇皆在手 2025-02-13 22:06:11

使用@Guasi建议的一些解决方案,我能够通过使用HTMLTools软件包的HTML函数将一个不高的但可行的解决方案拼凑在一起:

{r, eval = has_two_qs, echo = FALSE}

tq2_1 <- "<table frame=hsides rules=rows>
  <tr>
    <td>SAQ Q2</td>
    <td>"
  
tq2_2 <- "</td>
  </tr>
  </table>"

然后:然后:

{r, eval = has_two_qs, echo=FALSE}
library(htmltools)
library(knitr)

HTML(tq2_1)
asis_output(saq2_text)
HTML(tq2_2)

Using some of the solution suggested by @guasi, I was able to cobble together an inelegant, but workable solution by using the HTML function from the htmltools package:

{r, eval = has_two_qs, echo = FALSE}

tq2_1 <- "<table frame=hsides rules=rows>
  <tr>
    <td>SAQ Q2</td>
    <td>"
  
tq2_2 <- "</td>
  </tr>
  </table>"

And then:

{r, eval = has_two_qs, echo=FALSE}
library(htmltools)
library(knitr)

HTML(tq2_1)
asis_output(saq2_text)
HTML(tq2_2)

坏尐絯 2025-02-13 22:06:11

该解决方案需要htmltools软件包。如果您可以将表的输出存储在变量中,则可以具有打印输出的IF语句。

{r echo=FALSE}
library(htmltools)
table_q2 = NULL
table_q2 <- "<table border=1 frame=hsides rules=rows>  
  <tr>
    <td>Q2</td>
    <td>`r q2_text`</td>
  </tr>
  <tr>
    <td>Q2 key</td>
    <td>`r q2_answers`</td>
  </tr>
  <tr>
    <td> Q2 percent correct</td>
    <td>`r q2_pct_correct`</td>
  </tr>
</table>"
{r echo=FALSE}
if (!is.null(table_q2)) HTML(table_q2)

在此示例中,您必须在没有table> table_q2输出时将table_q2初始化为null,否则请投诉。

This solution requires the htmltools package. If you can store the output of the table in a variable, then you can have an if statement that prints the output.

{r echo=FALSE}
library(htmltools)
table_q2 = NULL
table_q2 <- "<table border=1 frame=hsides rules=rows>  
  <tr>
    <td>Q2</td>
    <td>`r q2_text`</td>
  </tr>
  <tr>
    <td>Q2 key</td>
    <td>`r q2_answers`</td>
  </tr>
  <tr>
    <td> Q2 percent correct</td>
    <td>`r q2_pct_correct`</td>
  </tr>
</table>"
{r echo=FALSE}
if (!is.null(table_q2)) HTML(table_q2)

In this example, you have to initialize table_q2 to NULL when there is no table_q2 output, otherwise rmarkdown complains.

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