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

knitr::purl fail with parametrized Rmd when params is used in chunk option #1938

Open
cderv opened this issue Jan 5, 2021 · 7 comments
Open
Labels
feature Feature requests

Comments

@cderv
Copy link
Collaborator

cderv commented Jan 5, 2021

From ropensci/targets#256

When tangle = TRUE and Rmd uses parameters, it works ok if params are used in code chunks but it fails if params is used in one of the chunk option.

lines <- c(
  "---",
  "title: parameters",
  "output: html_document",
  "params:",
  "  paramcd: TRUE",
  "---",
  "",
  "```{r, eval = params$paramcd}",
  "print(params$paramcd)",
  "print(object)",
  "```"
)
writeLines(lines, "report.Rmd")
knitr::knit("report.Rmd", tangle = TRUE, quiet = TRUE)
#> Error in eval(x, envir = envir) : objet 'params' introuvable
#> [1] "report.R"
xfun::file_string("report.R")
#> params <-
#> list(paramcd = TRUE)

lines[8] <- "```{r, eval = TRUE}"
writeLines(lines, "report.Rmd")
knitr::knit("report.Rmd", tangle = TRUE, quiet = TRUE)
#> [1] "report.R"
xfun::file_string("report.R")
#> params <-
#> list(paramcd = TRUE)
#> 
#> ## ---- eval = TRUE-------------------------------------------------------------
#> print(params$paramcd)
#> print(object)

It seems like a bug.

Also, it seems knitr is not aware about parameters defined in a Rmd file when knitting. But this may be unsupported as parametrized report is a rmarkdown feature mainly

lines <- c(
  "---",
  "title: knitr with params",
  "output: html_document",
  "params:",
  "  paramcd: TRUE",
  "---",
  "",
  "```{r, eval = TRUE}",
  "str(params)",
  "```"
)
writeLines(lines, "report.Rmd")
res <- knitr::knit("report.Rmd", quiet = TRUE)
xfun::file_string(res)
#> ---
#> title: knitr with params
#> output: html_document
#> params:
#>   paramcd: TRUE
#> ---
#> 
#> ```r
#> str(params)
#> #> Error in str(params): object 'params' not found
#> ```
@cderv cderv added the bug Bugs label Jan 5, 2021
@yihui
Copy link
Owner

yihui commented Jan 24, 2021

But this may be unsupported as parameterized report is a rmarkdown feature mainly

That's it.

@cderv cderv added feature Feature requests and removed bug Bugs labels Jan 25, 2021
@cderv
Copy link
Collaborator Author

cderv commented Jan 25, 2021

Then we can say it is a feature request to be able to "purl" Rmd file, even when using parameters ?

It could also be another tool than knitr, as parsing code chunks and inline code from a Rmd chunk is something that seem to be often needed. For now, I think every usage like that uses purl().

@yihui
Copy link
Owner

yihui commented Jan 25, 2021

In principle, purl() doesn't evaluate any code in the document (only knit() does), so it won't recognize params$paramcd. To support this feature, we may have to break the design.

@cderv
Copy link
Collaborator Author

cderv commented Jan 25, 2021

I see. Thanks!

I think there is a need for Rmd code parsing with evaluation (as the one in targets - cc @wlandau @yonicd) but maybe it has to be different from purl. I'll keep think about it for the long term, looking at other existing use case.

@yihui
Copy link
Owner

yihui commented Jan 25, 2021

I'm okay with supporting this case. I have already had some code supporting the special case of R Markdown in knitr. Parsing YAML doesn't involve "evaluating" code, so that's probably fine.

In fact, params is supported when you do not use it in chunk options:

knitr/R/output.R

Lines 235 to 238 in 0272a83

params = knit_params(text)
params = if (length(params))
c('params <-', capture.output(dput(flatten_params(params), '')), '')
.knitEnv$tangle.params = params # for hook_purl()

The deeper problem is that I have never been satisfied with purl(). It is hard and sometimes impossible to do it right without evaluating code. The true way to purl code from a document is actually to use the hook knitr::hook_purl().

@wlandau
Copy link
Contributor

wlandau commented Jan 25, 2021

The true way to purl code from a document is actually to use the hook knitr::hook_purl().

I think there is still a need to statically extract code from a knitr document from outside the document itself (examples from targets and drake).

@yihui
Copy link
Owner

yihui commented Jan 25, 2021

Yes, there is a need. The problem is that it is not possible to both statically and faithfully extract code in certain cases. The hook_purl is must more reliable than purl(), but the obvious disadvantage is that it requires knitting the document.

clrpackages pushed a commit to clearlinux-pkgs/R-styler that referenced this issue Aug 9, 2022
…1.7.0

Indrajeet Patil (1):
      update GHA

Lorenz (62):
      switch to latest pre-commit.ci
      avoid pre-commit checks
      fix pre-commit issues
      more ignore
      emergency release to comply with CRAN policy
      drop unused option
      delete outdated documentation
      drop unnecessary conditions
      update NEWS
      update cran comments
      fix version in NEWS.md
      make this the devel version
      eof
      various fixes
      use latest {precommit} version
      ignore alignment vignette
      styler
      no invalid files
      purl can't work when header has outside dependencies (yihui/knitr#1938)
      more exclusion
      more exclusion
      re-roxygenize
      styler
      add news
      exclude more
      bump version
      autoupdate
      try newever pre-commit version
      also fix out file
      use latest hooks
      add vignette
      also recognize lintr ignore  markers
      stylerignore should be respected fr alignemnt detection
      add blog post on quick customization as vignette, Edis proposed by Maelle for main vignette
      rename
      bump
      bump
      see if serialization is problem
      bump
      bump
      bump
      bump
      bump
      bump
      bump
      bump
      bump
      bump
      bump2
      bump3
      try without serial
      fix refs
      add ability to set cache root within R.cache
      suggest cache name
      fix reference issues
      add messages
      upate main
      add news
      Don't allow multiple since regex is already enough flexible
      bump
      bump
      link GitHub template

Lorenz Walthert (127):
      file.remove does not work on Windows for directories.
      should not make a difference but not worse...
      Update R/utils-cache.R
      add dot
      improve (I think)
      Add news
      reverse order
      update pre-commit
      bump to 3.4 and remove backports
      remove < 3.4 related problem
      use drop-in replacement for xfun::read_utf8()
      complete / improve on #853
      remove xfun dep
      use touchstone bleeding edge
      bump
      Revert "use touchstone bleeding edge"
      make no trailing line a pre-processing step like trimming whitespaces
      styled text has no trailing, input also not (since last commit)
      bump
      use boostrap 5
      use touchstone after api change
      news
      fix issue
      remove helper from index
      don't opt in to boostrap 5
      add pre-commit via GHA
      also run on main
      add style problem
      also on master and main push
      bump
      only push conditional on master
      add style problem
      restore initial
      move pipe adder one level up so we have the context of the enclosing call and can check if it's substitute or not.
      remove trees
      ignore more for built
      re-arrange news
      quoted key  does not mean aligned per se
      reject styling wrongly parsed coder
      add more concise advice
      fix error
      preserve trailing blank line
      fix invalid braces
      Update NEWS.md
      internal api should handle empty strings more grateful, yet not 100% consistent
      latest pre-commit
      add a test
      fix pre-commit
      always use keyword invalid for unparsable
      more renaming
      add new hooks
      re-order
      sort Rbuildignore
      add more examples
      re-order
      Only one concurrent build from each group
      test if in-flight is cancelled
      also switch for touchstone
      empty
      action only set within jobs probably
      bump
      more generic
      bump
      fix concurrency
      document
      must have python 3.9
      creating tokens must respect stylerignore
      more cases and tests
      fix old R version
      needs explicit 0
      don't eval vignette
      remove indent again
      Revert "don't eval vignette"
      fix and test .onLoad()
      fix test
      stronger guarantees for output
      "\01" does not really exist
      dont subset with [[1]], since comments will return empty expression
      Update NEWS.md
      use touchstone action
      use touchstone GitHub Action
      upgrade R
      fix json
      newer binaries
      use action from main
      slight test relax
      replace action with template
      add concurency
      test PR #99
      don't double count the comma between column and the previous column
      prepare news
      switch back to main after #99 was merged.
      tilde is flattened out and causes indention like other operators
      somehow simplify and extend
      switch back to main after #99 was merged.
      bump
      bump
      update actions
      add concurrency for pkgdown also
      move concurrency down
      fix failing build
      style
      within functioncall, `token_after` is a nest and not a terminal -> `token_after` is `NA`
      set styler cache
      {glue} should not be a runtime CRAN dependency, it's only used in {touchstone} script, which is in .Rbuildignore and {touchstone} already covers {glue} (https://github.com/lorenzwalthert/touchstone/blob/main/actions/receive/action.yaml#L49).
      Update NEWS.md
      proper version comparision with string
      add new integrations
      pass down indent_by
      more updates
      more updates
      allow for arbitrary indention characters
      relax speed bound
      autoupdate pre-commit
      try newer release for parsing and deps
      fix error typo
      test ignore markers
      test on problematic file
      eq sub:
      Update NEWS.md
      don't check specific error message
      prepare release
      note on dependencies
      add all contributors
      fix urls
      must mask all non-base packages
      require higher cli to avoid backwards incompatibility with https://github.com/r-lib/styler/pull/894/files

Maëlle Salmon (1):
      Add another "hacking" tip

bersbersbers (1):
      Fix incorrect option name file_type (close #854)

github-actions[bot] (4):
      pre-commit
      pre-commit
      pre-commit
      pre-commit

pre-commit-ci[bot] (2):
      [pre-commit.ci] pre-commit autoupdate
      [pre-commit.ci] auto fixes from pre-commit.com hooks
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

3 participants