In a vignette demonstrating how to use a Suggested
package, I have something like this:
if (suggested_package_not_available) {
knitr::opts_chunk$set(eval = FALSE)
}
This means that the vignette still runs etc. although the Suggested
package is not available. It just shows the code, not the results.
Can I do something similar for inline R
code (`r code`
)?
Maybe a hook that uses a regex (a la `r [^`]+`
) to add two backticks around the inline code so that the inline code is showed instead of evaluated (which would normally cause an error because the chunks are no longer evaluated)?
A trick might be to print a string or evaluate the expression:
check_code <- function(expr, available){
if(available){
eval(parse(text = expr))
} else {
expr
}
}
check_code("1+1", TRUE)
check_code("1+1", FALSE)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With