creating LaTex table from xtable in R - r

I have a very basic question however I'm stumped.
I am creating a LaTex table in R and I am having trouble with actually outputting the real table.
Here is example code borrowed from:
http://cran.r-project.org/web/packages/xtable/vignettes/xtableGallery.pdf
data(tli)
tli.table <- xtable(tli[1:10,])
print(tli.table,floating=FALSE)
This code just gives me the raw table output:
% latex table generated in R 3.0.3 by xtable 1.7-3 package
% Tue Jul 15 13:23:25 2014
\begin{table}[ht]
\centering
\begin{tabular}{rrlllr}, etc. etc. etc.
How in the world do I actually see the real table that is shown in the above referenced table. The code in that source seems like the above 3 lines produce the table but, for me, it's just producing the raw version.
Forgive the simplicity of this question but I am stumped.

Related

Obtaining a regression table with texreg in Lyx

I'm using the packages knitr and texreg in a Lyx document and I've got this problem. When I'm using a float table in Lyx, I've always got this lines before the table in the pdf:
## Version: 1.36.23
## Date: 2017-03-03
## Author: Philip Leifeld (University of Glasgow)
##
## Please cite the JSS article in your publications – see citation("texreg").
How could I get only the table without these lines?

R, knitr doesn't respect order of chunks and text

Imagine I knit this Rnw file:
\documentclass{article}
\begin{document}
Table1
<<example,results='asis', echo=FALSE>>=
require(xtable)
nn <- 15
mydata <- data.frame(A=1:nn,C=nn:1, C=runif(nn), D=rnorm(nn))
xtable(mydata, caption="Table1")
#
Table2
<<example2,results='asis', echo=FALSE>>=
xtable(mydata, caption="Table2")
#
Table3
<<example3,results='asis', echo=FALSE>>=
xtable(mydata, caption="Table3")
#
\begin{obeylines}
Just some text
\end{obeylines}
\end{document}
It's a simple example that just prints some text and three tables.
Strangely it doesn't respect the order of what I've written on my code.
I get this (sideview of the two pdf pages)
But "Table3" text should appear before the table3 itself and after table2, and the text "just some text" should appear at the very end of the document.
If I write several lines there it breaks the lines.
I understand that if a table doesn't fit on a place it must be moved to the next page but so should be done with the following text and tables.
I've also observed that in other examples some tables are reallocated randomly when they don't fit well.
How can I prevent knitr from doing this?
I don't know whether is a knitr problem or latex.
I'm using Texlive 2015, Rstudio, R 3.2.3 and Windows 10 and the latest version of all packages involved.
By default print.xtable() produces a LaTeX \table{...} environment, which is defined as a floating object. See `?print.xtable and try e.g.
<<example2,results='asis', echo=FALSE>>=
print(xtable(mydata, caption="Table2"),floating=FALSE)
#
(untested ...)
alternatively you could try table.placement="H"; you may need \usepackage{float} (see this question from tex.stackexchange.com).
(also untested ...)

how do you print xtable using knitr in word document

I am trying to print xtable in using knitr in word document as follows:
library(knitr)
library(xtable)
p1<-xtable(head(iris), include.rownames=FALSE, floating=FALSE)
print(p1)
I get this error, any ideas:
latex table generated in R 3.2.2 by xtable 1.7-4 package % Mon Oct 26 11:17:22 2015
I don't think you can do this with the standard word_document that comes with rmarkdown. xtable only produces output in HTML and LaTeX formats. To render a word document, your table has to be prepared in markdown format (so far as I know).
Some alternative options are to use knitr::kable or the pixiedust package.
If you're comfortable downloading things from GitHub, you may also use devtools::install_github("gforge/Grmd") and then replace output:word_document with output: Grmd::docx_document. Then using print.xtable with type = 'html' will render to an HTML document that can be opened as a docx.

Problems cross referencing in LaTex with knitr and xtable

I am currently working with R Studio to produce PDF documents with R/knitr in LaTex. Within these documents I want to present part of my results in tables that I want to refer to in the text. I produce these tables using the xtable package within R and it is working perfectly and giving me the right tables.
So far so good but when it come to referencing I ran into some troubles. At the moment the resulting PDF output refers to the section in which the table lies. I searched around in some LaTex help files and it is stated that this happens when the label of the floating object is placed before the caption. However when I checked the output of xtable it was placed after the caption.
Does anyone have a solution for this?
Here is an example code similar to the one I used that gives me the same kind of result in the PDF (I left all packages used in LaTex and R in to make sure it is not something caused by some interference with these).
\documentclass[preprint,authoryear]{elsarticle}
\usepackage[a4paper]{geometry}
\usepackage{natbib}
\usepackage{mathpazo}
\usepackage{booktabs}
\usepackage{dcolumn}
\usepackage[breaklinks]{hyperref}
\usepackage{hyperref,url}
\usepackage{lscape}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
\usepackage{amssymb}
\usepackage{gensymb}
\usepackage[allow-number-unit-breaks=true]{siunitx}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[version=3]{mhchem}
\usepackage[section]{placeins}
\usepackage[colorinlistoftodos]{todonotes}
\usepackage{float}
\usepackage{lineno}
\begin{document}
\section{Test}
This is a test. You can see the result in Tab. \ref{tab:Test}.
\section{Table}
<<Ini,eval=TRUE,include=FALSE,echo=FALSE,warning=FALSE,error=FALSE,cache=FALSE>>=
#set chunk options
opts_chunk$set(echo=FALSE,warning=FALSE,message=FALSE,dev.args=list(pointsize=12),fig.pos="!h",dpi=500)
require(stringr)
require(reshape2)
require(lubridate)
library(plyr)
library(lattice) #Needed for multi-panel graphs
library(mgcv) #Needed for smoothing curves in scatterplots
library(ggplot2)
library(ggmap)
require(gridExtra)
library(scales)
library(xtable)
require(nlme)
library(zoo)
#
<<results='asis',echo=FALSE>>=
Test<-data.frame(Test=c(1:3),a=c(4:6))
print(xtable(Test,caption="Test table",label="tab:Test",
digits=2),
include.rownames=FALSE,
caption.placement="top",
latex.environments = "left")
#
\end{document}
The result from the xtable code looks like this:
% latex table generated in R 3.0.3 by xtable 1.7-4 package
% Tue Jun 02 09:27:38 2015
\begin{table}[ht]
\begin{left}
\caption{Test table}
\label{tab:Test}
\begin{tabular}{rr}
\hline
Test & a \\
\hline
1 & 4 \\
2 & 5 \\
3 & 6 \\
\hline
\end{tabular}
\end{left}
\end{table}
When I try to run this I get 4 warning messages that seem kind of strange but there is still a PDF output. All 4 warnings are around the code that is produced by xtable. Here the output from the Log within R Studio:
! Missing $ inserted.
<inserted text>
$
l.81 \begin{left}
I've inserted a begin-math/end-math symbol since I think
you left one out. Proceed, with fingers crossed.
! Missing delimiter (. inserted).
<to be read again>
\global
l.82 \caption
{Test table}
I was expecting to see something like `(' or `\{' or
`\}' here. If you typed, e.g., `{' instead of `\{', you
should probably delete the `{' by typing `1' now, so that
braces don't get unbalanced. Otherwise just proceed.
Acceptable delimiters are characters whose \delcode is
nonnegative, or you can use `\delimiter <delimiter code>'.
! Missing $ inserted.
<inserted text>
$
l.82 \caption{Test table}
I've inserted a begin-math/end-math symbol since I think
you left one out. Proceed, with fingers crossed.
! Missing \right. inserted.
<inserted text>
\right .
l.82 \caption{Test table}
I've inserted something that you may have forgotten.
(See the <inserted text> above.)
With luck, this will get me unwedged. But if you
really didn't forget anything, try typing `2' now; then
my insertion and my current dilemma will both disappear.
I hope there is someone that is able to help me!
PS: Please note that I am not able to post a screen shot of my PDF output because I don't have enough reputation.
The left environment seems to be messing up the referencing. I tried
% latex table generated in R 3.2.0 by xtable 1.7-4 package
% Tue Jun 02 09:53:12 2015
\begin{table}[ht]
\caption{Test table}
\label{tab:Test}
\begin{left}
\begin{tabular}{rr}
\hline
Test & a \\
\hline
1 & 4 \\
2 & 5 \\
3 & 6 \\
\hline
\end{tabular}
\end{left}
\end{table}
and it produces a correct reference.

How to add caption with the tables r package?

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

Resources