Silencing a package load message in Sweave - r

I'm loading optmatch in a Sweave document as follows:
<<myCodeBlock, echo=FALSE>>=
library(optmatch, quietly=TRUE)
#
You're loading optmatch, by Ben Hansen, a package for flexible
and optimal matching. Important license information:
The optmatch package makes essential use of D. P. Bertsekas
and P. Tseng's RELAX-IV algorithm and code, as well as
Bertsekas' AUCTION algorithm and code.
Bertsekas and Tseng freely permit their software to be used for
research purposes, but non-research uses, including the use of it
to 'satisfy in any part commercial delivery requirements to
government or industry,' require a special agreement with them.
By extension, this requirement applies to any use of the
fullmatch() function. (If you are using another package that has
loaded optmatch, then you will probably be using fullmatch indirectly.)
For more information, enter relaxinfo() at the command line
As you can see, I've tried every way I can think of to silence the package load message, to no avail. I assume this is because they just used a straight-up cat() or something like that, but it's mighty annoying. Any thoughts on how to silence this so that those reading my final, beautiful, LaTeXified PDF don't have to read about RELAX-IV?
Other things that don't seem to work (take from Andrie's pointer to a related thread):
suppressMessages(library(optmatch))
suppressPackageStartupMessages(require("optmatch"))
I should note this is pretty obviously an R problem not a Sweave problem, as the messages pop up in R also.

Try loading the package in a hide results chunk:
<<packages,results=hide>>=
require(optmatch)
#
If you use the knitr package, you need to quote hide:
<<packages,results='hide'>>=
require(optmatch)
#

Here is an R solution to your problem. The package author uses cat to print the messages to the console, rather than using standard message statements. You can intercept these messages by using sink to divert console output to a temporary file:
<<myCodeBlock, echo=FALSE>>=
zz <- tempfile()
sink(file=zz)
library(optmatch, quietly=TRUE))
unlink(zz)
#
PS. The solution by #XuWang uses only SWeave, so is clearly much more suitable in your case.

Related

print the data.table package's .onAttach messages with knitr

I have a bookdown rmd looking like...
Further introductory materials are offered when the package is loaded:
```{r dt-startup, echo=-1, message=TRUE, verbose=TRUE, hide=FALSE}
if ("data.table" %in% .packages()) detach("package:data.table")
library(data.table)
```
My intention was to show the reader the package's startup messages. However, they don't print. Is there some other chunk option to use here?
As you can see, I just threw several maybe-relevant chunk options at it to no good result. I'm not terribly familiar with management of output streams, so that's as far as I knew to go. I also tried calling directly with data.table:::.onAttach(), but no dice.
I'm not sure what else would be relevant here, but ...
Currently the package has not been loaded before this chunk. I just added the first line for robustness in case I rearrange the document.
My before_chapter_script contains nothing but knitr::opts_chunk$set(comment="#").
My knit header value is bookdown::render_book and the output is bookdown::html_book.
Don't. Anything hacked in for this would be fragile and arguably not terribly useful.
Yihui Xie (knitr's author) makes a good case. My synopsis:
This is not useful. You're writing a tutorial, so why include dynamic content (that may change when the package changes)? Moreover, why not point to resources directly rather than to the list of resources printed there?
This is very hard. It is not just a matter of output streams. The messages don't print because they are walled behind an interactive() check. It's not obvious how this should be overridden and, supposing it could be done, what weird side effects that might introduce.

R documentation, how to force Rcmd Rd2pdf to automatically break an overfull line in examples R-like section?

My package passed Rcmd check successfully. but in constructed PDF format of R documentation, within the examples section, half of the line (R code) is out of the paper. I also found another CRAN submitted package, ftsa , that suffer from overfulling in a line too, see ftsa Reference manual.
I guess this
problem roots in the behavior of the verbatim environment in Latex. However there are some Latex packages to deal with this, https://tex.stackexchange.com/questions/14342/verbatim-environment-that-can-break-too-long-lines, but I do not know how to use them with Rcmd.
In dealing with these cases, why Rcmd check does not show any error, warnings or note as Latex does?
How can I force line breaking in examples section?
Thank you
R CMD check does not analyze / forward all LaTeX warnings. Yes, in an ideal world it would.
It has been the case, always that you should format the \examples{ ... } section
well, yourself,
notably including sensible line breaks (and leading spaces) for nice alignment of multi -line examples.
Use the sources of R itself or recommended packages (or those authored by me, as I do pay attention to this quite a bit), to see good examples in their *.Rd files.
Remember: The current development sources of R are always web accessible at
R (devel) source tree # svn.r-project.org, i.e. R's standard packages at R sources src/library.

what does it mean that "cacheSweave doesn't cache side-effects"?

I'm using cachesweave, but I don't think I get how everything works. I've tried to separate the code into simulation chunks and the plotting chunks, but some of the code is very long and written before I started the sweave document, so I instead use something like
<<foo,cache=TRUE>>
source("mainScript.R")
#
<<plot,fig=TRUE>>
a<- print(str(F1))
plot(F1)
#
The thing is mainScript.R is somewhat convoluted simulation code including plot functions and so on. I've read in cacheSweave vignette "cacheSweave doesn't cache side-effects" and plots are not cached, so I was wondering if the plotting functions in mainScript.R effect how the expressions are evaluated?
This might be an obvious question. Let's say I have another chunk after the two above. all of the results of the expressions in both "foo" and "plot" can be used in this new chunk, right? For example,
<<post-chunk>>
print(a)
print(str(F1))
#
See Wikipedia for a full explanation. Some common side-effects in R include: print() objects, draw plots, write files and load packages.
The cacheSweave package only enables you to skip computation, and you have to lose all side-effects. As Dason commented, the knitr package is much more natural in terms of caching -- what you see in an uncached chunk will be seen in the cached chunk. The caching of side-effects in knitr is explained in its manual and the cache page in the website.
BTW, knitr keeps compatibility with Sweave and cacheSweave, so hopefully you do not need to do anything for the transition; just call library(knitr); knit('file.Rnw').

Print the sourced R file to an appendix using Sweave

I keep R and Rnw files separate, then load the R data/plots with load("file.R") in the first Sweave chunk. Is there a way that I can print the sourced R file to an appendix without executing all of the code? (i.e., the code is slow enough that I don't want to source() it in an echo=TRUE chunk).
Thanks!
Update -- actually, I don't think my source() idea works.
How about using a Latex package?
Add into your header
\usepackage{fancyvrb}
Then
\VerbatimInput{yourRfile.R}
You can use highlight package to output nicely formatted, colorful code:
highlight("myRfile.R", renderer = renderer_latex(document = F))
But don't forget to put in your latex doc the lengthy preamble which you get with document=T.
You can experiment with code directly:
highlight(output="test.tex",
parser.output = parser(text = deparse(lm)),
renderer = renderer_latex(document = T))
And get
I usually solve this by:
\begin{appendix}
\section{Appendix A}
\subsection{R session information}
<<SessionInforamtaion,echo=F,eval=T,results=tex>>=
toLatex(sessionInfo())
#
\subsection{The simulation's source code}
<<SourceCode,echo=F,eval=T>>=
Stangle(file.path("Projectpath","RnwFile.Rnw"))
SourceCode <- readLines(file.path("Projectpath","Codefile.R"))
writeLines(SourceCode)
#
\end{appendix}
Using this you have to think of a maximum numbers of characters per line.
Separating R and Rnw files sort of defeats the purpose of literate programming. My own approach is to include the code chunks at the appropriate place in the text. If my audience isn't interested in the code, then I might mark it as
<<foo, echo=FALSE>>=
x <- 1:10
#
I might assemble the code in an appendix as
<<appendix-foo, eval=FALSE>>=
<<foo>>
#
which I admit is a bit of a kludge and error prone (forgotten chunks). One quickly wants to bundle the document with supporting material (data sets, useful helper functions, non-R scripts) into an R package, and these are not difficult to create. Building the package automatically creates the pdf and Stangle'd R file, which is exactly what you want. Package building can be a slow process, but installing the package does not require that the vignettes be rebuilt and so is fast and convenient for whomever you're giving the package to.
For twiddling with formatting / text, I use a global option \SweaveOpts{eval=FALSE}.

How do I generate reports in R without texi2dvi or TeX installed?

I've been struggling for a week now trying to figure out how to generate reports in R using either Sweave or Brew. I should say right at the beginning that I have never used Tex before but I understand the logic of it.
I have read this document several times. However, I cannot even get a simple example to parse. Brew successfully converts a simple markup file (just a title and some text) to a .tex file (no error). But it never ever converts tex to a pdf.
> library(tools)
> library(brew)
> brew("population.brew", "population.tex")
> texi2dvi("population.tex", pdf = TRUE)
The last step always fails with:
Error in texi2dvi("population.tex", pdf = TRUE) :
Running 'texi2dvi' on 'population.tex' failed.
What am I doing wrong?
The report I am trying to build is fairly simple. I have 157 different analysis to summarize. Each one has 4 plots, 1 table and a summary. I just want
output plot 1,2,3,4
output table
\pagebreak
...
that's it. Can anyone help me get further? I use osx, don't have Tex installed.
thanks
You cannot run this without texi2dvi or TeX installed.
An alternative may be html output -- the hwriter package is useful for that.
That said, if you want to produce pdf out, Sweave is the way to go. Frank Harrell's site has a lot of useful info but all this requires a bit of familiarity with LaTeX so you may need to install and learn that first.
If you are on OSX, might as well install the full tex live
http://mirror.ctan.org/systems/mac/mactex/MacTeX.mpkg.zip
It is a big download, but it will be nice to never have to install additional packages.
Another solution: the ascii package in conjonction to your favorite markup language (asciidoc, txt2tags, restructuredtext, org or textile).
http://eusebe.github.com/ascii/
It may be worthwhile spending a week or so just using LaTeX without R and going through a bunch of introductory LaTeX tutorials.
Thus, when you start producing Sweave or Brew documents and you get errors, you will be better able to identify whether the error is arising from LaTeX or Sweave / Brew.
A couple of Windows tools that make it easy to get started with LaTeX include MikTeX + TeXnicCenter or MikTeX + WinEdt.
Another solution is to try a solution of connecting R to microsoft.
It is much weaker then Sweave, but for basic reporting might be what you need.
You might want to go through the example sessions given here: Exporting R output to MS-Word with R2wd (an example session)
I've also been hearing a lot of good things about the knitr package. It seems to resemble Sweave a lot, but add some more to it. I would definitely take a look at it.

Resources