From xie @ending from yihui@n@me  Mon Jul  9 22:19:38 2018
From: xie @ending from yihui@n@me (Yihui Xie)
Date: Mon, 9 Jul 2018 15:19:38 -0500
Subject: [Rd] Parametrized Vignettest in R packages
In-Reply-To: <CAAjnpdhgaDipXmcydysPmnM9+VCGCMVBc0NPm2rdUfVZX=UUgg@mail.gmail.com>
References: <CAAjnpdhaeX+=RXCmq3g7jwYQZanfvps1q5i5znmynAGqxb1MPw@mail.gmail.com>
 <129b8986-700f-20bc-fefd-b813df73d06c@gmail.com>
 <CAAjnpdikxuy=KW1OVQEsqkQRwX8VLfedqwxO9mWzZ=cnijx-ZA@mail.gmail.com>
 <cafaec59-174f-9259-4d5c-f963299cdf23@gmail.com>
 <CAAjnpdhkSa2_-4yCMvM5pNPjdLem-D4MwM=UmNJrjTGeV8AQfQ@mail.gmail.com>
 <6ac69071-c1b9-a7df-b33c-3c9ab1a5c973@gmail.com>
 <CANROs4ffNzoB5ZCDpVjkz6RjboyYuDGb+BCP5Z2NtKywmXuRTQ@mail.gmail.com>
 <CAAjnpdjWuQGYPBEstqHQn0wz-9d+8fyEH8oaArA+EDZ-u-WeGQ@mail.gmail.com>
 <95032c71-f99c-23d1-64a2-1651efa15cb0@gmail.com>
 <CAAjnpdhgaDipXmcydysPmnM9+VCGCMVBc0NPm2rdUfVZX=UUgg@mail.gmail.com>
Message-ID: <CANROs4cuDNBEZGSd-vFk73Qk=pR9pb4f0vLqv1wMtmZPP4r28A@mail.gmail.com>

I wonder if get(data(foo, package="myPackage")) could be rewritten as
myPackage::foo. The latter will be a little more rigorous, because
data(foo) simply returns a character string "foo", so you are
essentially calling get ("foo"), and the default get(, inherits =
FALSE) may cause you trouble sometimes.

Anyway, the value returned by quote() can be represented when calling
dput(), so knitr can support it when generating the R script from the
vignette.

Another way to specify the params defaults could be:

---
params:
  foo: null
---

```{r}
if (is.null(params$foo)) params$foo = myPackage::foo
```

Regards,
Yihui
--
https://yihui.name


On Mon, Jul 9, 2018 at 3:11 PM, Witold E Wolski <wewolski at gmail.com> wrote:
> Dear Duncan,
>
> Was close to giving up to use the parameterized rmarkown as vignettes.
> But your suggestions to use quote and eval, as well as to use the
> package parameter in data
>  made it work, with all devtools::install,check,build and
> build_vignettes as well as with R CMD ... etc.
> But most importantly it also still works with:
> rmarkdown::render("vignettes/tr_srm_summary.Rmd",
> params=list(configuration=skylineconfig, data=sample_analysis    ))
>
> THANK YOU.
>
> This is how my vignette header looks (see below) and it works.
>
> ---
> title: "Titel"
> author: "WEW at FGCZ.ETHZ.CH"
> date: "`r Sys.Date()`"
> output:
>   pdf_document: default
>   html_document: default
> params:
>   configuration:  !r quote(get(data(skylineconfig, package="myPackage")))
>   data: !r quote(get(data(sample_analysis, package="myPackage")))
> vignette: >
>   %\VignetteIndexEntry{Summarize Peptide Level Measurements}
>   %\VignetteEngine{knitr::rmarkdown}
>   %\VignetteEncoding{UTF-8}
> ---
>
>
> ```{r setup, include=FALSE}
> library(tidyverse)
>
> knitr::opts_chunk$set(echo = FALSE, message=FALSE)
> data <- eval(params$data)
> configuration <- eval(params$configuration)
> ```
>
> Have a great evening.
>
> regards
> Witek
>
> On 9 July 2018 at 21:32, Duncan Murdoch <murdoch.duncan at gmail.com> wrote:
>> On 09/07/2018 3:24 PM, Witold E Wolski wrote:
>>>
>>> Dear Yihui,
>>>
>>> Thank you for the valuable questions.
>>>
>>> sample_analysis is a "tibble" while
>>> configuration is an "R6" class.
>>> But I also have parametrized reports where I pass R reference classes
>>> as arguments.
>>>
>>> This is the Rmd yaml params part corresponding to the error message.
>>>
>>> params:
>>>    configuration:  !r get(data(skylineconfig))
>>>    data: !r get(data(sample_analysis))
>>>
>>> Might the R6 class which I pass to  configuration the source of the
>>> problem here? I have heard somewhere that R6 uses environments to
>>> implemented some features.
>>>
>>>
>>>
>>> There is also a further problem I am encountering reproducible when
>>> running devtools::install() or R CMD INSTALL
>>>
>>> ** installing vignettes
>>>     'tr_srm_summary.Rmd' using 'UTF-8'
>>> Warning in data(skylineconfig) : data set 'skylineconfig' not found
>>> Error in get(data(skylineconfig)) : object 'skylineconfig' not found
>>
>>
>> You likely need to specify the package name, e.g.
>>
>> data("skylineconfig", package = "yourpackage")
>>
>> Also see my suggestion to use quote() to delay evaluation:
>>
>>
>>> params:
>>>    configuration:  !r quote(get(data(sample_analysis)))
>>>
>>> in the YAML will set configuration to the expression needed to get the
>>> environment;
>>>
>>>      eval(params$configuration)
>>
>>
>> Duncan Murdoch
>
>
>
> --
> Witold Eryk Wolski



[Rd] Parametrized Vignettest in R packages
Yihui Xie