I do not know much about S4, but I figured out one possible solution
on knitr's side, which I do not really understand. In short, your S4
methods need to be evaluated in globalenv(), but knitr uses the
parent.frame() by default, which is not the global environment when it
is called as the vignette builder.
Now I have forced the evaluation to be in the global environment, and
pushed the changes to the knitr development version on Github:
https://github.com/yihui/knitr
Regards,
Yihui
--
Yihui Xie <xieyihui at gmail.com (mailto:xieyihui at gmail.com)>
Phone: 515-294-2465 Web: http://yihui.name
Department of Statistics, Iowa State University
2215 Snedecor Hall, Ames, IA
On Mon, Apr 1, 2013 at 7:37 PM, Henrik Bengtsson <hb at biostat.ucsf.edu (mailto:hb at biostat.ucsf.edu)> wrote:
Hi,
things have indeed changed on how non-Sweave vignettes are built (this
happened around R devel 2013-03-05 r62130). However, it's not clear
to me what changes would be behind your problems, if any.
Build your vignette with the following buildVignette(), which emulates
what R does when it builds vignettes (cf. tools::buildVignettes()).
As you'll see, it reproduces your error:
if (!exists("buildVignette", mode="function")) {
source("http://r-forge.r-project.org/scm/viewvc.php/*checkout*/pkg/R.rsp/R/buildVignette.R?root=r-dots");
}
EXAMPLE (in a fresh R session):
url <- "http://r-forge.r-project.org/scm/viewvc.php/*checkout*/pkg/BayesFactorPCL/vignettes/manual.Rmd?root=bayesfactorpcl"
if (!file.exists("manual.Rmd")) download.file(url, "manual.Rmd");
output <- buildVignette("manual.Rmd", buildPkg="knitr");
browseURL(output);
bfr <- readLines(output);
idxs <- grep("which.max(bf)", bfr, fixed=TRUE);
idxs <- sort(sapply(idxs, FUN=function(idx) idx+(-5:5)));
cat(sprintf("%03d: %s\n", idxs, bfr[idxs]));
cat(sprintf("%03d: %s", idxs, bfr[idxs]), sep="\n")
2517: </code></pre>
2518:
2519: <pre><code>## [1] TRUE
2520: </code></pre>
2521:
2522: <pre><code class="r">BayesFactor::which.max(bf)
2523: </code></pre>
2524:
2525: <pre><code>## complaints
2526: ## 1
2527: </code></pre>
2531:
2532: <pre><code>## critical + advance
2533: ## 21
2534: </code></pre>
2535:
2536: <pre><code class="r">which.max(bf)
2537: </code></pre>
2538:
2539: <pre><code>## Error: no method for coercing this S4 class to a vector
2540: </code></pre>
2541:
So, have a look at browseVignette() and how it calls the 'knitr' weave
function. That should help you narrow down what's going on.
R version 3.0.0 RC (2013-03-31 r62463)
Platform: x86_64-w64-mingw32/x64 (64-bit)
locale:
[1] LC_COLLATE=English_United States.1252
[2] LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] tools stats graphics grDevices utils datasets methods
[8] base
other attached packages:
[1] BayesFactor_0.9.3 markdown_0.5.4 coda_0.16-1 lattice_0.20-15
[5] knitr_1.1.7
loaded via a namespace (and not attached):
[1] digest_0.6.3 evaluate_0.4.3 formatR_0.7 grid_3.0.0
[5] mvtnorm_0.9-9994 pbapply_1.0-5 stringr_0.6.2
Hope this helps
/Henrik
On Mon, Apr 1, 2013 at 4:52 PM, Richard D. Morey <r.d.morey at rug.nl (mailto:r.d.morey at rug.nl)> wrote:
A new problem has cropped up with compiling vignettes for my package BayesFactor. I'm not sure when it started, but I can tell you it didn't occur on R 2.15.3, and it does on 3.0.0 RC (2013-03-31 r62463) (session info is at the bottom of this message).
I have defined methods for which.min and which.max for a class (I've defined both S3 and S4 methods for the class "BFBayesFactor") in my package. I've exported the S4 class using exportMethods, and declared the S3 method with S3method. You can see the NAMESPACE file here:
https://r-forge.r-project.org/scm/viewvc.php/pkg/BayesFactorPCL/NAMESPACE?view=markup&root=bayesfactorpcl
and the methods here:
https://r-forge.r-project.org/scm/viewvc.php/pkg/BayesFactorPCL/R/methods-BFBayesFactor.R?view=markup&root=bayesfactorpcl
I have code in a vignette that calls the which.max method on a BFBayesFactor object. However, when that happens as the vignette is being compiled, I get an error:
which.max(bf)
## Error: no method for coercing this S4 class to a vector
This also occurs with which.min, but oddly not any other method (including the is.na method, which is declared exactly the same way, as far as I can tell). This seems to imply that the which.min and which.max methods are not exported.
When I use double colon notation - which as I understand, only works with exported functions - it works (see below).
When I compile the Rmd file manually, I do not see this problem; I get no errors. This seems to be a problem unique to compiling the vignette. I've tried it in Rstudio, R from the command line, and using Rscript and calling knitr directly. It only occurs when building a vignette for the source package.
Steps to reproduce:
1. Check out latest version of BayesFactor package from R-forge (https://r-forge.r-project.org/scm/?group_id=554) (revision 320)
2. Create a source package (which compiles the vignette)
3. Open tar to see compiled vignette HTML (doc/manual.html - or, alternatively, see https://www.dropbox.com/s/6csznytp8i1akjl/manual.html). Search for second occurrence of "which.max", and see the following lines:
## which model index is the best?
is(bf, "BFBayesFactor")
## [1] TRUE
BayesFactor::which.max(bf)
## complaints
## 1
BayesFactor::which.min(bf)
## critical + advance
## 21
which.max(bf)
## Error: no method for coercing this S4 class to a vector
which.min(bf)
## Error: no method for coercing this S4 class to a vector
I've been pulling my hair out on this, especially because something seems to have changed over the past few weeks that caused this, but I didn't catch exactly when, and I don't know if the issue lies with R 3.0.0 or my package.
R version 3.0.0 RC (2013-03-31 r62463)
Platform: x86_64-apple-darwin10.8.0 (64-bit)
locale:
[1] en_AU.UTF-8/en_AU.UTF-8/en_AU.UTF-8/C/en_AU.UTF-8/en_AU.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] BayesFactor_0.9.5 markdown_0.5.4 knitr_1.1.6 coda_0.16-1 lattice_0.20-15
loaded via a namespace (and not attached):
[1] digest_0.6.3 evaluate_0.4.3 formatR_0.7 grid_3.0.0 mvtnorm_0.9-9994
[6] pbapply_1.0-5 stringr_0.6.2 tools_3.0.0
Any help would be greatly appreciated,
Richard