How can I fix knit tables in RMarkdown? - r

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

Related

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

Stargazer in a bookdown site

I'm trying to show Stargazer tables (both for regressions and data summary) in a bookdown based website. stargazer output does not look like it looks in outside bookdown (i.e markdown/knitr/html). It has spaces between rows, rows are zebra striped (abit like bootstrapped theme)
I believe that the style.css overrides stargazers' known table format, however I couldn't find any evidence to that, nor couldn't modified it by myself.
I'm sorry for asking without reprex, I find it a bit hard to make reprex to a book. Anyway, stargazer chunk is:
```{r ,results='asis'}
stargazer(lm1, type=`html`)
````
So to problem is that stargazer tables are wrapped again according to the styling in the "plugin-table.css" file that located in
"./_book/libs/gitbook-2.6.7/css/plugin-table.css".
I get rid from the Kable-like formating when I remove this file it after rendering the book with these command:
bookdown::render_book("index.Rmd","bookdown::gitbook", clean_envir = TRUE) # render to HTML
file.remove("./_book/libs/gitbook-2.6.7/css/plugin-table.css")
Of course, this is not the best practice, this solution hurts kableextra formating for example. However, as I tend to use mainly Stargazer, I don't find it that bad.

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

Style a kable table knitted to pdf

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

Is it possible to use Rtable (FlexTable) to generate a pdf table in RMarkdown

I'm creating some RMarkdown reports and want to be able to extensively format a table. I've tried working with kable and pander but they don't give me enough functionality (for example to use colors for rows).
I ran across the Rtable package (that goes along with the FlexTable object of the Reporters package) which allows significant customization. The only problem is that I can so far only get my table to show up when I knit to html, when I knit to pdf (what I need) I just get an unformatted list of text instead of my table.
here's what my code looks like (summaryterms is a dataframe)
setZebraStyle(FlexTable(summaryterms[c(5,1:3,9:11,14)]),odd = "#DDDDDD", even = "#FFFFFF" )
Is there anyway to use the FlexTable object with a pdf report?
Thank you so much for the help
Also, I'm open to any and all other solutions to format tables in rmarkdown, I'm not married to using flextable.

Resources