How to export an r script file and not execute and display only the output? - r

I would like to execute an R script file to a pdf or word and not have the figures, tables, output, etc. displayed, only the lines of written code.
I tried this:
rmarkdown::render('script.R')
But I need it without the output that is also generated. I am pretty new to R so I am not that familiar with all the options.

Related

Is there any way to open multiple R markdown files and knit them at the same time?

I have several RMD files in one folder and I need to knit them one by one everyday to get html for each of them. Is there any way or function that I can open them at the same time and knit them by running only few lines code or one function?
did you have a look at this chapter: https://bookdown.org/yihui/rmarkdown/parameterized-reports.html. I think that is exactly what you will need.

Save a Sweave PDF output using custom file name

I have a sweave code that is producing a pdf. The code works fine.
I run it from the command line using:"
R CMD Sweave --pdf filename.Rnw
The resulting pdf that is produced is save in the working directory as
filename.pdf
Instead I would like 2 things:
Have the resulting output .pdf saved in another directory say wrk\random\dir
Change the name on a daily basis by adding the date at the end of it so I can maintain a history - such as filename.05032017.pdf
I tried using suggestions from here: Attach date to PDF generated with Sweave
but that didn't work. Needless to say it could be because I am absolutely new to R/SWEAVE and suck?
I don't think that can be done on the command line, but you can do it if you run Sweave within R:
filename <- paste0("wrk/random/dir/filename", format(Sys.Date(), "%d%m%Y"), ".tex")
Sweave("filename.Rnw", output=filename)
tools::texi2pdf(filename)
You could put this in a *.R file and run it with Rscript if you don't want to start R.

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.

Knit2html not replicating functionality of Knit HTML button in R Studio

I'm trying to write a Bash script in Ubuntu 10.04 that opens a Python file which exports a CSV, and then runs the following Rscript with the goal of exporting a HTML with plots from Dashboard.Rmd:
require(knitr)
setwd('/home/sensors/Desktop/')
knit2html('Dashboard.Rmd')
browseURL('Dashboard.html')
Dashboard.Rmd is an R markdown that calls read.csv on the csv from the first step, makes a data frame and creates plots, but that part's working fine. According to this, I figure that Rscript should replicate the action of pressing "Knit HTML" in R Studio. However, the html it creates is identical to the last time Knit HTML was pressed; i.e. even if the CSV is different, the html doesn't reflect the change.
I also tried using a separate line for knit and markdownToHTML with the same effect. It seems like it doesn't source the code from the Rmd when performing knit. It does update the html properly when I enter the commands from that Rscript into the console of R Studio with Dashboard.Rmd open. However I'm not sure how to translate that into a Bash script. I also tried knit2html with envir=new.env(), envir=R_GlobalEnv, and envir=parent.frame() with no luck. Any help would be appreciated!
So it turns out that this was an artifact of cache=TRUE -- the HTML file was not changed because everything was cached.

practically getting started with Sweave

my question(s) might be less general than the title suggests. I am running R on Mac OS X with a MySQL database to store the data. I have been working with the Komodo / Sciviews-R for some time. Recently I had the need for auto-generated reports and looked into Sweave. I guess StatET / Eclipse appears to be the "standard" solution for Sweavers.
1) Is it reasonable to switch from Komodo to StatET Eclipse? I tried StatET before but chose Komodo over StatET because I liked the calltip / autosuggest and the more convenient config from Komodo so much.
2) What´s a reasonable workflow to generate Sweave files? Usually I develop my R code first and then care about the report later. I just learned today that there is one file in Sweave that contains R code and Latex code at once and that from this file the .tex document is created. While the example files look handily and can't really imagine how to enter my 250 + lines of R code to a file and mixed it up with Latex.
Is it possible to just enter the qplot() and ggplot() statements to a such a document and source the functionality like database connection and intermediate results somehow?
Or is it just a matter of being used to the mix of Latex and R code?
Thx for any suggestions, hints, links and back-to-the-roots-shout-outs…
You've asked several questions, so here's several answers;
Is StatEt/Eclipse the right way to do Sweave ?
Not nessarily (note: I'm an avid StatEt/Eclipse user, and use it for both pure R and Sweave/R and love it, I haven't used Komodo / sciviews-R). You should be able to run the sweave command from any R command line which will generate a .tex file. You can then turn the .tex file into something readable (like pdf) from any tex environment.
What's a good Sweave workflow ?
When I have wanted to turn an r script into a sweave report I generaly start with an empty sweave template and copy/paste my entire R script into a sweave R block just after the title, i.e;
<<label=myEntireRScript, echo=false, include=false>>
#Insert code here
myTable<-dataframe(...)
myPlot<-qplot(....)
#
Then I go through and find the parts I want to report. For instance, if i want to put a table into the report, I'll cut the R block and put an xtable block in, and the same for variables and plots.
<<label=myEntireRScript, echo=false, include=false>>=
#Insert code here
#
Put any text I want before my table here, maybe with a \Sexpr{print(variable)} named variable
<<label=myTable, result=Tex>>=
myTable<-dataframe(...)
print(xtable(mytable,...),...)
#
Any text I want before my figure
<label=myplot, result=figure>>=
myPlot<-qplot(....)
print(qplot)
#
You may want to look at these related SO posts. The rest of my post relates to your question 2.
When creating reports with Sweave, I usually keep most of the R code and the report text separate. If the R code is fast to run, then I prefer I will include something like the following at the start of the .Rnw file:
<<>>
source('/path/to/script.r')
#
On the other hand, if the R code takes a long time, I will often include something like the following at the end of the R script:
Sweave('/path/to/report.Rnw'); system('pdflatex report.tex')
That way, I can re-generate the report quickly, without needing to run all the R code again. Then, the only work R has to do in the Sweave file is print tables, make graphs and maybe extract a few figures.
Like nullglob, I prefer to keep the R and Sweave files separate, but I prefer to save the workspace with save.image() rather than to source() the file. This avoids running the R calculations with each .Rnw file compiling (and I always end up tinkering with the typesetting more than I'd like).
My general work flow is to do each paper/project in it's own folder with it's own R file(s). When the calculation side is "done", I save.image() to store all the workspace variables as-is.
Then, in the .Rnw file in the same directory I set the working directory with setwd() and load all variables with load(".Rdata"). Of course, you can change the name you use for your workspace, but I do one workspace per folder and keep the default name. Oh, and if you tinker with the R file, be sure save the workspace image and watch out for variables that linger in the workspace and .Rnw file, but are no longer part of the R file... this is where the save.image() approach can cause some headaches.
I am on a Mac and I suggest TextMate if you're mildly geeky and emacs/ess if you're really geeky. I use vim and command line R, but emacs/ess works best for most. If you're in this for the long haul, I doubt you'll regret learning emacs/ess for R, Sweave, and LaTeX.

Resources