Style a kable table knitted to pdf - r

I'm using kable to output a table from a data.frame in a R markdown document that is parsed to pdf.
This is the output:
I would like to style the table. Specifically I'd like to:
Increase cell height. The padding argument passed to kable() function didn't have effect.
Make the headings bold. (No idea about this).
I call kaggle() in a function that is then called into the chunk in the .Rmd file.
Thanks for your help

I don't know whether you tried already, but the kableExtra package offers a lot more features than kable. You could simply pipe your kable call into row specifications with
%>% row_spec (0, bold = T)
I couldn't find an option for cell height there. In case you'd generally want to change it you could either pass it as an option into the YAML header or change the default.tex file.
Regards

Related

How can I fix knit tables in RMarkdown?

I have an RMarkdown document that has some data wrangling parts within it. I can see all tables calculated as they should look in RStudio once computed. However, once I knit to HTML, I can see all the outputs, however some tables look very messy. Does anyone have any idea how to fix this? Refer to below picture.
Why do the two columns below not appear next to the other ones in one line?
You could try a few things, starting with round down some of the decimals so everything fits.
If you need the decimals you could try printing it with knitr::kable(table). This will help with the tables looking messy.
For even more control there is a library kableExtra that has a function kable_styling:
library(kableExtra)
table %>%
knitr::kable() %>% # you can control column names, row names, rounding of table here
kableExtra::kable_styling(latex_options = c("scale_down") # will fit table
kable_styling has an argument full_width for HTML tables too. For more information on this library see this link

How to made a flextable table in the same MSWord style than tables made with knitr::kable, or how to define interlines?

After kniting a Bookdown project into a .docx file, the - excellent - flextable package made tables in my .docx document with MS-Word 'Normal Style', which has very large interline and is supposed to define the text, nor the tables' cells.
Please, how can I made a flextable table with 1.0 interline or how can I made the flextable tables in the same style than the knitr::kable() ?
The knitr::kable() behavior is to define the MS-Word 'Compact Style' for the entire table, after knitting to .docx. This behavior of knitr::kable produce tables with simple interlines, in my case (vs. the 'Normal Style' for flextable tables with 1.5 interlines). The following image is an example of these differences, in my .docx output.
It seems that it's not possible to apply a MSWord style on a flextable object, and we have to set the police size and font by adding font(), etc. But flextable::padding(padding = 0) doesn't define interline. Maybe I'm wrong on this point ? There is a way to define interlines for a table, or set the same MSWord style for all the tables produced with flextable ?
Thanks a lot, un très grand merci.
It seems that the - wonderful - flextable package is now able to set interline efficiently, with flextable::line_spacing(). So, for a table with only a single line of text in each row (like in the example above), you have to add:
line_spacing(x = flextable_object, space = 0.8, part = "body")
This work well for tables with one single line of text in each cell. If there is several lines of text in a single row, be aware that flextable::line_spacing() affect the line spacing of these several lines of text in a row, in addition to the global line spacing between the lines of the table.
Thanks a lot David G.

Markdown output not fitting column

I'm writing an article in RStudio (global setting is using knitr) trying to make the output generated by a chunk of code fit the specification of the IEEE jornal, butthe output will wonder off the column (text is organized in two columns).
I've tried specifying the out.width of the code chunk with the column width (out.width="\columnwidth") but it doesn't seem to do much. I've also tried specifying the same parameter with "\linewidth" and "\textwidth" but still I get the same output to the pdf. I've also tried scaling down the size by puting in "%10" and "1%" but no sucess.
<>=
levels(classObito$estado_fisico)
#
Expected the output of the code chunk to fit into the column, but it wonders off the page or to the other column.
Solved with adding option(width="50") inside the code chunk

Aligning XTable Output in R Markdown

I am putting an assignment together in R Markdown to PDF (through Latex) and been using xtable to nicely format my output. The below code generates a small table that I would like to align right in the document and have text wrap around it. Is there a way to achieve this?
summary.table <- xtable(ddply(aircon, ~when, summarise, Mean=mean(hours), StdDev=sd(hours)), caption="Mean and Standard Deviation (in Hours) Before and After Change")
print(summary.table, include.rownames=FALSE)
Given the update regarding using LaTeX to do the wrapping, how could I write this in R Markdown to take advantage of this? I have tried to print inline using r <code> but that didn't work.
I guess you could add the begin/end wraptable commands in the r-chunk that creates the table, like this:
cat("\\begin{wraptable}{r}{5.5cm} \n")
...
print(summary.table,...)
cat("\\end{wraptable} \n")
You can use the Includes mechanism documented here, to include a custom header.tex which would contain usepackages, etc.

Displaying dataframes in R Markdown

I can't find a method to remove the hash marks and row numbers from dataframes outputted to a word document in R markdown. I'd like to be able to present only the data without those features
The knitr website and specifically the page on Chunk options suggests the use of a separate chunk (before your want to display a data.frame in this manner) to change the default for the chunk option comment, perhaps like this:
```{r global_options}
opts_chunk$set(comment = NA) # default value is '##'
```
to disable the inserting of comment characters on output. Realize that this setting of the comment option is applicable to all chunks that follow this chunk; this chunk itself will not be affected by it.
This does give the textual representation of the data.frame (as if it were on the terminal), and not a more refined representation. I second #PierreLafortune's suggestion to look at knitr::kable.
Check out the sjPlot package and specifically the view_df function

Resources