Witold E Wolski
2015-Nov-10 10:40 UTC
[R] conditionally disable evaluation of chunks in Rmarkdown...
I do have an Rmd where I would like to conditionally evaluate the second part. So far I am working with : ```{r} if(length(specLibrary at ionlibrary) ==0){ library(knitr) opts_chunk$set(eval=FALSE, message=FALSE, echo=FALSE) } ``` Which disables the evaluation of subsequent chunks. However my RMD file contains also these kind of snippets : `r ` How do I disable them? regards -- Witold Eryk Wolski
Bert Gunter
2015-Nov-10 14:44 UTC
[R] conditionally disable evaluation of chunks in Rmarkdown...
Strictly speaking, wrong email list. Markdown is R Studio software, and you should post on their support site. Cheers, Bert Bert Gunter "Data is not information. Information is not knowledge. And knowledge is certainly not wisdom." -- Clifford Stoll On Tue, Nov 10, 2015 at 2:40 AM, Witold E Wolski <wewolski at gmail.com> wrote:> I do have an Rmd where I would like to conditionally evaluate the second part. > > So far I am working with : > > ```{r} > if(length(specLibrary at ionlibrary) ==0){ > library(knitr) > opts_chunk$set(eval=FALSE, message=FALSE, echo=FALSE) > } > ``` > > Which disables the evaluation of subsequent chunks. > > However my RMD file contains also these kind of snippets : `r ` > > How do I disable them? > > regards > > > > -- > Witold Eryk Wolski > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
Jeff Newmiller
2015-Nov-10 15:20 UTC
[R] conditionally disable evaluation of chunks in Rmarkdown...
Well, strictly speaking knitr and rmarkdown are a contributed packages and the official contact for any contributed package is found via the maintainer() function. The Posting Guide indicates that R-help should only be used as a backup if that avenue is a dead end. To be fair to the OP, there are a lot of useful contributed packages that get discussed here anyway. But Bert is right that if there is a forum more appropriate for a contributed package then it should be preferred and any question posed here should mention any dead ends encountered and good netiquette would be to link to any publicly-visible record of those attempts. --------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity. On November 10, 2015 6:44:16 AM PST, Bert Gunter <bgunter.4567 at gmail.com> wrote:>Strictly speaking, wrong email list. > >Markdown is R Studio software, and you should post on their support >site. > >Cheers, >Bert > > >Bert Gunter > >"Data is not information. Information is not knowledge. And knowledge >is certainly not wisdom." > -- Clifford Stoll > > >On Tue, Nov 10, 2015 at 2:40 AM, Witold E Wolski <wewolski at gmail.com> >wrote: >> I do have an Rmd where I would like to conditionally evaluate the >second part. >> >> So far I am working with : >> >> ```{r} >> if(length(specLibrary at ionlibrary) ==0){ >> library(knitr) >> opts_chunk$set(eval=FALSE, message=FALSE, echo=FALSE) >> } >> ``` >> >> Which disables the evaluation of subsequent chunks. >> >> However my RMD file contains also these kind of snippets : `r ` >> >> How do I disable them? >> >> regards >> >> >> >> -- >> Witold Eryk Wolski >> >> ______________________________________________ >> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >> https://stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide >http://www.R-project.org/posting-guide.html >> and provide commented, minimal, self-contained, reproducible code. > >______________________________________________ >R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide >http://www.R-project.org/posting-guide.html >and provide commented, minimal, self-contained, reproducible code.
Yihui Xie
2015-Nov-10 17:00 UTC
[R] conditionally disable evaluation of chunks in Rmarkdown...
The short answer is you cannot. Inline R code is always evaluated. When it is not evaluated, I doubt if your output still makes sense, e.g. "The value of x is `r x`." becomes "The value of x is ." That sounds odd to me. If you want to disable the evaluate of inline code anyway, you may use a custom function to do it. e.g. cond_eval = function(x) { if (isTRUE(knitr::opts_chunk$get('eval'))) x } Then `r cond_eval(x)` instead of `r x`. Regards, Yihui -- Yihui Xie <xieyihui at gmail.com> Web: http://yihui.name On Tue, Nov 10, 2015 at 4:40 AM, Witold E Wolski <wewolski at gmail.com> wrote:> I do have an Rmd where I would like to conditionally evaluate the second part. > > So far I am working with : > > ```{r} > if(length(specLibrary at ionlibrary) ==0){ > library(knitr) > opts_chunk$set(eval=FALSE, message=FALSE, echo=FALSE) > } > ``` > > Which disables the evaluation of subsequent chunks. > > However my RMD file contains also these kind of snippets : `r ` > > How do I disable them? > > regards > > > > -- > Witold Eryk Wolski
Henrik Bengtsson
2015-Nov-10 19:51 UTC
[R] conditionally disable evaluation of chunks in Rmarkdown...
On Tue, Nov 10, 2015 at 9:00 AM, Yihui Xie <xie at yihui.name> wrote:> The short answer is you cannot. Inline R code is always evaluated. > When it is not evaluated, I doubt if your output still makes sense, > e.g. "The value of x is `r x`." becomes "The value of x is ." That > sounds odd to me. > > If you want to disable the evaluate of inline code anyway, you may use > a custom function to do it. e.g. > > cond_eval = function(x) { > if (isTRUE(knitr::opts_chunk$get('eval'))) x > } > > Then `r cond_eval(x)` instead of `r x`. > > Regards, > Yihui > -- > Yihui Xie <xieyihui at gmail.com> > Web: http://yihui.name > > > On Tue, Nov 10, 2015 at 4:40 AM, Witold E Wolski <wewolski at gmail.com> wrote: >> I do have an Rmd where I would like to conditionally evaluate the second part. >> >> So far I am working with : >> >> ```{r} >> if(length(specLibrary at ionlibrary) ==0){ >> library(knitr) >> opts_chunk$set(eval=FALSE, message=FALSE, echo=FALSE) >> } >> ``` >> >> Which disables the evaluation of subsequent chunks. >> >> However my RMD file contains also these kind of snippets : `r ` >> >> How do I disable them?Just a FYI and maybe/maybe not a option for you; this is one of the use cases where RSP (https://cran.r-project.org/package=R.rsp) is handy because it does not require that code snippets (aka "code chunks" as originally defined by weave/tangle literate programming) to contain complete expressions. With RSP-embedded documents, you can do things such <% if (length(specLibrary at ionlibrary) > 0) { %> [... code and text blocks to conditionally include ...] <% } # if (length(specLibrary at ionlibrary) > 0) %> or include from a separate file, e.g. <% if (length(specLibrary at ionlibrary) > 0) { %> <%@include file="extras.md.rsp"%> <% } %> You can also use loops over a mixture of code and text blocks etc. Depending on when 'specLibrary at ionlibrary' gets assigned, you could preprocess you R Markdown file with RSP, but for this to work out of the box you basically need to know the value length(specLibrary at ionlibrary) before your R Markdown code is evaluated, i.e. before you compile the Rmd file. Your build pipeline would then look something like: rmd <- R.rsp::rfile("report.rmd.rsp") rmarkdown::render(rmd) /Henrik (author of R.rsp)>> >> regards >> >> >> >> -- >> Witold Eryk Wolski > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.