How to make the output recognizable in Sweave - r

I am using Sweave to be able to combine LaTeX and R. I find useful to be able to remove the prefix ">" and "+" from the input writing options(prompt=" ", continue=" ") .
I would like to find a way to make the output recognizable from the input, for instance adding a prefix "#" to the output (as it happens with R Markdown).
Any help?

I'm not sure if you know that knitr can also compile .Rnw documents (Sweave), in addition to .Rmd (R Markdown). You just pass the file path to knitr::knit(), e.g.,
knitr::knit('test.Rnw')
And you will get the .tex output file. If you want to get the PDF output directly, you may call:
knitr::knit2pdf('test.Rnw')
By default, knitr doesn't add the prompt character > or + to your code in the output.

Related

How to include output of help() in sweave pdf

I would like to include function documentation from the help file in a sweave document. I tried the following sweave block
<<>>=
?lm
#
but I get error messages when calling Sweave on the Rnw file. How can I include the entire help message in the document?
The key to this is really figuring out how to get the information you desire as a character string.
help("lm") opens up the help file for the relevant function, but not in the console.
utils:::.getHelpFile gives you the Rd version of that file.
From there, you can use tools:::Rd2txt to convert it to text...
Which can be "captured" using capture.output.
Those are essentially the steps contained in the first few lines of helpExtract from my "SOfun" package. That function, however, captures just the requested section.
Instead, if you can settle for just the text, you can do something along the lines of:
gsub("_\b", "",
capture.output(tools:::Rd2txt(
utils:::.getHelpFile(utils::help("lm")))))

How to insert a number from R to LaTeX?

I´m trying to insert a number directly from R into a LaTeX file. I have only been able to do it as a table and that doesn´t allow me to put it in the middle of a sentence.
I would like the output to look like this:
"The final number is [number directly from R]"
What should I do?
If you use the Sweave or knitr (assuming .Rnw file), use \Sexpr{foo}. Then run the file through R + Sweave or R + knitr and you'll have a LaTeX source with the value of foo inserted in place of \Sexpr{foo}.
Basically you need to markup your LaTeX source appropriately, run it through a system using R to identify the things you want to replace and insert the actual data into the source file, and output a LaTeX source which you can then compile.
There are other systems besides Sweave and Knitr so find a system you like that suits your workflows. For example, there is brew.

Converting Tex file generated by Knitr into .doc

Why is it important? My collaborators would like to be able to modify my .pdf reports and they are not able to modify the actual .tex code. They are sooo used to word !
How I write my reports? I use knitr. What a great and useful tool; thanks Yihui :)
What I'm looking for? convert the generated pdf report into .doc (my collaborators favorite !)
a solution might help: Pandoc (http://johnmacfarlane.net/pandoc/). As it's explained in the introduction of page of Pandoc, it can be used to convert LaTex to .docx and to markup. I then thought converting the .tex file of each of my reports (a .tex file is generated when I run my knitr .Rnw file) and convert it to .docx using pandoc:
pandoc -s myTexRepot.tex -o aDocReport.docx
issue: 1) Figures are missing in my .docx file. 2) I generate all my tables in my reports using xtable(), none of them can be converted! 3) Also, I lose all table of contents, section numbering.
Markup to Word: Issue (1) above is still an issue in converting Markup code to .doc
Is there any better idea to approach the issue of converting latex to doc. If Pandoc is the best, would you know how I can solve the issues above?
I really appreciate your help.
since you are already using knitr, you can call the rmarkdown:render() function to knit the document.
there is a parameter in render() called output_format[1]. the parameter along with the YAML header in your knitr document will allow you do generate the pdf and word document simultaneously
[1] output_format
R Markdown output format to convert to. Pass "all" to render all formats defined within the file. Pass the name of a format (e.g. "html_document") to render a single format or pass a vector of format names to render multiple formats. Alternatively you can pass an output format object; e.g. html_document(). If NULL is passed then the output format is the first one defined in the YAML metadata of the input file (defaulting to HTML if none is specified).

Hmisc tilde rowname

I am trying to group row names in a R data frame for typesetting with the Hmisc latex() function. Problem is that latex() adds two tilde characters before each row name, and these show up in my document.
How can I either remove these characters or have them not show up?
Example:
test.df <- data.frame(row.names=letters[1:4], col1=1:4, col2=4:1, col3=4:7)
latex(test.df, file="", n.rgroup=c(2,2), rgroup=c("First","Second"))
Edit:
The latex function occurs inside a knitr chunk. The resulting .Rnw file is compiled through the knit2pdf function, which uses pdfLatex by default, I think. All other tables/figures in the document compile fine, without any residual LaTex syntax showing up.
They will not show up if you use latex with a TeX processor:
test.df <- data.frame(row.names=letters[1:4], col1=1:4, col2=4:1, col3=4:7)
latex(test.df, file="test", n.rgroup=c(2,2), rgroup=c("First","Second"))
If you want to "capture" the text that is "printed" to the screen and remove the double tildes with sub then you probably need to use capture.output, because it appears that latex is not returning a value but is acting more like the cat function which has output to the screen as a side-effect:
out <- sub("^~~", "", capture.output(
latex(test.df, file="",
n.rgroup=c(2,2), rgroup=c("First","Second"))))
You could then use writeLines or cat with a file argument to send that text to a destination. I suppose it is possible that you could just put the sub call inline without diverting the results to a named object. That will depend on exactly how your are processing this text.
If you don't want to use LaTeX then i suggest either the ascii package that has pretty advanced options that do a nice raw text output (it also has the rgroup & n.rgroup options for grouping row names). If you are interested in getting the tables into a Word document (or just HTML) i suggest Markdown with my htmlTable function - the arguments are based upon the Hmisc latex arguments as I needed a replacement when I was switching to Markdown, thus all you need to do is change the function name to htmlTable after loading my package.

How can I save text to a file in R?

I have a R function which can generate the LaTeX code (the output is the LaTex code) by using cat(), while now I want to save these LaTeX code, but I don't know which function can save these LaTeX code...
I like to use the sink() function:
latex.code <- function(){
cat("\\begin{align}\n")
cat("[X'X]^{-1}X'y\n")
cat("\\end{align}\n")
}
sink(file='ols.txt')
latex.code()
sink()
Edit: Obviously, you can choose the file path where the file will be saved by changing the sink argument such as: sink(file='c:/Users/Eva/Desktop/ols.txt'), or sink(file='~/ols.txt')
Assuming your R function returns a character string of LaTeX code (your question would be much improved if you made it more concrete, with some specific examples), you can output something like that to a file using the cat() function, and specifying a file using the file= argument. You can read about it via ?cat.
If it happens that you have your output in a character vector (i.e. you are using something like cat(<something>) to have it written to the console), you can use writeLines function, like this:
writeLines(<something>,"filename.txt")
However the best way to make a LaTeX file in R is to use either Sweave or make a brew template.

Resources