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

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.

Related

In bookdown: avoid wide tables to be cut-off

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.

Data Table cross label in rmarkdown

When I call datatable in an r chunk in rmarkdown, the caption is labeled as a figure and I can only cross-reference it as a figure. e.g.
tab_data <- read.csv("localfile.csv")
tab_data %>%
DT::datatable(arguments)
The caption will have a Figure label instead of Table. Is there a way to change the label to Table
This appears to be a known issue with no straight forward answer. see this for more discussion : https://github.com/rstudio/bookdown/issues/313

kable produces malformed reference links within lapply function in blogdown

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

How to append notes to a latex table in Hmisc in R?

An unelegant way is just to add lines of tex code with the note text.
But is there a more native way of making notes in latex function of Hmisc package in R?
There is a way not in Hmisc but using starpolishr package,
install.packages("devtools")
library(devtools)
install_github("ChandlerLutz/starpolishr");library(starpolishr)
Insert footnotes with caption package
la<-latex(mtcars[1:2,1:5],file="example.tex",caption = "table using caption",
where="!htbp")
file0<-readLines("example.tex")
file0<-star_notes_tex(file0, note = "file using caption*")
cat(file0, file = paste0(getwd(),"/file0.tex"),sep="\n")
Insert footnotes with threeparttable
Attention, you must use the caption and label argument in Hmisc::latex
mt <- mtcars[1:2,1:5]
mt[1,1]<-paste0(mt[1,1],"\\tnote{*}")
la<-latex(mt,file="example1.tex",label=" ",caption="table using threeparttable",
where="!htbp")
file1<-readLines("example1.tex")
file1<-star_notes_tex(file1, note.type = "threeparttable",
note = "* notes with threeparttable : For this example you must use both label and caption, the
threepartable code is meant to be used with tables generated by
stargazer where a row with label is present. Otherwise the threepart table
argument is badly positionned.")
cat(file1, file = paste0(getwd(),"/file1.tex"),sep="\n")

header on each page of big table of xtable?

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}

Resources