I am knitting to pdf using kable() to draw some tables. I create a few tables functionally, thus some of them end up being split between pages. Is there any way to prevent this behavior?
I know I could just move to a new page after each table but I would much rather have multiple kables on the same page.
If you're only exporting to PDF, then try this:
knitr::kable(
my_data,
format = "latex",
longtable = FALSE
)
A longtable table allows page breaks between rows. Looking at the code for knitr:::kable_latex, which kable calls, the default should be longtable = FALSE. But explicitly setting this argument makes sure you're not making longtables.
Related
I have a table generated in kable that I want to print to a .tex file, in the same way I would use print.xtable.
that is,
print(xtable(data.frame, xtable.options), print.options, file = filename.tex))
When I tacked print onto a kable table generation, it doesn't work. It seems to be ignored.
I am using:
kable(df, format = 'latex', kable.options) %>% print(file = filename.tex)
Note, the pipe operator %>% should drop the preceding into the first argument of the following function. I do not see a print.kable function available. Is there some other function I am missing?
I am using kable to allow for grouping columns; something xtable by default doesn't appear to allow.
You can use cat -
cat(kable(head(mtcars), format = 'latex'), file = 'file.tex')
If I have a chunk like
niris <- iris %>%
select(starts_with("Sepal"))
The knitr output of the chunk is
niris <- iris %>% select(starts_with("Sepal"))
But I would like it complies with the break lines of my code so it is printed in html as I print in code
. I was looking at R Markdown reference guide and at Yihui web and trying multiple options from Results and Code decoration and I couldn't get my goal.
Do you know how should I do?
Thank you!
You didn't provide a reproducible example, so I can only guess. You probably turned on the tidy option (i.e., you used the chunk option tidy = TRUE). If you did that, either set tidy = FALSE or just leave out tidy = TRUE since tidy = FALSE is the default.
I am using blogdown to to create a blogpost that has a series of tables. Creating a single table using the kable function works fine. If you do
blogdown::new_site()
blogdown::new_post("test", ext = ".rmd")
A new rmd file will be created within the content/post directory of the project. If you open that file and create a single table by doing
```{r test1}
library(knitr)
library(magrittr)
library(shiny)
data.frame(a= c(1,2,3)) %>% kable(caption = 'test',format = 'html')
```
A correctly formatted table will be generated. The caption will read "
Table 1: test" If you look at the code of the generated site, the caption will look like this.
<caption>
<span id="tab:test1">Table 1: </span>test
</caption>
Ideally I don't have any desire to label the table as Table 1 in the first place but that is another question. If formatting of captions by kable can be disabled entirely, I'd also be happy.
However if I use lapply to generate 2 tables instead
```{r test2}
lapply(1:2,function(x){
data.frame(a= c(1,2,3)) %>% kable(caption = 'test2',format = 'html') %>% HTML()
}) -> tables
tables[[1]]
tables[[2]]
```
The captions will have the prefix \#tab:test2. If you look at the caption of these tables, you'll see
<caption>(\#tab:test2)test2</caption>
The question is, why kable behaves differently when its called from a lapply compared to its behaviour outside? Note that both of these behaviours are different that its behaviour when simply knitting the file as an html_document.
I did some digging into the kable's code and found that the caption link is created by the knitr:::create_label function. Looking into this function, I saw the part that is responsible for the wrong behaviour seen with the multiple tables.
if (isTRUE(opts_knit$get("bookdown.internal.label"))) {
lab1 = "(\\#"
lab2 = ")"
}
I could not find the code, responsible for the "correct" behaviour with the single table but it seems like knitr internal options are responsible.
Ultimately the behaviour that I want is simply
<caption>test</caption>
which is the behaviour when simply knitting an html document. But I am yet to find a way to set the relevant knitr options and why are they different within the same document.
Edit: Further examination suggests that the issue isn't lapply specific. It can be replicated using a for loop or even { by itself. A complete post with all the problematic examples can be acquired from this issue on knitr's github page. This github repo includes the basic blogdown site that replicates the issue
Turns out the responsible party is not the lapply call but the HTML call at the end. It seems like the regular process by knitr in blogdown and bookdown is to have a temporary marker for the table references in the form of (\#tab:label) and replace it with the appropriate syntax later in the processing.
I was using the HTML call to be able to use the tags object in shiny/htmltools to bind the tables together. This approach seems to make the process of replacing the temporary marker impossible for reasons outside my understanding. For my own purposes I was able to remove the temporary marker all together to get rid of both malformed captions and the working-as-intended table numbers by doing
remove_table_numbers = function(table){
old_attributes = attributes(table)
table %<>% as.character() %>% gsub("\\(\\\\#tab:.*?\\)","",.)
attributes(table) = old_attributes
return(table)
}
data.frame(a= c(1,2,3)) %>% kable(caption = 'test',format = 'html') %>% remove_table_numbers
This question still would benefit from a proper explanation of the reference link placement process and if its possible to apply it to tables in HTML calls as well. But for know this solves my issue. I'll gladly switch the accepted answer if a more complete explanation appears
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.
How do you put on a big table of xtable the table header on each page?, So that is easier to read the table the table between pages.
I use the following in Sweave:
test.big<- xtable(test,label="table",caption='test')
align(test.big) <- "|c|c|c|c|l|c|c|c|"
print(test.big,tabular.environment='longtable',include.colnames = TRUE,floating=FALSE)
Thanks for your answers
I think a better answer for this question is provided here: Column names on each page with xtable in Sweave
What if you want to edit your table in R? The solution above edits the output, so you won't need to add those lines in your longtable code manually. Thus this works better:
print(test.big, tabular.environment='longtable', include.colnames = TRUE,
floating=FALSE, add.to.row = list(pos = list(0), command = "\\hline \\endhead "))
Note that you can add several arguments in your add.to.row list:
print(test.big, tabular.environment='longtable', include.colnames = TRUE,
floating=FALSE, list(pos = list(seq(1,nrow(get(groups[i])), by = 2), 0),
command = c("\\rowcolor[gray]{.95} ","\\hline \\endhead ")))
You'll have to add this to your Sweave file:
\usepackage{colortbl}
This produces gray filling on every second row & header for each page.
The longtable (LaTeX) package specification can be found at that URL. The section of code in the examples whose output appears on pages 2 and 3 is in section 8 and I have reproduced bit of it below:
\caption[]{(continued)}\\
\hline\hline
\multicolumn{2}{#{*}c#{*}}%
{This part appears at the top of every other page}\\
\textbf{First}&\textbf{Second}\\
\hline\hline
\endhead
When they say on "every other page", they mean every page other than the first, which had a different header. If the xtable call is not working out of the box without any editing, then you should first check that you have the longtable package specified in your LaTeX preamble:
\usepackage{longtable}