Sweaving #-Symbol - r

How can I Sweave text with a #-sign?
Here, Sweaving and compiling is no problem:
<<echo=false,results=tex>>
cat("This is my text\n")
#
But here, compiling gives an error:
<<echo=false,results=tex>>
cat("This is #my text\n")
#
The hash sign could not be compiled in tex. I have to set the Sweave option to "tex" because I want to print different text files in a loop and each text file should be separated by a new chapter.
<<results=tex>>
for(i in d){
tx <- readLines(i)
cat(tx, sep="\n")
\chapter{i}
}
Thanks for every hint.
TIM

You have to escape the hash-sign, since it has in LaTeX a special meaning.
\documentclass{article}
\begin{document}
\SweaveOpts{concordance=TRUE}
<<echo=false,results=tex>>=
cat("This is \\#my text\n")
#
\end{document}

Related

Sweave syntax highlighting in output - revisit

My question is the same as the one in the link Sweave syntax highlighting in output.
I have followed the suggestion by daroczig https://tex.stackexchange.com/questions/5113/how-to-colorize-syntax-using-r-sweave, and created the following test.Rnw file:
\documentclass{article}
\RequirePackage{fancyvrb}
\RequirePackage{listings}
\SweaveOpts{keep.source=TRUE}
<<SweaveListingsPreparations, results=tex, echo=FALSE, strip.white=false>>=
require(SweaveListingUtils)
SweaveListingPreparations()
setToBeDefinedPkgs(pkgs = c("SweaveListingUtils","distr"),
keywordstyles = c("\\bf\\color{blue}","\\bf\\color{red}"))
#
\begin{document}
\SweaveOpts{concordance=TRUE}
\section{Example}
This is an example with three variables in R.
<<>>=
options(continue = " ")
x <- 10
t <- 'The brown fox'
b <- TRUE
#
<<>>=
x
#
<<>>=
t
#
<<>>=
b
#
\end{document}
<<cleaup, echo=FALSE>>=
unloadNamespace("SweaveListingUtils")
#
However, I get the following error message:
Suggestions?
Since you don't have \usepackage{Sweave} in your document, Sweave will insert it. But the SweaveListingsUtils package also inserts conflicting code.
So you need to follow the advice in the SweaveListingsUtils documentation, and put in a comment
% \usepackage{Sweave}
near the start of your document. This will prevent R from inserting it.
However, do note that support for SweaveListingsUtils is ending; you are better off using knitr.

Conditionally print text or show graph in beamer presentation

I have some difficulties implementing a (beamer) presentation. Everything works fine until I include a function which checks a specific condition and accordingly returns the output (graph - print text). Without that function it works fine. So how can I either graph or print the output?
\documentclass[10pt]{beamer}
\usepackage[T1]{fontenc}
\begin{document}
\begin{frame}{test}
<<echo=FALSE, fig.height = 4>>=
dates <- seq(as.Date("2015-02-13"), as.Date("2015-02-22"), by = "days")
b <- c(1,1,1,1,2,2,3,3,3,0)
c <- c(20,30,26,20,30,40,5,10,4,0)
d <- c(11,2233,12,2,22,13,23,23,100,0)
df <- data.frame(dates,b,c,d)
plot(df)
test <- function(df) {
if(sum(tail(df[2:ncol(df)], 1)) > 0) { # check only last date
return(plot(df))
} else {
print("Have a nice day!")
}
}
test(df)
#
\end{frame}
\end{document}
knitr wraps output in verbatim as can be seen from the TEX that the Rnw in the question produces:
\begin{frame}{test}
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{verbatim}
## [1] "Have a nice day!"
\end{verbatim}
\end{kframe}
\includegraphics[width=10cm,height=8cm]{figure/unnamed-chunk-2-1}
\end{knitrout}
\end{frame}
However:
It is straightforward to use Sweave or knitr with beamer; the only thing you need to be careful of is you have to add the fragile option to the frames that contain verbatim code. [Source]
Therefore, the frame needs the fragile option:
\begin{frame}[fragile]{test}
With fragile make sure not to indent \end{frame}. (This happened to me after copying the code from the question …)

Remove brackets in Sweave output

I want to know if there is a way to print R object directly in a text (Sweave), but directly in a line of text.
I'd like to print this code
\documentclass{article}
\begin{document}
\SweaveOpts{concordance=TRUE}
YO MANNNNNNN 2+2 =
<<here, results=tex, echo=FALSE>>=
2+2
#
YEAHHHH!
\end{document}
And I want an output similar to this:
YO MANNNNNNN 2+2 = 4 YEAHHHH!
But, R is adding the answer brackets like this
YO MANNNNNNN 2+2 = [1] 4YEAHHHH!
Code:
\documentclass{article}
\begin{document}
% \SweaveOpts{concordance=TRUE}
YO MANNNNNNN 2+2 = \Sexpr{2+2} YEAHHHH!
\end{document}
n.b. For me commenting out \SweaveOpts{concordance=TRUE} worked just fine, but you made need to uncomment depending on your setup.
Prints:
YO MANNNNNNN 2+2 = 4 YEAHHHH!
Update - extra info per comment...
You can define an object within a 'chunk' to be later evaluated in line using \Sexpr{}
<<somechunk, results='tex',echo=FALSE, results='hide'>>=
x <- 2
#
Inline evaluation of x prints the number \Sexpr{x}.

Why doesn't knitr allow directional assignment? [duplicate]

I am having trouble when using "reverse" assignment operators (->) in a knitr .Rnw file. For example, I have the following simple .Rnw file
\documentclass{article}
\begin{document}
<<test>>=
options(tidy=FALSE, width=50)
1:5 -> a
#
\end{document}
When I use knitr to compile into a pdf, the operator -> has been reversed so the output actually has
1:5 <- a
in it!
how can I change this?
Make tidy=FALSE a knitr chunk option rather than an R option:
\documentclass{article}
\begin{document}
<<test,tidy=FALSE>>=
options(tidy=FALSE, width=50)
1:5 -> a
#
\end{document}
(I don't think tidy=FALSE does anything at all in options(), but I guess it's harmless ...)
For setting tidy=FALSE on a chunk-by-chunk basis, Ben's answer has got you covered.
To reset the option globally, use opts_chunk$set(), like so:
\documentclass{article}
\begin{document}
<<setup, include=FALSE, cache=FALSE>>=
opts_chunk$set(tidy=FALSE)
#
<<test>>=
1:5 -> a
#
\end{document}
Additionally, as documented here, tidy.opts can give you finer-grained control over many aspects of the knitr's (and ultimately formatR::tidy.source()'s) tidying behavior. Perhaps unfortunately in this case, while you can tell knitr not to replace "=" with "<-" (by doing opts_chunk$set(tidy.opts=list(replace.assign=FALSE))you cannot use that option to control whether "->" is replaced by "<-".
Here's an example that uses tidy.opts
\documentclass{article}
\begin{document}
<<setup, include=FALSE, cache=FALSE>>=
opts_chunk$set(tidy.opts=list(replace.assign=FALSE))
#
<<test>>=
j <- function(x) { x<-y ## x<-y will be printed on new line, with added inter-token spaces
a = 1:5 ## will be indented, but "=" won't be replaced
} ## closing brace will be moved to start of line
#
\end{document}

knitr changing my reverse assignment operator -> to <-

I am having trouble when using "reverse" assignment operators (->) in a knitr .Rnw file. For example, I have the following simple .Rnw file
\documentclass{article}
\begin{document}
<<test>>=
options(tidy=FALSE, width=50)
1:5 -> a
#
\end{document}
When I use knitr to compile into a pdf, the operator -> has been reversed so the output actually has
1:5 <- a
in it!
how can I change this?
Make tidy=FALSE a knitr chunk option rather than an R option:
\documentclass{article}
\begin{document}
<<test,tidy=FALSE>>=
options(tidy=FALSE, width=50)
1:5 -> a
#
\end{document}
(I don't think tidy=FALSE does anything at all in options(), but I guess it's harmless ...)
For setting tidy=FALSE on a chunk-by-chunk basis, Ben's answer has got you covered.
To reset the option globally, use opts_chunk$set(), like so:
\documentclass{article}
\begin{document}
<<setup, include=FALSE, cache=FALSE>>=
opts_chunk$set(tidy=FALSE)
#
<<test>>=
1:5 -> a
#
\end{document}
Additionally, as documented here, tidy.opts can give you finer-grained control over many aspects of the knitr's (and ultimately formatR::tidy.source()'s) tidying behavior. Perhaps unfortunately in this case, while you can tell knitr not to replace "=" with "<-" (by doing opts_chunk$set(tidy.opts=list(replace.assign=FALSE))you cannot use that option to control whether "->" is replaced by "<-".
Here's an example that uses tidy.opts
\documentclass{article}
\begin{document}
<<setup, include=FALSE, cache=FALSE>>=
opts_chunk$set(tidy.opts=list(replace.assign=FALSE))
#
<<test>>=
j <- function(x) { x<-y ## x<-y will be printed on new line, with added inter-token spaces
a = 1:5 ## will be indented, but "=" won't be replaced
} ## closing brace will be moved to start of line
#
\end{document}

Resources