In bookdown: avoid wide tables to be cut-off - r

When using bookdown and rendering a gitbook, wide tables are being cut-off to the right.
Try
libray(dplyr)
cbind(mtcars, mtcars) %>%
knitr::kable(caption = "A wide table")
in any .Rmd for bookdown.
Although in general I find the kableExtra package not working very well with bookdown (especially bootrstrap_options and full_width), it seems that we can use scroll_box(width = "100%", height = "300px") to allow for wide tables to be displayed correctly.
Sometimes, one would only know if a table is too wide after rendering the document.
Question: is there any way that we can depict wide tables without explicitly using the scroll_box() option?
Thanks in advance!

If you output is gitbook only, you can try out rmarkdown::paged_table.
Otherwise, you can try to make you scroll_table box not noticeable and use it to every table...
library(kableExtra)
kable(cbind(mtcars, mtcars)) %>%
kable_styling() %>%
scroll_box(width = "100%", box_css = "border: 0px;")
For all bookdown multi-format tables, you need to specify new_session: true in the _bookdown.yml file.
I have an example here https://haozhu233.github.io/kableExtra/bookdown/cross-format-tables-in-bookdown.html#prepare-your-tables-for-all-formats. Note that recent version doesn't require you to specify options(kableExtra.html.bsTable = T) anymore. I will change that doc.

Related

print_md in huxtable changes table formatting

I am using the huxtable package to create tables in a PDF rendered in bookdown. The table is formatted exactly the way I want it, up until I run the print_md command, after which a border is moved up row from underneath the column names to underneath the header. Also, the header is moved from a centered position to right-aligned. Check it out:
df <- data.frame(
"colname1" = c("something indicator"),
"colname2" = "[Something](http://www.overleaf.com)",
"colname3" = "[Something again](http://www.overleaf.com)")
df <- df %>%
as_hux() %>%
theme_basic() %>%
set_tb_padding(2)
df <- df %>%
set_contents(1, 2:3, c("colname2", "colname3")) %>%
insert_row("", "Header", "Header", after = 0) %>%
merge_cells(1, 2:3) %>%
set_align(1, everywhere, "center") %>%
set_tb_padding(1, everywhere, 0) %>%
set_bold(1, everywhere)
df
Which gives:
Table is formatted correctly. But. You'll notice that the URLs are not formatted correctly. It should only be showing the part within the brackets, which when clicked will take you to the site in parentheses.
This can be remedied with the following code:
df %>% print_md()
Which gives:
Now the URLs look like they should, but the border has erroneously moved up a row, and "Header" is now right-aligned instead of center-aligned. How do I stop that from happening?
Don't ask me why it works. But changing print_md() to set_markdown() fixed both the border and alignment problems.
EDIT: I'm adding #dash2's comment to this answer.
The reason print_md() was causing problems is because it converts the table to markdown format, which R Markdown then reads and produces a table from. So some features (alignment) get lost in translation. It'd be better to print the table in the intended output format, be it Latex, HTML or whatever you're using, instead of markdown.
But the cells with markdown hyperlinks need to be respected still - print_md() is just the wrong way to go about it. Instead, use set_markdown(). This will ensure that, within huxtable itself, cells with markdown code are interpreted as markdown before the table is printed. The printed table will then retain the intended format.
Thank you #dash2 for creating such a powerful package!

Knitr table and formattable functions don't work in Rstudio & Shiny

i'm trying to make my knitr tables (through KableExtra package) more appealing by adding some of the functions of the formattable package (i.e. colortile, ...).
But when running the code, the table gets visualized, but the table does not render the elements of the formattable. However I can see the HTML code in the table where the formattable functions would have to take place (6th column in the printscreen below):
This is my code to render the table:
input_file %>%
mutate(
month_perc = color_bar("lightgreen")(month_perc)) %>%
kable(format = 'html')%>%
kable_styling(bootstrap_options = c("hover","striped"))
Probably this is an easy fix, but does anyone have an idea why it doesn't render?
Thanks in advance!
Kind regards,
Simon
You need escape=F in your kable function.

FlexTable output .docx horizontal table adjusted to narrow margins

I have a flextable object called html_table which I want to directly put in a word document, in horizontal layout with narrow margins.
I face 2 problems:
1) The approach suggested in the vignette produces extra pages (one before, one after the table). I think this is a known issue but not clear how to solve it.
2) I would like to have narrow margins and the resulting table on horizontal pages to be automatically fitted to the page. I want this so that I can print the table using as much page as I can. My current approach is to manually open the document, change the layout and select "autofit" on Word.
Here's the code I'm using to produce the document. For illustrative purposes, I will use mtcars for my table, but the real one has more rows than mtcars.
html_table <- regulartable(mtcars)
doc <- read_docx() %>%
# Make it landscape
body_end_section_continuous() %>%
# Add the table
body_add_flextable(value = html_table,
split = TRUE
) %>%
body_end_section_landscape()
# Write the .docx
print( doc, target = "my_table.docx" )
In Word document, sections are only defined when they stop (I can not explain why it has been made that way but this is how the underlying xml is...). Also a landscape oriented section need a page break if the preceding section is not landscape oriented.
To autofit a flextable, use function autofit.
library(flextable)
library(officer)
library(magrittr)
html_table <- regulartable(mtcars) %>%
autofit()
doc <- read_docx() %>%
body_add_flextable(value = html_table, split = TRUE) %>%
body_end_section_landscape() %>% # a landscape section is ending here
print( target = "my_table.docx" )
If you don't want extra page, you will need a template with a default page orientation as landscape. Also, you would not need any code to manage orientation nor margins.

knitr: exporting to html file but keeping style

I just found out the awesome knitr library in R, when viewing the result in the viewer it seems nice. However, when I write this to a html file the style is lost.
Code
library(knitr)
library(kableExtra)
some.table <-
data.frame (
x = rep(1,3),
y = rep(1,3)
)
some.table
x <- kable(some.table, format = "html") %>%
kable_styling(bootstrap_options = "striped", full_width = F, position = "left")
x
file <- file('test.html')
write(x, file)
Table in viewer
Table in browser
How can I export the table with the same style to a html file?
Note that I have more data in the html file, so I should be able to append it.
Response to comment(s)
User: #Hao
When I use 'inspect element' in the Rstudio viewer, I can find this link to a stylesheet:
However the code herein seems to be huge as it is 582.298 characters.
The typical way of doing this is to put the code inside a rmarkdown document. It will handle everything for you.
The only case you need to use the save_kable function kableExtra is that you have lots of tables and you want to save them as fragments. In that case, you can use
library(kableExtra)
cars %>%
kable() %>%
kable_styling() %>%
save_kable()

Is it possible to include a footnote without symbol in KableExtra?

When I use add_footnote("Footnote 1"), KableExtra always introduce a symbol in the footnote. But in many occasions, I would like to include a footnote without a symbol. For example, I would like to use add_footnote("Source: Consumer Expenditure Survey") to include information about the source of the data. But I do not want to have the symbol in front of "Source..." in my table. Is that possible?
Now you can. :)
With current dev ver (should be on CRAN in a week or so), there is a new function called footnote. To get some non-labeled footnotes, just type
mtcars[1:5, 1:5]%>%
kable("latex", booktabs = T)%>%
footnote(general = c("Note 1", "Note 2"), general_title = "")
Similar for html tables.
I don't think so if you want to use kableExtra. If you're producing your document in LaTeX, see https://tex.stackexchange.com/questions/30720/footnote-without-a-marker for a way to do it in pure LaTeX.

Resources