R markdown: Using R code within latex table - r

I have a finished latex table like this one loaded into r markdown.
\begin{table}[!h]
\centering
\begin{tabular}{|c|c|}
\hline
$\textbf{Details}$ & \\
\midrule[1pt]
\specialrule{2pt}{-2pt}{-2pt}
\specialrule{2pt}{0pt}{0.5pt}
Name der Sitzung & $A$
\\ \hline
erstellt am & $XXXXXX$ \\ \hline
erstellt von & $x$ \\ \hline
Zeitpunkt Auswertung & $x$ \\ \hline
Anzahl Teilnehmer & $x$ \\ \hline
Anzahl Stimmen & $x$ \\ \hline
Anzahl Fragen & $x$ \\ \hline
\end{tabular}
\end{table}
The part $XXXXX$ is supposed to be replaced by the following R code: DATA$TABLE[[1]]$a[[1]].
How can i do that?
When I plug in
´´´{r}
Table[[1]]$a[[1]]
´´´
for $XXXXX$, I get an error message:
output file: dynmaicMarkdown.knit.md
! You can't use `macro parameter character #' in math mode.
l.124 #
# NULL
Fehler: LaTeX failed to compile dynmaicMarkdown.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips. See dynmaicMarkdown.log for more info.
Zusätzlich: Warnmeldung:
In has_crop_tools() :
Tool(s) not installed or not in PATH: pdfcrop, ghostscript
-> As a result, figure cropping will be disabled.
Ausführung angehalten

Related

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,

Stargazer not publishing regression table

The packages I currently have downloaded are: tinytex, stargazer, and rmarkdown.
I have used this function to create a publishable table:
stargazer(EB734_May_2010_model_u)
But got this instead:
% Table created by stargazer v.5.2.2 by Marek Hlavac, Harvard University. E-mail: hlavac at fas.harvard.edu
% Date and time: Wed, Sep 02, 2020 - 09:30:46
\begin{table}[!htbp] \centering
\caption{}
\label{}
\begin{tabular}{#{\extracolsep{5pt}}lc}
\\[-1.8ex]\hline
\hline \\[-1.8ex]
& \multicolumn{1}{c}{\textit{Dependent variable:}} \\
\cline{2-2}
\\[-1.8ex] & trust \\
\hline \\[-1.8ex]
D\_economy2 & $-$0.220$^{*}$ \\
& (0.119) \\
& \\
D\_economy0 & 0.243$^{**}$ \\
& (0.118) \\
& \\
E\_economy2 & $-$0.166 \\
& (0.112) \\
& \\
E\_economy0 & $-$0.168 \\
& (0.112) \\
& \\
Support\_EMU0 & 0.655$^{***}$ \\
& (0.027) \\
& \\
Constant & $-$2.038$^{***}$ \\
& (0.148) \\
& \\
\hline \\[-1.8ex]
Observations & 18,282 \\
Log Likelihood & $-$22,543.580 \\
Akaike Inf. Crit. & 45,141.170 \\
\hline
\hline \\[-1.8ex]
\textit{Note:} & \multicolumn{1}{r}{$^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01} \\
\end{tabular}
\end{table}
I need to publish the summary for my logistic regression model into word from R.
Any idea on how to fix this? Thanks. :)
The easiest way would be:
stargazer(EB734_May_2010_model_u, type = "text")
which will give you output in text form that you can use immediately.
Alternatively, if you want a good-looking HTML table that you can then paste into Word:
stargazer(EB734_May_2010_model_u, out = "output.html")
Don't worry if your Rmd shows a lot of code below as output, just go into your working directory after you run it, and look for a file called "output.html". You should be able to open that with Word, and then copy and paste into your document.

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.

R markdown won't compile a piece of LaTeX code

I have the following piece of code, which I would like to run I R markdown. I have tried to run it in a LaTeX compiler, which works absolutely fine. But when I add it into R markdown it won't compile the piece of code.
---
title: "Titre"
date: Fecha
output:
pdf_document:
keep_tex: true
includes:
in_header: mystyle.sty
---
\begin{table}[!htbp] \centering
\caption{}
\label{}
\begin{tabular}{#{\extracolsep{5pt}}lD{.}{.}{-3} }
\\[-1.8ex]\hline
\hline \\[-1.8ex]
& \multicolumn{1}{c}{\textit{Dependent variable:}} \\
\cline{2-2}
\\[-1.8ex] & \multicolumn{1}{c}{mpg} \\
\hline \\[-1.8ex]
cyl & -2.876^{***} \\
& (0.322) \\
& \\
Constant & 37.885^{***} \\
& (2.074) \\
& \\
\hline \\[-1.8ex]
Observations & \multicolumn{1}{c}{32} \\
R$^{2}$ & \multicolumn{1}{c}{0.726} \\
Adjusted R$^{2}$ & \multicolumn{1}{c}{0.717} \\
Residual Std. Error & \multicolumn{1}{c}{3.206 (df = 30)} \\
F Statistic & \multicolumn{1}{c}{79.561$^{***}$ (df = 1; 30)} \\
\hline
\hline \\[-1.8ex]
\textit{Note:} & \multicolumn{1}{r}{$^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01} \\
\end{tabular}
\end{table}
The file mystyle.sty contains the following
\usepackage{dcolumn}
\newcolumntype{d}[1]{D{.}{.}{#1}}
\usepackage{booktabs}
\newcommand{\mc}[1]{\multicolumn{1}{c}{#1}} % handy shortcut macro
\usepackage{verbatim}
Are there someone who can explain why R markdown can't compile what's above?
By adding the table code line by line and recompiling each time, I isolated the problem to the lines with carets, e.g. the line that reads cyl & -2.876^{***} \\.
I was able to compile the document by enclosing the superscripted part in two $ signs, i.e.
cyl & -2.876$$^{***}$$ \\
(I don't know why you need two, rather than just one, $.)
This compiles just fine:
---
title: "Titre"
date: Fecha
output:
pdf_document:
keep_tex: true
includes:
in_header: mystyle.sty
---
\begin{table}[!htbp] \centering
\begin{tabular}{#{\extracolsep{5pt}}lD{.}{.}{-3} }
\\[-1.8ex]\hline
\hline \\[-1.8ex]
& \multicolumn{1}{c}{\textit{Dependent variable:}} \\
\cline{2-2}
\\[-1.8ex] & \multicolumn{1}{c}{mpg} \\
\hline \\[-1.8ex]
cyl & -2.876$$^{***}$$ \\
& (0.322) \\
& \\
Constant & 37.885$$^{***}$$ \\
& (2.074) \\
& \\
\hline \\[-1.8ex]
Observations & \multicolumn{1}{c}{32} \\
R$^{2}$ & \multicolumn{1}{c}{0.726} \\
Adjusted R$^{2}$ & \multicolumn{1}{c}{0.717} \\
Residual Std. Error & \multicolumn{1}{c}{3.206 (df = 30)} \\
F Statistic & \multicolumn{1}{c}{79.561$^{***}$ (df = 1; 30)} \\
\hline
\hline \\[-1.8ex]
\textit{Note:} & \multicolumn{1}{r}{$^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01} \\
\end{tabular}
\end{table}

Resources