Using the iris data, we make a table with the tables package:
library(tables)
table <- tabular( (Sepal.Length+Sepal.Width) ~
Format(format(digits=2))*(mean + sd), data=iris )
With the Hmisc package, we would add the caption with the latex function like this:
latex(table, caption="My table")
But that does not work with the tables package, because it defines an S3 method, latex.tabular.
So I have tried this, following an example in the package vignette:
latex(table, options=list(toprule="\\caption{My table}"))
But it does not work, it says that caption is outside float. How can I correctly add caption with the latex.tabular function from the tables package?
I figured out one way to do it and it is actually pretty easy: you just have to embed the tables package result inside the latex code.
Below one example with knitr, the << >>= is the r chunk code.
\begin{table}
\caption{My awesome table from tables package}
\begin{center}
<<cool multilevel table, results='asis', echo=FALSE>>=
latex(table)
#
\end{center}
\label{tab:mytable}
\end{table}
This generated this awesome table, now with title (in portuguese):
I am posting this to help late to help other people looking for help with this. I have been working to understand the tables package recently. The workflow I use is to export the table to a .tex file then pull those into my document which I compile with TexStudio. The solution above wont work with this workflow, so I am providing one which will, it will produce a caption below the table:
latex(table, , options=list(bottomrule = "\\bottomrule\\\\
\\caption{My table}")
This will produce one above:
latex(table, , options=list(bottomrule = "\\caption{My table}\\\\
\\toprule")
The tables package now has a function latexTable(), when used with a latex.tabular object, can add caption and labels. See the vignette
Related
I am using huxtable to display tables in RMarkdown for output: pdf_document.
When I put a label in the table
huxtable(mytable) %>%
set_label("tab:mylabel")
and then make a reference in the body text like
See Table \#ref(tab:mylabel) for more details
It displays "See Table #ref(tab:mylabel) for more details" rather than giving the table number.
This was covered for Bookdown in
Huxtable package for R: How to correctly reference huxtables in bookdown?
Do I need some special option or other setting to make it work for a regular PDF document?
Seems so. The rmarkdown guide says you will need "a bookdown format" to use cross-referencing in that way. However, there's an alternative: use embedded TeX, rather than this rmarkdown format. A MWE:
```{r}
hux("A huxtable") %>%
set_label("tab:foo") %>%
set_caption("You'll need a caption too")
```
Here's a reference to \ref{tab:foo}.
In bookdown how can I include R code within a custom block, whereby the R code will be parsed. Like in the example below the r code (plot function) will not be parsed. Is there a way to make this work?
```{block2 type='test'}
some text here
plot(1:10)
```
You can use text references as a hack. This seems to work as long as valid_markdown contains a single paragraph/line of markdown.
```{r}
valid_markdown <- your_function()
```
(ref:my_hack) `r valid_markdown`
```{block2, type="block"}
(ref:my_hack)
```
This hack works fine with text. Getting it work with plots will require a bit more work.
I was looking for a way to change the setting in my Rmd file so that the html output contains all the columns and the table does not break. I tried to change the css properties as in this solution (Output table width in Rmarkdown) but this does not affect my output.
I have currently 17 columns and using a pandoc.table, but only 5 coloumns are shown before the table is broken and the next 5 columns are displayed below.
What changes do I need to make so that the entire table can be shown in my html output?
Thanks for your help.
I can't use the pandoc package because is not currently available for R version 3.2.0. Instead, I used knitr with the kable() function. This code works fine:
{r, echo=FALSE, results='asis'}
library(knitr)
examp <- data.frame(matrix(rep("Unicorn"), nrow=5, ncol=100))
kable(examp)
I think, because you don't provide an example, that you need to specify the results='asis' chunk option.
Try ?kable for further information.
Anyway ?pandoc.table shows that there is an option split.table that may help.
I am sorry, I am new to using knitr to make slides. I generally use the latex() function in Hmisc package to generate my tables based on R objects. I would like to produce a slide that shows the r code and then below it displays the properly formatted table. Something like:
``` {r}
latex(tabdat,file="tables/tabdat.tex",ctable=TRUE,caption="A basic table",caption.loc="bottom",label="tab:dat1",row.names=NULL,rowlabel="")
```
So that the finished slide displays the exact r code and the formatted table looking exactly as if I had run latex using \input{tabdat}
I would appreciate any advice on how to accomplish this.
Thanks!
I am a bit puzzled because you talk about PDF/LaTeX output but you are using R markdown tags.
Here are small examples for both cases, R Sweave, i.e. LaTeX output, and R markdown, i.e. HTML output. For creating the LaTeX code there are several packages available (xtable, Hmisc etc.) for HTML AFAIK only xtable.
The main point of how to include the raw output just like it appears in the console is the same for both output types and was already explained by Tyler Rinker above, i.e. by adding results="asis" to the chunk options.
PDF/LaTeX / Rnw-file
\documentclass{article}
\begin{document}
<<echo=FALSE, message=FALSE>>=
library(Hmisc)
library(xtable)
#
<<results='asis'>>=
latex(head(iris), file = '')
#
<<results='asis'>>=
xtable(head(iris))
#
\end{document}
HTML, Rmd-file
```{r echo=FALSE}
library(xtable)
```
```{r results='asis'}
tbl <- xtable(head(iris))
print(tbl, type="html")
```
Look here for more examples and options: http://www.stat.iastate.edu/centers/CCGS/slides/slides-Rtables.pdf
I'm using Sweave and latex() from the Hmisc package to insert a longtable in my PDF.
When I do it the first time, the table is spread nicely, filling up the pages with the table.
If I do it again, some pages are only half full (like page 4 of PDF), which looks weird and somehow wrong because it seems to be unnecessary space.
Is there a way to control this? Or what could I do to improve the look?
Especially, if I'm adding some text and graphs, it won't look good with the empty space on page 4.
\documentclass{article}
\usepackage{Sweave}
\usepackage{longtable}
\usepackage{booktabs}
\begin{document}
\SweaveOpts{concordance=TRUE}
I want to insert this longtable
<<tab.R,echo=FALSE,results=tex>>=
library(Hmisc)
#library(xtable)
x <- matrix(rnorm(1000), ncol = 10)
x.big <- data.frame(x)
latex(x.big,"",file="",longtable=TRUE, dec=2,caption='First longtable spanning several pages')
#
then write some text. Maybe add a graph...
And then another table
<<tab.R,echo=FALSE,results=tex>>=
latex(x.big,"",file="",longtable=TRUE, dec=2,caption='Second longtable spanning wrongly')
#
\end{document}
Don't pass this question over to the latex group, this is a problem of Hmisc/latex that adds a \clearpage into tex every 40 lines by default. Check parameter lines.page=40 of latex. I do not understand why this default has been set, but something like
latex(x.big,"",file="",longtable=TRUE, dec=2,
caption='Second longtable spanning wrongly', lines.page=4000)
gets you around the problem.