3

I am trying to make a report in a LaTeX format using R. The file format is in .Rnw. I am preparing a table using the kable and kableExtra packages but I have only one problem.

Here's the screenshot of the table I made:

table in kable

I wanted to make the two species (in rows 1 and 2 of column 1) to be in italics. How to make it?

Thanks.

Here's the data:

df1 <- data.frame(`Species Name` = c("Encrasicholina punctifer", "Stolephorus indicus", "Others", "Total"),
                  `2014` = c(171.12, 0, 0.59, 171.72),
                  `2015` = c(231.18, 3.07, 0.29, 234.55),
                  `2016` = c(412.1, 0, 0.44, 412.54),
                  Total = c(814.4, 3.07, 1.33, 818.8),
                  Percent = c(99.46, 0.38, 0.16, 100))

By the way, here's the R code:

\begin{table}[H]
\centering
<<table_2>>=
df1 %>% 
  kable(format = "latex", 
        booktabs = T) %>%
  kable_styling(latex_options = c("HOLD_position"),
                position = "center",
                font_size = 9) %>% 
  row_spec(0, bold = T) %>% 
  row_spec(4, bold = T)
@
\end{table}
Michael Harper
  • 14,721
  • 2
  • 60
  • 84
D. Braed
  • 31
  • 1
  • 3
  • Why are you using .Rnw files? These have largely been made redundant by RMarkdown .Rmd files – Michael Harper Apr 04 '18 at 10:28
  • I tried .Rmd but I have difficulty in setting two-column layout. It is easy in .Rnw (that's what I think) since I can use LaTeX template. That's why I used .Rnw to make the report since my supervisor needs a two-column layout, like in academic papers. – D. Braed Apr 04 '18 at 10:36
  • RMarkdown can be fully customised with LaTeX commands. Two columns can easily be achieved here: https://stackoverflow.com/questions/34808612/how-make-2-column-layout-in-r-markdown-when-rendering-pdf . Trust me, the benefits of RMarkdown are definitely worth it :) – Michael Harper Apr 04 '18 at 16:49

1 Answers1

1

kableExtra comes with a function cell_spec which can be used to add extra styling to the table. However, in comparison to kable_styling, this has to be used before the kable function is called. This is explained within the guidance here: https://haozhu233.github.io/kableExtra/awesome_table_in_pdf.pdf

However, if you only want to edit a few individual cells, I find it is better to just reformat the data manually. As seen in this similar question, you can change the cell name to \textit{YOUR STRING}* to make the words italics.

I wasn't able to get your Rnw file working, so here is a fully reproducible Rmarkdown .Rmd file. The only difference to the .Rnw file in principle is that you need to add a double backslash to the formatting of the LaTeX command to make sure they are read by pandoc (i.e. \\textit{}):

---
output: pdf_document
---

```{r}
df1 <- data.frame(`Species Name` = c("Encrasicholina punctifer", "\\textit{Stolephorus indicus}", "*Others*", "\\textbf{Total}"),
                  `2014` = c(171.12, 0, 0.59, 171.72),
                  `2015` = c(231.18, 3.07, 0.29, 234.55),
                  `2016` = c(412.1, 0, 0.44, 412.54),
                  Total = c(814.4, 3.07, 1.33, 818.8),
                  Percent = c(99.46, 0.38, 0.16, 100))
```


```{r}
knitr::kable(df1)
```

enter image description here

Note: I would recommend the use of RMarkdown over .Rnw. You can fully customise the LaTeX template just as you do with the Rnw file, and it is much more supported. Sweave was dropped by the author over 5 years ago: https://yihui.name/knitr/demo/sweave/

Hao
  • 7,476
  • 1
  • 38
  • 59
Michael Harper
  • 14,721
  • 2
  • 60
  • 84
  • Thank you. I will try to transfer it to .Rmd. But another question. What if the data frame is a result of data manipulation (using dplyr)? – D. Braed Apr 05 '18 at 00:24
  • You can see another similar answer here which uses a function to easily reformat the cells required: https://stackoverflow.com/a/49656650/7347699 – Michael Harper Apr 05 '18 at 08:16
  • Thanks again. I really have a difficulty in making it work in .Rmd, since I am not an expert especially in making a LaTeX template for Rmarkdown. – D. Braed Apr 06 '18 at 04:54