CRunyon
CRunyon

Reputation: 93

Conditionally include html table with r objects in rmarkdown?

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>

Upvotes: 0

Views: 228

Answers (2)

guasi
guasi

Reputation: 1769

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.

Upvotes: 0

CRunyon
CRunyon

Reputation: 93

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)

Upvotes: 0

Related Questions