Your problem with listings
can be solved in this way:
Example1.tex
:
\documentclass{article}
\usepackage{listings}
\lstset{
inputencoding=utf8,
extendedchars=true,
literate={‘}{{`}}1 {’}{{'}}1,}
\begin{document}
\lstinputlisting{script.R}
\end{document}
But as there are not any syntax code in script.R, probably is better just use verbatim
:
Example2.tex
:
\documentclass{article}
\usepackage{verbatim}
\begin{document}
\verbatiminput{script.R}
\end{document}
Said that, do not include externally saved R outputs. Use R to produce that output from the document itself with knitr
. The example below show some approaches to show parts of a regression model summary.
The code is a bit complex mainly due to the added option to cut the raw summary as you do manually. Without output.lines
option, show the summary is fairly simple.
The other alternative is extract values this format and format these elements in your own LaTeX summary. The main hack here could be if you need the asteriks, that are not in the matrix of coefficients.
But anyway, all the outputs will be updated automatically if you change the source data, while using a saved outputs, or copying and pasting ...
Example3.Rnw
(if you do not know how compile this file, use Rstudio > Compile PDF).
\documentclass{article}
\usepackage[margin=1cm]{geometry}
\usepackage{booktabs,parskip,tabto}
\begin{document}
\section*{Sample data}
<<xxx,echo=F>>=
# Sample data and model regression
var <- c(1,2,3,4,5,6,7,8,9)
Besuche <- c(111,231,328,450,541,613,711,815,919)
mod <- lm(var~Besuche)
@
var = \Sexpr{var}\par Besuche = \Sexpr{Besuche}
\section*{Raw}
% https://stackoverflow.com/a/23205752/2803235
<<output,echo=F>>=
library(knitr)
hook_output <- knit_hooks$get("output")
knit_hooks$set(output = function(x, options) {
lines <- options$output.lines
if (is.null(lines)) {
return(hook_output(x, options)) # pass to default hook
}
x <- unlist(strsplit(x, "\n"))
more <- " "
if (length(lines)==1) { # first n lines
if (length(x) > lines) {
# truncate the output, but add ....
x <- c(head(x, lines), more)
}
} else {
x <- c(more, x[lines], more)
}
# paste these lines together
x <- paste(c(x, ""), collapse = "\n")
hook_output(x, options)
})
@
<<zzz,echo=F,output.lines=-(1:8),comment=" ">>=
y <- summary(mod)
y
@
\section*{Cooked}
Coefficients:
<<test,echo=F,results='asis'>>=
library(xtable)
options(xtable.floating = FALSE)
DF <- read.table(textConnection(#
capture.output(summary(mod))[11:12]), fill = T)
names(DF) <- c(" ", colnames(coef(summary(mod))), "Signif.")
print(xtable(DF,digits=c(0,0,3,3,3,2,0),display=c("s","s","g","g","g","g","s")), math.style.exponents=T, include.rownames = F, booktabs = T)
@
<<buklreport,echo=F,output.lines=-(1:12),results="asis">>=
y
@
\section*{Floating version}
See the table \ref{float}.
\begin{table}[hb!]
\centering
\caption{Regression coefficients.}\label{float}
<<test2,echo=F,results="asis">>=
print(xtable(y), booktabs = T)
@
\begin{tabular}{ll}
Residual standard error: & \Sexpr{round(y$sigma,4)}
on \Sexpr{y$df[2]} degrees of freedom.\\
Multiple R$^2$: & \Sexpr{round(y$r.squared,4)}.\\
Adjusted R$^2$: & \Sexpr{round(y$adj.r.squared,4)}\\
F-statistic: & \Sexpr{round(y$fstatistic[1],0)} on
\Sexpr{y$fstatistic[2]} and
\Sexpr{y$fstatistic[3]} DF.\\
p-value: & \Sexpr{signif(y$coef[,4][2],4)} \\
\end{tabular}
\end{table}
\end{document}