-
Notifications
You must be signed in to change notification settings - Fork 74
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
tar_make error with Rmd files: Error in eval(x, envir = envir) : object 'params' not found #256
Comments
Hard to say why this would be happening. Would you post a reproducible example so I can troubleshoot? A traceback would also help. tar_manifest(callr_function = NULL)
traceback() |
library(targets)
tar_dir({
lines <- c(
"---",
"title: '`r params$paramcd`'",
"output: html_document",
"params:",
" paramcd: TRUE",
"---",
"",
"```{r, eval = params$paramcd}",
"print(params$paramcd)",
"print(object)",
"```"
)
writeLines(lines, "source.Rmd")
tar_script({
library(tarchetypes)
pipe1 <- tar_pipeline(
tar_target(domains,c("domain1", "domain2"))
)
pipe2 <- tar_pipeline(
tarchetypes::tar_render(report,"source.Rmd")
)
targets::tar_bind(pipe1)
})
tar_manifest(callr_function = NULL)
traceback()
})
#> Error in eval(x, envir = envir) : object 'params' not found
#> No traceback available Created on 2021-01-04 by the reprex package (v0.3.0) |
Thanks. The problem seems to come from lines <- c(
"---",
"title: '`r params$paramcd`'",
"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) : object 'params' not found
#> [1] "report.R" Created on 2021-01-04 by the reprex package (v0.3.0) I think the problem comes from the fact that this example report uses an R Markdown parameter inside the metadata for the code chunk: lines <- c(
"---",
"title: '`r params$paramcd`'",
"output: html_document",
"params:",
" paramcd: TRUE",
"---",
"",
"```{r, eval = TRUE}",
"print(params$paramcd)",
"print(object)",
"```"
)
writeLines(lines, "report.Rmd")
knitr::knit("report.Rmd", tangle = TRUE, quiet = TRUE)
#> [1] "report.R" Created on 2021-01-04 by the reprex package (v0.3.0) Related: I actually just deprecated user-side pipeline objects: #254. Now, you can just end |
Well, it looks like you actually can use variables for code chunk options: https://bookdown.org/yihui/rmarkdown-cookbook/chunk-variable.html. So maybe submit a bug report to |
Thanks for checking. It also seems to pop up from this angle too REditorSupport/vscode-R#504 @cderv where would be a good place to start an issue for this problem? |
Yes a variable can be pass in a chunk option. The issue here is with parametrized report. 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
#> ``` Created on 2021-01-05 by the reprex package (v0.3.0.9001) I would open an issue in knitr to investigate this, I am not sure if this is a bug or a missing feature (for knitr alone to be aware of parameters in YAML). This indeed become an issue with tools like target as you rely on knitr only to transform a .Rmd file into a .R file using knitr. With the .Rmd file using parameters, it seems it does not work.
For my understanding, why the need of purling the Rmd file ? Maybe there is a missing piece in R Markdown to provide a tooling for a specific need where |
What is supported though is using However, there is an issue in knitr when |
Because some R Markdown reports depend on upstream targets computed earlier in the pipeline. Users register these dependency targets with |
So in the case of |
@cderv Can purl/tangle preserve the yaml header? |
@wlandau so you need to find It is some times now that I think we miss a tool to directly extract the R code chunks to do some advanced stuff in other tools. Today, everything rely on purl. there are some internal functions that does that but they can't be used on their own for now. I am wondering if it could help for this type of usage. Thanks for sharing.
@yonicd You can keep all the text in the Rmd as roxygen comments in the R script lines <- c(
"---",
"title: knitr with params",
"output: html_document",
"params:",
" paramcd: TRUE",
"---",
"",
"```{r, eval = TRUE}",
"str(params)",
"```"
)
writeLines(lines, "report.Rmd")
res <- knitr::purl("report.Rmd", documentation = 2, quiet = TRUE)
xfun::file_string(res)
#> params <-
#> list(paramcd = TRUE)
#>
#> #' ---
#> #' title: knitr with params
#> #' output: html_document
#> #' params:
#> #' paramcd: TRUE
#> #' ---
#> #'
#> ## ---- eval = TRUE-------------------------------------------------------------
#> str(params) Created on 2021-01-05 by the reprex package (v0.3.0.9001) Obviously it must be in comments so that you have a valid R script at a result. |
I would love that! |
Prework
Question
When I have a
tar_bind
of pipelines and one of them has a parameterized Rmd file and runtar_visnetwork(targets_only = TRUE)
ortar_make()
i get this error in the console but the output is created regardless.It looks like targets is not finding the object that is created via the yaml header in the rmd and generating this error, is there any way to suppress this message since it doesnt really effect the output?
thanks
Reproducible example
The text was updated successfully, but these errors were encountered: