2

I am attempting to create a simple data table in which the genus name in the "Coral_taxon" column is italicized while the "spp." Portion following the genus name is not capitalized. I attempted to code each row for "Coral_taxon" using the expression() function but have had no success.

sum_dat_final2 <- data.frame(Site = c("Hanauma Bay", "Hanauma Bay", "Hanauma Bay", "Waikiki", "Waikiki", "Waikiki"), 
                                 Coral_taxon = c(expression(italic("Montipora"), " spp."), expression(italic("Pocillopora"), " spp."), expression(italic("Porites"), " spp."), expression(italic("Montipora"), " spp."), expression(italic("Pocillopora"), " spp."), expression(italic("Porites"), " spp.")),
                                 Estimate = c("0.45 ± 0.13 %", "0.43 ± 0.18 %", "19.5 ± 2.6 %", "0.05 ± 0.03 %", "0.47 ± 0.3 %", "1.19 ± 0.56 %"))
Error in as.data.frame.default(x[[i]], optional = TRUE) : 
  cannot coerce class ‘"expression"’ to a data.frame

enter image description here

IRTFM
  • 258,963
  • 21
  • 364
  • 487
Eric Dilley
  • 337
  • 2
  • 10
  • you could place it inside a `list` – akrun Aug 05 '20 at 21:53
  • If you mean change "Coral_taxon = c(..." to "Coral_taxon = list(..." that yielded the same error message as listed above. What else would need to be altered for list() to work? – Eric Dilley Aug 05 '20 at 21:57
  • R dataframes are not designed to hold lists or expression vectors as columns. So replace the `data.frame` call with `list`. Also since expression vectors can be indexed your use of `c(expression( .), ...)` will result in unnecessary complexity. – IRTFM Aug 05 '20 at 21:59

2 Answers2

3

sjPlot library uses HTML tags. You can use <i>Text</i> for italicizing.

df1 <- data.frame(Site = c("Hanauma Bay", "Hanauma Bay", "Hanauma Bay", 
                           "Waikiki", "Waikiki", "Waikiki"),
                  Coral_taxon = I(list("<i>Montipora</i> spp.", 
                                       "<i>Pocillopora</i> spp.", 
                                       "<i>Porites</i> spp.", 
                                       "<i>Montipora</i> spp.", 
                                       "<i>Pocillopora</i> spp.", 
                                       "<i>Porites</i> spp.")),
                  Estimate = c("0.45 <U+623C><U+3E31> 0.13 %", "0.43 <U+623C><U+3E31> 0.18 %", 
                               "19.5 <U+623C><U+3E31> 2.6 %", "0.05 <U+623C><U+3E31> 0.03 %", 
                               "0.47 <U+623C><U+3E31> 0.3 %", "1.19 <U+623C><U+3E31> 0.56 %"))  

sjPlot::tab_df(df1)

Run the snippet to see the formatted table.

    <table style="border-collapse:collapse; border:none;">

    <tr>

    <th style="border-top: double; text-align:center; font-style:italic; font-weight:normal; padding:0.2cm; border-bottom:1px solid black; text-align:left; ">

    Site

    </th>

    <th style="border-top: double; text-align:center; font-style:italic; font-weight:normal; padding:0.2cm; border-bottom:1px solid black; ">

    Coral_taxon

    </th>

    <th style="border-top: double; text-align:center; font-style:italic; font-weight:normal; padding:0.2cm; border-bottom:1px solid black; ">

    Estimate

    </th>

    </tr>

    <tr>

    <td style=" padding:0.2cm; text-align:left; vertical-align:top; text-align:left; ">

    Hanauma Bay

    </td>

    <td style=" padding:0.2cm; text-align:left; vertical-align:top; text-align:center; ">

    <i>Montipora</i> spp.

    </td>

    <td style=" padding:0.2cm; text-align:left; vertical-align:top; text-align:center; ">

    0.45 ±0.13 %

    </td>

    </tr>

    <tr>

    <td style=" padding:0.2cm; text-align:left; vertical-align:top; text-align:left; ">

    Hanauma Bay

    </td>

    <td style=" padding:0.2cm; text-align:left; vertical-align:top; text-align:center; ">

    <i>Pocillopora</i> spp.

    </td>

    <td style=" padding:0.2cm; text-align:left; vertical-align:top; text-align:center; ">

    0.43 ±0.18 %

    </td>

    </tr>

    <tr>

    <td style=" padding:0.2cm; text-align:left; vertical-align:top; text-align:left; ">

    Hanauma Bay

    </td>

    <td style=" padding:0.2cm; text-align:left; vertical-align:top; text-align:center; ">

    <i>Porites</i> spp.

    </td>

    <td style=" padding:0.2cm; text-align:left; vertical-align:top; text-align:center; ">

    19.5 ±2.6 %

    </td>

    </tr>

    <tr>

    <td style=" padding:0.2cm; text-align:left; vertical-align:top; text-align:left; ">

    Waikiki

    </td>

    <td style=" padding:0.2cm; text-align:left; vertical-align:top; text-align:center; ">

    <i>Montipora</i> spp.

    </td>

    <td style=" padding:0.2cm; text-align:left; vertical-align:top; text-align:center; ">

    0.05 ±0.03 %

    </td>

    </tr>

    <tr>

    <td style=" padding:0.2cm; text-align:left; vertical-align:top; text-align:left; ">

    Waikiki

    </td>

    <td style=" padding:0.2cm; text-align:left; vertical-align:top; text-align:center; ">

    <i>Pocillopora</i> spp.

    </td>

    <td style=" padding:0.2cm; text-align:left; vertical-align:top; text-align:center; ">

    0.47 ±0.3 %

    </td>

    </tr>

    <tr>

    <td style=" padding:0.2cm; text-align:left; vertical-align:top; text-align:left; border-bottom: double; ">

    Waikiki

    </td>

    <td style=" padding:0.2cm; text-align:left; vertical-align:top; text-align:center; border-bottom: double; ">

    <i>Porites</i> spp.

    </td>

    <td style=" padding:0.2cm; text-align:left; vertical-align:top; text-align:center; border-bottom: double; ">

    1.19 ±0.56 %

    </td>

    </tr>

    </table>
M--
  • 25,431
  • 8
  • 61
  • 93
  • 1
    Very helpful. If you "run" the code `tab_df(df1)` at an Rstudio command line there's no need for the CSS code. It just appears in a "viewer" window pane. – IRTFM Aug 05 '20 at 22:45
  • @IRTFM I'm aware and I do get that table in the viewer; but can I post it here? I can put up a screenshot I guess but this is more sophisticated (this is actually output of `reprex::reprex`). P.S. This is HTML. – M-- Aug 05 '20 at 22:47
  • Apparently an HTML page with header and body are being displayed and that is the actual result/side-effect of the tab_df function. If you click the little icon with an arrow that has a popup highlight that says "show in new window, it will send the page to an open browser. You can then use the browser "show source" function. I'm not sure that showing all that code was terribly informative, but I did think it was fun. – IRTFM Aug 05 '20 at 22:52
0

I'm guessing you wanted the species names printed with the text spp. immediately following. So do not use an expression vector inside a list, but rather construct correct expressions for that purpose. Suggest instead using this form:

sum_dat_final2 <- list(Site = c("Hanauma Bay", "Hanauma Bay", "Hanauma Bay", "Waikiki", "Waikiki", "Waikiki"), 
                   Coral_taxon = expression( italic(Montipora)~ spp., 
                                             italic(Pocillopora)~spp., 
                                             italic(Porites)~spp., 
                                             italic(Montipora)~ spp.,  
                                             italic(Pocillopora)~spp.,  
                                             italic(Porites)~spp.),
                   Estimate = c("0.45 ± 0.13 %", "0.43 ± 0.18 %", "19.5 ± 2.6 %", "0.05 ± 0.03 %", "0.47 ± 0.3 %", "1.19 ± 0.56 %"))

Using M--'s modification this is the result of the tab_df call:

<html>
<head>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
<style>
html, body { background-color: white; }
table { border-collapse:collapse; border:none; }
caption { font-weight: bold; text-align:left; }
td {  }
.thead { border-top: double; text-align:center; font-style:italic; font-weight:normal; padding:0.2cm; }
.tdata { padding:0.2cm; text-align:left; vertical-align:top; }
.arc { background-color:#f2f2f2; }
.summary { padding-top:0.1cm; padding-bottom:0.1cm; }
.summarydata { text-align:center; }
.fixedparts { font-weight:bold; text-align:left; }
.randomparts { font-weight:bold; text-align:left; padding-top:.8em; }
.zeroparts { font-weight:bold; text-align:left; padding-top:.8em; }
.simplexparts { font-weight:bold; text-align:left; padding-top:.8em; }
.lasttablerow { border-bottom: double; }
.firsttablerow { border-bottom:1px solid black; }
.firstsumrow { border-top:1px solid; }
.labelcellborder { border-bottom:1px solid; }
.depvarhead { text-align:center; border-bottom:1px solid; font-style:italic; font-weight:normal; }
.leftalign { text-align:left; }
.centeralign { text-align:center; }
.firsttablecol { text-align:left; }
.footnote { font-style:italic; border-top:double black; text-align:right; }
.subtitle { font-weight: normal; }
.modelcolumn1 {  }
.modelcolumn2 {  }
.modelcolumn3 {  }
.modelcolumn4 {  }
.modelcolumn5 {  }
.modelcolumn6 {  }
.modelcolumn7 {  }
.col1 {  }
.col2 {  }
.col3 {  }
.col4 {  }
.col5 {  }
</style>
</head>
<body>
<table>
  <tr>
    <th class="thead firsttablerow firsttablecol col1">Site</th>
    <th class="thead firsttablerow col2">Coral_taxon</th>
    <th class="thead firsttablerow col3">Estimate</th>
  </tr>
  <tr>
    <td class="tdata firsttablecol col1">Hanauma Bay</td>
    <td class="tdata centeralign col2"><i>Montipora</i> spp.</td>
    <td class="tdata centeralign col3">0.45 ± 0.13 %</td>
</tr>
  <tr>
    <td class="tdata firsttablecol col1">Hanauma Bay</td>
    <td class="tdata centeralign col2"><i>Pocillopora</i> spp.</td>
    <td class="tdata centeralign col3">0.43 ± 0.18 %</td>
</tr>
  <tr>
    <td class="tdata firsttablecol col1">Hanauma Bay</td>
    <td class="tdata centeralign col2"><i>Porites</i> spp.</td>
    <td class="tdata centeralign col3">19.5 ± 2.6 %</td>
</tr>
  <tr>
    <td class="tdata firsttablecol col1">Waikiki</td>
    <td class="tdata centeralign col2"><i>Montipora</i> spp.</td>
    <td class="tdata centeralign col3">0.05 ± 0.03 %</td>
</tr>
  <tr>
    <td class="tdata firsttablecol col1">Waikiki</td>
    <td class="tdata centeralign col2"><i>Pocillopora</i> spp.</td>
    <td class="tdata centeralign col3">0.47 ± 0.3 %</td>
</tr>
  <tr>
    <td class="tdata firsttablecol lasttablerow col1">Waikiki</td>
    <td class="tdata centeralign lasttablerow col2"><i>Porites</i> spp.</td>
    <td class="tdata centeralign lasttablerow col3">1.19 ± 0.56 %</td>
</tr>
</table>

</body></html>
IRTFM
  • 258,963
  • 21
  • 364
  • 487