Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[suggestion] option purl="comment" #1352

Open
stla opened this issue Jan 19, 2017 · 0 comments
Open

[suggestion] option purl="comment" #1352

stla opened this issue Jan 19, 2017 · 0 comments
Labels
feature Feature requests

Comments

@stla
Copy link

stla commented Jan 19, 2017

Hello knitr developers,

I've modified hook_purl in order to have the option purl="comment". With this option, the code is commented in the output script, even when it is evaluated.

I'm just sharing in case you think this is a good idea (see example below) and you want to add this modification in the package.

My code:

hook_purl2 <- function (before, options, envir)
{
  knitEnv <- getFromNamespace(".knitEnv", "knitr")
  if (before || knitr:::isFALSE(options$purl) || options$engine != "R")
    return()
  output = knitEnv$tangle.file
  if (knitr:::isFALSE(knitEnv$tangle.start)) {
    assign("tangle.start", TRUE, envir=knitEnv)
    unlink(output)
    params = knitEnv$tangle.params
    if (length(params))
      writeLines(params, output)
    assign("tangle.params", NULL, envir=knitEnv)
  }
  code = options$code
  if (knitr:::isFALSE(options$eval) || identical(options$purl, "comment"))
    code = knitr:::comment_out(code, "# ", newline = FALSE)
  if (is.character(output)) {
    cat(knitr:::label_code(code, options$params.src), file = output,
        sep = "\n", append = TRUE)
  }
}

Below is an example of application that illustrates the reason why I did this modification:

<<response-knitexpand, purl="comment">>=
outResp <- NULL
for (iResponse in seq_len(nResponses)){
  outResp <- c(outResp, knit_expand(responseTemplateFile))
}
@

In this chunk, I run knit_expand. Of course I want this code to be evaluated, but I don't want it in the script extracted by hook_purl. However I use purl="comment" instead of purl=FALSE. In this way I get the code as comments in the script. This is helpful to see that the report ran knit_expand at this place.

@cderv cderv added the feature Feature requests label Jan 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Feature requests
Projects
None yet
Development

No branches or pull requests

2 participants