I am have problems tyring to input a .Rtex file into my shareLatex project using the /input{file} or include{file} commands.
Is there a way to include this as you can do with other .tex files?
The file looks like this :
<<>>=
# Create a sequence of numbers
X = 2:10
# Display basic statistical measures
summary(X)
#
The code is compiled if I put it in the main.Rtex file, but not if I try to include it. This questions is similar to the one on tex.stackexchange.
The following works in Overleaf (main file named *.Rtex).
\documentclass{standalone}
\begin{filecontents*}{tmp999.Rtex}
<<>>=
rnorm(3)
#
\end{filecontents*}
\begin{document}
\input{tmp999}
\end{document}
Instead of \input or \include, you have to use knitr's knit_child function. For this to work, both, the main file and the file containing the R code must have the .Rtex file extension. Other files can keep .tex. For your example, this would look like:
main.Rtex
\documentclass[]{article}
\begin{document}
\Sexpr{knit_child('content/knitr-child.Rtex')}
\end{document}
content/knitr-child.Rtex
<<>>=
# Create a sequence of numbers
X = 2:10
# Display basic statistical measures
summary(X)
#
I posted a similar answer to your linked question on tex.stackexchange.com.
Related
I just found this awesome technique to put the code used in the .Rmd file in the appendix (of that same file).
However, I am using R Sweave and not R Markdown and I would like to know if there exists a similar way to put all the code at the end in a unique chunk. The code to do that in Markdown does not work in Sweave. I precise that, unlike this post, I do not have a separate .R file where the calculations are made. Everything is done in the .Rnw file.
Does anybody know how to do it?
Edit : a reproducible example
\documentclass[11pt, twocolumn]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\begin{document}
\SweaveOpts{concordance=TRUE}
<<reg2, echo=FALSE, print=FALSE>>=
head(mtcars)
#
<<reg3, echo=FALSE, print=FALSE>>=
head(iris)
#
\section*{Appendix}
% the place where I could like to put the whole code
\end{document}
This chunk works to include the code:
<<echo=FALSE, eval=TRUE>>=
filename <- tempfile(fileext=".R")
Stangle("test.Rnw", output = filename, quiet = TRUE)
cat(readLines(filename), sep = "\n")
#
When I include that in your example file, I see this:
I think it's possible to modify the format a bit; see ?Rtangle for some details. Similar things are possible with knitr, but it's more flexible. I suspect the best method would be similar to the one you found for RMarkdown.
I want to include help of R functions into .Rnw document using help_console function from noamtools package which can be installed from github using the following comand:
devtools::install_github('noamross/noamtools', build_vignettes = TRUE)
Minimum working example is given below:
\documentclass{article}
\begin{document}
<< label=Test, echo=FALSE >>=
library(noamtools)
# help_console(topic="mean", format = "latex")
help_console(topic="mean", format = "Rd")
#
\end{document}
I'm not getting the correct output.
The solution is to include the line
\usepackage{Rd}
in the beginning of the .Rnw file and then use
help_console(topic="mean", format="latex")
For example, put the following in a file, say test.Rnw and compile to produce documentation about the function mean. Note that results=tex ensures that the Latex code produced by R is actually created like Latex code.
% devtools::install_github('noamross/noamtools', build_vignettes = TRUE)
\documentclass{book}
\usepackage{Rd} % Rstudio will be able to find this .sty file
\begin{document}
<<label=Test, results=tex>>=
library(noamtools)
help_console(topic="mean", format = "latex")
#
\end{document}
Which produces a pdf which looks like this,
Looking here helped me realize that the Rd.sty file needed to be included. Running the following code
pack <- "ggplot2"
path <- find.package(pack)
system(paste(shQuote(file.path(R.home("bin"), "R")),
"CMD", "Rd2pdf --no-clean", shQuote(path)))
# .Rd2xxxx directory created
# cd .Rd2xxxx
You can look inside the created directory named something like .Rd2xxxx to find Rd2.tex which shows what is put in the Latex documentation files which R creates.
Since I am a newbie as far as knitr is concerned, I am reading and modifying the examples given in knitr web site. One the approaches that caught my attention is to call chunks of an R script within an Rmw file. After compiling and modifying several examples, I wonder whether one can set a variable on an Rmw file and pass it to an R-script.
Here is an example
\documentclass{article}
\begin{document}
<<set-options, echo=FALSE, cache=FALSE>>=
options(replace.assign=TRUE)
opts_chunk$set(cache=TRUE, fig.show='asis')
read_chunk('simple_example.R')
#
\title{Example}
\author{Somebody}
\maketitle
\section{Print variable}
<<Print-data, echo=TRUE>>=
inp=2
#
\end{document}
and
# Simple Example
## ---- Print-data ----
inp=inp+2
print(inp)
The output result is ip=2 and an error msg "object inp not found".
Many thanks
The code chunk Print-data overrode the one in the Rnw file, so inp=2 was not executed, hence the error.
It seem you want to embed the chunk after Print-data:
<<>>=
inp=2
<<Print-data>>
#
I have an Rnw file (a large one) and I want to show all code used in an appendix.
It is suggested in some of the knitr examples (https://github.com/yihui/knitr-examples/blob/master/073-code-appendix.Rnw, also a good MWE) that having a code block like this is the way:
<<Rcode, eval=FALSE, ref.label=all_labels()[-1],echo=TRUE, cache=FALSE>>=
#
This works fine except all the code chunks merge into each other and none are labelled.
On the other hand if I run purl(myfile.Rnw) it labels the code chunks and separates them by two lines, which makes things much easier to read.
Is there any way of automatically listing the code using the second approach in a report appendix? I know I can have a code chunk to run purl to produce myfile.R as part of my report, but how do I then show the code in myfile.R in my appendix?
Here's an example .Rnw file (called "example.rnw"):
\documentclass{article}
\begin{document}
<<a>>=
x <- 1:10
x
#
<<b>>=
y <- 10:1
y
#
<<c>>=
z <- 1:5
z
#
\clearpage
\input{example-purl.tex}
\end{document}
If you create a file in your working directory called "template.rnw" that just contains:
<<%sCHUNK_LABEL_HERE, eval=FALSE>>=
#
Then, you run:
stitch(purl("example.rnw",output="example-purl.r"),template="template.rnw")
knit("example.rnw")
Does that make sense? Basically, you're purling, stitching the purled code, knitting the original document, and then compiling the resulting LaTeX ("example.tex") that includes the knitting and purling. Everything should be formatted nicely (and consistently).
I am new to R and much newer to Sweave and I am experimenting with graphics. Here is a sample code:
\documentclass[a4paper]{article}
\usepackage{Sweave} %%%%%%
\SweaveOpts{eps=true}
\begin{document}
<<echo=FALSE>>=
test.frame<-data.frame(year=8:11, value= c(12050,15292,23907,33991))
#
\SweaveOpts{prefix.string=Evolution}
<<label=amount,echo=FALSE,results=hide>>=
postscript('doudou.eps',
width=6, height=7,
colormodel="cmyk",
family = "ComputerModern")
with(test.frame,plot(year, value))
dev.off()
#
\begin{figure}[htbp]
\begin{center}
\includegraphics[width=1\textwidth,angle=90]{doudou.eps}
\end{center}
\end{figure}
\end{document}
What I want to do above?
To have manual control over the EPS file that I am inserting so that I can have the \includegraphicscommand in the Sweave file itself.
And I am trying to give a proper file name to the figure: with prefix Evolution and label amount such that the EPS figure produced will be named Evolution-amount.eps.
What is going wrong?
As you can see, I am inserting a file name in the R postscript option i.e. doudou.eps . If I don't do this a file named Rplots.ps is created by R.
So my code is well ignoring the prefix and label that I want to give to my figure file.
And I am explicitly asking later on by \includegraphics to put doudou.eps.
How I want it to be?
To be able to have prefix and label as I mentioned above in the figure file name, while I still have manual control over the \includegraphics command in the Sweave file. Is this possible?
What is the use of this?
Say I am writing a paper and I have figures in different sections. So it would be nice to have something like:
\SweaveOpts{prefix.string=Paper2}
<<label=section2,echo=FALSE,results=hide>>=
and for example I specify in the postscript option: model.eps.
Then the figure would be named Paper2-section2.model.eps for example. Is that feasible?
And I would need to put this name somehow manually ?? in the \includegraphics command that follows.
Thanks a lot...
Update: 09 dec 2011.
A close solution with the help of cbeleites is:
\documentclass[fleqn, a4paper,12pt]{article}
\usepackage[latin1]{inputenx}
\usepackage[T1]{fontenc}
\usepackage{Sweave} %%%%%%
\SweaveOpts{eps=TRUE}
\begin{document}
<<echo=FALSE>>=
test.frame<-data.frame(year=8:11, value= c(12050,15292,23907,33991))
#
\SweaveOpts{prefix.string=Paper2}
<<label=section2, echo=FALSE,results=hide>>=
ps.options ( width=6, height=7, colormodel="cmyk", family = "ComputerModern")
#
<<label=section2, fig=TRUE, include = TRUE, echo=FALSE>>=
with(test.frame,plot(year, value))
#
\end{document}
On compilation I get the EPS and PDF files named Paper2-section2. This is as close as we can get I think.
You need to use a figure chunk.
<<fig=TRUE, include = FALSE>>=
with(test.frame,plot(year, value))
#
You can pass more options to the figure chunk, such as width and height.
for more advanced options, use R's postscript options:
<<>>=
ps.options (colormodel="cmyk", family = "ComputerModern")
#
if the options apply only to one figure, you'd probably be easier off without figure chunk and with manual \includegraphics. You could give the filename to the ps file as if it was produced by a Sweave figure chunk, though.
If the only reason for \includegraphics is that you want to control its options, have a look at graphicx' Gin:
\begin{figure}[htbp]
\begin{center}
\setkeys{Gin}{width=\linewidth}
<<fig=TRUE>>=
with(test.frame,plot(year, value))
#
\end{center}
\end{figure}
I thought Gin would work with the rotation as well, but it doesn't. see https://tex.stackexchange.com/a/31013 for explanation and possible solution. However, if the rotation is only because the figures appear sideways (as opposed to you wanting them to be sideways) this may be solved with correct ps settings in R (RSiteSearch () and ? postscript should help).