Print kable table to .tex file - r

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')

Related

How do I remove this Rmarkdown output after the read_excel function?

How do I go about removing the output shown in the picture below after I use read_excel to import the data in r markdown. Basically I don't want there to be any output after this function. Please see attached image.
bdims <- read_excel("bdims.xlsx")
head(bdims)
You can set results = 'hide'. So if eg. for standard rmarkdown. This will stop the output from showing. Read on the cheat sheet how to use other settings such as echo and eval
{results = 'hide'}
bdims <- read_excel("bdims.xlsx")
or with roxygen document for rendering rmarkdown,
#+ results = 'hide'
bdims <- read_excel("bdims.xlsx")

Output from `kable()` combined with text as character vector and print table

I have the following RMarkdown Document (also here as a gist)
results in this
I would like to use the function fun() to create a proper table as from the first code chunk.
I thought that format = "markdown and results = "asis" should do the job, but apparently not.
What Am I missing here?

How to prevent a kable from splitting between pages?

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.

Including links within Rmarkdown tables (pdf)

I am trying to include links to particular webpages in a 'kable' table in Rmarkdown, when creating a pdf.
The table has 4 columns, and I wish for the links to be in the second column, which currently includes strings. The output of the table is given below;
knitr::kable(ind_rank_table_final,row.names = FALSE,caption = "Industry Rank",align = rep("l",ncol(ind_rank_table)))
Using paste0, you can construct markdown-formatted URLs in your dataframe, and then pass that to kable, like so:
---
output: pdf_document
---
```{r}
# some urls
urls <- rep("https://stackoverflow.com/", 10)
# use paste0 to compose markdown-formatted hyperlinks
mtcars$mpg <- paste0("[", mtcars$mpg, "](", urls, ")")
# print the table, with hyperlinked text
knitr::kable(head(mtcars))
```
And you can see the result, blue text in the mpg column, and if I hover my mouse over, I see the URL:
If you want to print the URLs in the table, and have them clickable, then you'de do something like this mtcars$mpg <- paste0("[", urls, "](", urls, ")") like so:
Is that what you're after? This use of paste0 is pretty handy for doing all sorts of things to tables, for example, combining multiple values in one cell, and applying conditional formatting (like bold for significant values)
For those knitting to PDFs using bookdown, #Ben's answer will not get you fully the way there. As #mavericks pointed out, you will still see the full text ([21](https://stackoverflow.com/), to keep with #maverick's example).
To fix this, add the argument format = "pipe" or format = "simple" to kable(). The "latex" option, while generating a working link, will display like #maverick's example. The default behavior for kable() is to automatically determine the format, which I guess in the case of a bookdown document must be "latex"?
I don't know, but this should generate #Ben's first table for bookdown users:
output: bookdown::pdf_document2
# some urls
urls <- rep("https://stackoverflow.com/", 10)
# use paste0 to compose markdown-formatted hyperlinks
mtcars$mpg <- paste0("[", mtcars$mpg, "](", urls, ")")
# print the table, with hyperlinked text
knitr::kable(head(mtcars), format = "simple")

How to layout character value within R chunk

In the knitr package I like the kable function. It gives a nice layout of tables and data frame like objects even as it is called from within an R code chunk. Now I want to do the same thing with a character value. Is there a function that gives a kable-like output ("kprint") that can be formated?
knitr::kable() # exists for tables
knitr::kprint() # does a function like this exists for character values?
This is what I get now:
print("character value") # within the R Chunk
Output in generated report:
## [1] "character value"
And this is what I want, just:
character value
EDIT cat("character value") is not the solution I am looking for because I don't want an R output anymore, but just a plain text.
There are two things to do to get a "raw" character string (without any formatting or additional output like [1]) from R to TEX:
Use the chunk option results = "asis" to instruct knitr not to modify the output.
Use cat instead of print because print adds the lenght of the vector and quotes to the output.
In this context, inline output using \Sexpr{} might be useful because values in \Sexpr{} are by default printed "as they are": \Sexpr{myoutput}.
As there was the question of how to format the output in the comments, here some options:
Add LaTeX to the text you pass to cat: cat("\\emph{foo}"). Don't forget to escape \ by an additional \.
Do the same thing as above, but use a function to do the "dirty work":
makeItNiceR <- function(x) {
return(paste("\\fbox{\\texttt{", x, "}}"))
}
cat(makeItNiceR("foo bar is nice"))
(Note that we could use cat inside makeItNiceR to save some typing, but this makes the function less flexible and we cannot use it in combination with \Sexpr{} anymore.)
Manually add LaTeX formatting commands around \Sexpr{}:
Add formatting to \emph{\Sexpr{myoutput}} directly in LaTeX.
Combine makeItNiceR and \Sexpr{} to get nicely formatted output from \Sexpr{}:
\Sexpr{makeItNiceR(paste(myoutput, "is nice"))}
The following minimal examples demonstrates the usage of all code snippets from above:
\documentclass{article}
\begin{document}
<<results = "asis">>=
makeItNiceR <- function(x) {
return(paste("\\fbox{\\texttt{", x, "}}"))
}
myoutput <- "slim"
cat("foo")
cat("\\emph{foo}")
cat(makeItNiceR("foo bar is nice"))
#
\paragraph{Outside of chunk:} ~\\
\Sexpr{myoutput} \\
Add formatting to \emph{\Sexpr{myoutput}} directly in LaTeX. \\
\Sexpr{makeItNiceR(paste(myoutput, "is nice"))}
\end{document}

Resources