有条件地在rmarkDown中包含带有R对象的HTML表?
我正在研究一个自动生成一系列动态报告,并使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用@Guasi建议的一些解决方案,我能够通过使用HTMLTools软件包的HTML函数将一个不高的但可行的解决方案拼凑在一起:
然后:然后:
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:
And then:
该解决方案需要
htmltools
软件包。如果您可以将表的输出存储在变量中,则可以具有打印输出的IF语句。在此示例中,您必须在没有
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.In this example, you have to initialize
table_q2
to NULL when there is notable_q2
output, otherwise rmarkdown complains.