Remove additional footnote from R's xtable() table output in LaTeX - r

I am using the following R code to call xtable and generate a LaTeX table for a Sweave document.
ifelse(LaTeX==1, print(xtable(rule1.results.noTC, caption="Rule 1 Results 0 Transaction Costs",
digits=c(1,2,4,4,4), display=c("d","d","f","f","f"))),
print(rule1.results))
This produces the following LaTeX
% latex table generated in R 3.0.1 by xtable 1.7-1 package
% Sun Jul 28 16:54:42 2013
\begin{table}[ht]
\centering
\begin{tabular}{rrrrr}
\hline
& L & profits & annCumulExReturn & sharpe \\
\hline
1 & 5 & -888.8215 & -0.1501 & -4.3939 \\
2 & 10 & -909.8941 & -0.1533 & -6.8882 \\
3 & 20 & -893.6245 & -0.1509 & -6.9081 \\
4 & 40 & -865.6764 & -0.1466 & -9.8462 \\
5 & 80 & -832.4700 & -0.1417 & -11.7260 \\
6 & 160 & -757.0690 & -0.1305 & -16.3088 \\
7 & 320 & -626.9162 & -0.1118 & -31.6134 \\
8 & 640 & -340.8740 & -0.0730 & -44.2321 \\
\hline
\end{tabular}
\caption{Rule 1 Results with Transaction Costs}
\end{table}
When I convert this to pdf, I get a nice table. However, it is followed by a weird note:
[1] "
And I get several of these if I plot multiple tables in a row. How can I eliminate this either via R's xtable or by editing the LaTeX code.
Thanks

I can't say for sure without your data and an example of the .tex file, but I'm pretty confident this is due to your use of ifelse. I imagine the following will not give you the weird print out:
if(LaTeX==1) {
print(xtable(rule1.results.noTC,caption="Rule 1 Results 0 Transaction Costs",
digits=c(1,2,4,4,4), display=c("d","d","f","f","f")))
} else {
print(rule1.results))
}
This is because ifelse returns its result, which you're also printing. See, for example:
> ifelse(TRUE,print("true"),print("false"))
[1] "true"
[1] "true"

Related

Equation in a table in R

I want a table like this and make it as a kable so I can make footnote:
but I didn't manage so I made a table latex like this :
$$
\begin{array}{lccccc}
\hline
{Copula} & {Distribution} & {Parameter range} & {Complete dependence} & {Independence} \\
\hline
{Normal} & {C_{Normal} (u_1,u_2,\rho)=\Phi_{\rho}(\Phi^{-1}(u_1),\Phi^{-1}(u_2))} & {\rho\in(-1,1)} & {\rho=1,or -1} & {\rho=0} \\
\hline
{Student-t} & {C_t (u_1,u_2;\rho,d)=t_{\rho,d}(t^{-1}_d(u_1),t^{-1}_d(u_2))} & {\rho\in(-1,1)} & {\rho=1,or -1} & {\rho=0} \\
\hline
{Gumbel} & {C_{Gumbel} (u_1,u_2,\beta)=exp\{-[(-ln(u_1))^{\frac{1}{\beta}}+(-ln(u_2))^{\frac{1}{\beta}}]^{\beta}\}} & {\beta\in(0,1)} & {\beta=0} & {\beta=1} \\
\hline
{RG} & {C_{RG} (u_1,u_2,\alpha)=u_1+u_2-1+C_{Gumbel} (1-u_1,1-u_2,\alpha)} & {\alpha\in[1,\infty)} & {\alpha\rightarrow\infty} & {\alpha=1} \\
\hline
{Clayton} & {C_{Clayton} (u_1,u_2,\theta)=max((u_1^{-\theta}+u_2^{-\theta}-1)^{-\frac{1}{\theta}},0)} & {\theta\in[-1,\infty)\{0\}} & {\theta\rightarrow\infty} & {\theta \rightarrow 0} \\
\hline
{RC} & {C_{RC} (u_1,u_2,\alpha)=u_1+u_2-1+C_{Clayton} (1-u_1,1-u_2,\theta)} & {\theta\in[-1,\infty)\{0\}} & {\theta\rightarrow\infty} & {\theta \rightarrow 0} \\
\hline
\end{array}
$$
and it looks like this:
It's not bad until I found it's not easy to add footnotes since I used to make kable.
Is there any way I can add a footnote for it? or how can I convert it into a table so I can use kable to display it?

Latex - table not showing

I have seen several discussions on this board treating the topic but none of the solutions seem to work. I use R stargazer and have downloaded a table under Latex format, but when I copy paste the TeX code in my Latexfile, it does not show up in the PDF. I have also tried to use \input{myfile} with the table but the results is the same. Please find below a shorter version of the table. Does anybody have a solution ?
\documentclass[12pt]{article}
\begin{document}
\begin{table}[!htbp] \centering
\caption{}
\label{}
\begin{tabular}{#{\extracolsep{5pt}}lcccccccc}
\\[-1.8ex]\hline
\hline \\[-1.8ex]
Statistic & \multicolumn{1}{c}{N} & \multicolumn{1}{c}{Mean} & \multicolumn{1}{c}{St. Dev.} & \multicolumn{1}{c}{Min} & \multicolumn{1}{c}{Pctl(25)} & \multicolumn{1}{c}{Median} & \multicolumn{1}{c}{Pctl(75)} & \multicolumn{1}{c}{Max} \\
\hline \\[-1.8ex]
total\_asset & 10 & 30 & 40 & 50 & 60 & 50.000 & 5,556.000 \\
EBITDA\_margin & 6 & $-$5 & 12 & $-$1,250.000 & $-$0.085 & 0.1 & 0.033 & 42,500.000 \\
\hline \\[-1.8ex]
\end{tabular}
\end{table}
\end{document}
Kind regards,

rmarkdown matrices not functioning as expected or seen in rstudio

I am trying create a blogdown post using Rmarkdown but the matrix I am trying to create is not giving the correct output. In Rstudio it looks like the image below, however when I run the rmarkdown I get a different result.
Gives output in blogdown:
Rmarkdown code:
$$
\left(\begin{array}{cccc}
\text{var } & ... & -1 \\
\text{cov } & ... & ...\\
\text{mat } & ... & -1 \\
... & ... & -1\\
1 & ... & 0
\end{array}\right)
\left(\begin{array}{c}
w_{1} \\
... \\
... \\
... \\
w_{k} \\
\lambda
\end{array}\right)
=
\left(\begin{array}{c}
0 \\
... \\
... \\
... \\
0 \\
1
\end{array}\right)
$$
Any ideas on where I am going wrong and how I can make a matrix equation be on the same line as in the RStudio output?
The empty lines are breaking things. From https://tex.stackexchange.com/q/230338
An empty line in TeX should never be used just to arrange the source. It is an instruction exactly the same as the command \par which ends a paragraph and starts TeX's line breaking algorithm to split a paragraph into lines.
So it is not allowed in display math and it should never be used on the line before display math either, in that position it does not generate an error but forces TeX to take recovery action that produces a spurious white paragraph on the line above the display.
David Carlisle, Feb 26 '15 at 21:29
While it might not seem obvious that a LaTeX limitation would impose itself on an HTML target, the rendering uses MathJax (javascript library) that renders LaTeX math mode.
If you remove them, it seems to render correctly.
---
title: Quux
output: html_document
---
$$
\left(\begin{array}{cccc}
\text{var } & ... & -1 \\
\text{cov } & ... & ...\\
\text{mat } & ... & -1 \\
... & ... & -1\\
1 & ... & 0
\end{array}\right)
\left(\begin{array}{c}
w_{1} \\
... \\
... \\
... \\
w_{k} \\
\lambda
\end{array}\right)
=
\left(\begin{array}{c}
0 \\
... \\
... \\
... \\
0 \\
1
\end{array}\right)
$$
It also works with output: pdf_document:
R.version
# _
# platform x86_64-w64-mingw32
# arch x86_64
# os mingw32
# system x86_64, mingw32
# status
# major 3
# minor 5.3
# year 2019
# month 03
# day 11
# svn rev 76217
# language R
# version.string R version 3.5.3 (2019-03-11)
# nickname Great Truth
packageVersion("knitr")
# [1] '1.23'
packageVersion("rmarkdown")
# [1] '1.13'

Knitr chunk plots within a latex table - size issues

I am writing a report within R studio using the knitr package. I am trying to succinctly describe a list of variables and there inclusion in an analysis. In order to do this I am using the long table package within latex. I have inserted a chunk into one of the table columns in order to display a histogram of some of the data. The issue is that this disrupts the width of the table column. I have tried setting the table width within the latex code but something is happening within the chunk that is over riding this.
\documentclass{article}
\usepackage{float}
\usepackage{longtable}
\begin{document}
<<ch-req, echo=FALSE,results='hide',include=FALSE>>=
require(datasets)
#
\begin{center}
\begin{longtable}{|c|p{.45\textwidth}|c|}
\caption{Summary table for variables in 'cars' data set} \label{tab:csumTab} \\
\hline \multicolumn{1}{|c|}{\textbf{Variable Original Name}} & \multicolumn{1}{c|}{\textbf{Variable Summary}} & \multicolumn{1}{c|}{\textbf{Renamed Variable}} \\ \hline\hline
\endfirsthead
\multicolumn{3}{c}%
{{\bfseries \tablename\ \thetable{} -- continued from previous page}} \\
\hline \multicolumn{1}{|c|}{\textbf{Variable Original Name}} &
\multicolumn{1}{c|}{\textbf{Variable Summary}} &
\multicolumn{1}{c|}{\textbf{Renamed Variable}} \\ \hline
\endhead
\hline \multicolumn{3}{r}{{Continued on next page}} \\
\endfoot
\hline \hline
\endlastfoot
`speed' & numeric Speed (mph) of the car & \\
&
\begin{figure}[H]
<<histspeed1,echo=FALSE, fig=TRUE,out.width='2in', fig.align = 'left'>>=
hist(cars$speed)
#
\end{figure}
& \\
\hline
`dist' & This is the numeric stopping distance of the car (ft)& `Distance'\\
& \begin{tabular}{|c|c|c|}
\hline
min & median & max \\
\hline
2 & 36 & 120 \\
\hline
\end{tabular}& \\
\end{longtable}
\end{center}
\begin{center}
\begin{longtable}{|c|p{.45\textwidth}|c|}
\caption{Summary table for variables in `cars' data set} \label{tab:csumTab} \\
\hline \multicolumn{1}{|c|}{\textbf{Variable Original Name}} & \multicolumn{1}{c|}{\textbf{Variable Summary}} & \multicolumn{1}{c|}{\textbf{Renamed Variable}} \\ \hline\hline
\endfirsthead
\multicolumn{3}{c}%
{{\bfseries \tablename\ \thetable{} -- continued from previous page}} \\
\hline \multicolumn{1}{|c|}{\textbf{Variable Original Name}} &
\multicolumn{1}{c|}{\textbf{Variable Summary}} &
\multicolumn{1}{c|}{\textbf{Renamed Variable}} \\ \hline
\endhead
\hline \multicolumn{3}{r}{{Continued on next page}} \\
\endfoot
\hline \hline
\endlastfoot
`speed' & numeric Speed (mph) of the car & \\
&
\begin{figure}[H]
%<<histspeed2,echo=FALSE, fig=TRUE,out.width='2in', fig.align = 'left'>>=
%hist(cars$speed)
%#
\end{figure}
& \\
\hline
`dist' & This is the numeric stopping distance of the car (ft)& `Distance'\\
& \begin{tabular}{|c|c|c|}
\hline
min & median & max \\
\hline
2 & 36 & 120 \\
\hline
\end{tabular}& \\
\end{longtable}
\end{center}
\end{document}
There are two tables just to show that the table size does work when the plot is not incorporated in the code. I have used out.width='2in' to change the size of the histogram in the table, but there is still a large area reserved for the plot by default(?). You can see this more clearly if you change fig.align = 'left' to fig.align = 'center'. I want the table to look how it does in the second example but with the plot incorporated.
Any help greatly appreciated.

Basic R - Outputting basic R correlation table -> LaTex or text

I am generating a correlation table with http://myowelt.blogspot.com/2008/04/beautiful-correlation-tables-in-r.html
I am not successful, however, in outputting the file to a usable LaTex file or text file.
I have been unsuccessful using sink() to save the data to a text file.
Suppose I am using the following command:
corstarsl(lpp_axis1)
How would I pipe the output to a text file? I've read the documentation on sink and I'm missing a step somewhere. (I open the connection, execute the command, unlink the file and then I find nothing.)
I've also tried using the output from xtable(cortstarsl(lpp_axis1)) in a tex file yet I receive a "element table not found error. I do not know enough about Tex to track the source of the problem.
Suggestions for outputting this data?
Other suggestions for creating a correlation table?
Using the code from the web page you link to, I get (with the built in airquality data):
> require(Hmisc)
> require(xtable)
> xtable(corstarsl(airquality))
% latex table generated in R 2.12.1 by xtable 1.5-6 package
% Mon Feb 21 20:00:34 2011
\begin{table}[ht]
\begin{center}
\begin{tabular}{rlllll}
\hline
& ozone & solar.r & wind & temp & month \\
\hline
ozone & & & & & \\
solar.r & 0.35*** & & & & \\
wind & -0.60*** & -0.06 & & & \\
temp & 0.70*** & 0.28*** & -0.46*** & & \\
month & 0.16 & -0.08 & -0.18* & 0.42*** & \\
day & -0.01 & -0.15 & 0.03 & -0.13 & -0.01 \\
\hline
\end{tabular}
\end{center}
\end{table}
So the question then is how to get this TeX output to a file. Here capture.output() is one friend:
> capture.output(xtable(corstarsl(airquality)), file = "mytable.tex")
Which outputs the code to file named mytable.tex:
$ cat mytable.tex
% latex table generated in R 2.12.1 by xtable 1.5-6 package
% Mon Feb 21 20:01:03 2011
\begin{table}[ht]
\begin{center}
\begin{tabular}{rlllll}
\hline
& ozone & solar.r & wind & temp & month \\
\hline
ozone & & & & & \\
solar.r & 0.35*** & & & & \\
wind & -0.60*** & -0.06 & & & \\
temp & 0.70*** & 0.28*** & -0.46*** & & \\
month & 0.16 & -0.08 & -0.18* & 0.42*** & \\
day & -0.01 & -0.15 & 0.03 & -0.13 & -0.01 \\
\hline
\end{tabular}
\ end{center}
\end{table}
For "plain" delimited text output, perhaps to dump into a word processor or Spreadsheet, try write.table(), eg:
> write.table(corstarsl(airquality), file = "mytable2.txt")
Which results in a file like this:
$ cat mytable2.txt
"ozone" "solar.r" "wind" "temp" "month"
"ozone" "" "" "" "" ""
"solar.r" " 0.35***" "" "" "" ""
"wind" "-0.60***" "-0.06 " "" "" ""
"temp" " 0.70***" " 0.28***" "-0.46***" "" ""
"month" " 0.16 " "-0.08 " "-0.18* " " 0.42***" ""
"day" "-0.01 " "-0.15 " " 0.03 " "-0.13 " "-0.01 "
You can alter the quoting and delimiter to your heart's content - see ?write.table.

Resources