positron icon indicating copy to clipboard operation
positron copied to clipboard

RMD Documents with params set do not create the `params` object when run interactively

Open csnydermph opened this issue 1 year ago • 1 comments

System details:

Positron and OS details:

Positron Version: 2024.08.0 (system setup) build 31 Code - OSS Version: 1.91.0 Commit: 7ee0596b81d578059e63c9af7c535c6b6040b947 Date: 2024-08-10T13:58:22.910Z Electron: 29.4.0 Chromium: 122.0.6261.156 Node.js: 20.9.0 V8: 12.2.281.27-electron.0 OS: Windows_NT x64 10.0.19045

Interpreter details:

R 4.3.3

Describe the issue:

In RStudio, using ctrl+enter or running a code chunk inside of an RMD file would populate the params R object with the default params set in the YAML.. In Positron, the params object is not created when running code or code chunks interactively from the RMD file. This issue prevents manually running / testing parametric reports; the whole report must be rendered with defined params each time.

Steps to reproduce the issue:

  1. Create RMD file and define params in the YAML
  2. Create an R code chunk that references the params object, and manually run the code chunk
  3. Error: ! object 'params' not found

Expected or desired behavior:

The desired behavior is for Positron to populate the params object for RMD files as RStudio, so RMD files can be tested / iterated on when they are parameterized.

Were there any error messages in the UI, Output panel, or Developer Tools console?

Error:
! object 'params' not found

csnydermph avatar Aug 13 '24 19:08 csnydermph

This is custom RStudio behavior. Here's an example of where it is invoked:

https://github.com/rstudio/rstudio/blob/878a44dcec474d06b5e4f0adf68f962856187a75/src/cpp/session/modules/rmarkdown/NotebookQueue.cpp#L373-L380

Positron doesn't have any special processing for R Markdown docs and relies on the Quarto extension to provide most of its R Markdown functionality. So this might need to be implemented in the Quarto extension, in Positron, or via some collaboration between the two.

Related to #3676, another case where we want document context to be available when running code line-by-line from an R Markdown or Quarto doc.

jmcphers avatar Aug 13 '24 22:08 jmcphers

Also reported in https://github.com/posit-dev/positron/issues/4492.

jmcphers avatar Aug 26 '24 23:08 jmcphers

Also reported in https://github.com/posit-dev/positron/discussions/5289

juliasilge avatar Nov 07 '24 18:11 juliasilge

In vscode-R one could use something like this:

    {
        "key": "ctrl+shift+p",
        "command": "r.runCommandWithEditorPath",
        "args": "params <- rmarkdown::yaml_front_matter(\"$$\")$params |> lapply(\\(x) if (is.list(x)) x$value else x)"
    },

With this approach it is possible to read params variable via hotkey.

Could something like this be implemented in positron as an easy work-around? I couldn't find any R-mode commands which allow execution of arbitrary R code bound to a hotkey.

idavydov avatar Jun 04 '25 15:06 idavydov

@idavydov Would you want to try out creating a custom keyboard shortcut, as outlined here using workbench.action.executeCode.console?

https://positron.posit.co/keyboard-shortcuts.html#custom-shortcuts

juliasilge avatar Jun 04 '25 15:06 juliasilge

Thanks, @juliasilge .

This seems to work in the first approximation. I did not test it extensively.

    {
        "key": "Ctrl+Shift+P",
        "command": "workbench.action.executeCode.console",
        "when": "editorTextFocus && (editorLangId == 'rmarkdown' || editorLangId == 'quarto')",
        "args": {
            "langId": "r",
            "code": "params <- rmarkdown::yaml_front_matter(rstudioapi::getSourceEditorContext()$path)$params |> lapply(\\(x) if (is.list(x)) x$value else x)",
            "focus": true
        }
    }

idavydov avatar Jun 05 '25 08:06 idavydov

@rodrigosf672 Hi! I AM still seeing the params problem in Positron.

Image

Positron Version: 2025.08.0 (system setup) build 130 Code - OSS Version: 1.102.0 Commit: 76ddce53e85437b013671fe7d91a3a1c54f48341 Date: 2025-08-01T20:25:36.785Z Electron: 35.6.0 Chromium: 134.0.6998.205 Node.js: 22.15.1 V8: 13.4.114.21-electron.0 OS: Windows_NT x64 10.0.26100

don't think it matters but my sessionInfo:

R version 4.5.1 (2025-06-13 ucrt)
Platform: x86_64-w64-mingw32/x64
Running under: Windows 11 x64 (build 26100)

Matrix products: default
  LAPACK version 3.12.1

locale:
[1] LC_COLLATE=English_United States.utf8  LC_CTYPE=English_United States.utf8   
[3] LC_MONETARY=English_United States.utf8 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.utf8    

time zone: Europe/London
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] gt_1.0.0            janitor_2.2.1       TileMaker_0.2.7     logger_0.4.0        lubridate_1.9.4    
 [6] forcats_1.0.0       stringr_1.5.1       dplyr_1.1.4         purrr_1.1.0         readr_2.1.5        
[11] tidyr_1.3.1         tibble_3.3.0        tidyverse_2.0.0     ascFuncs_0.2.22     plotly_4.11.0      
[16] ggplot2_3.5.2       flexdashboard_0.6.2

loaded via a namespace (and not attached):
 [1] sass_0.4.10        generics_0.1.4     xml2_1.3.8         stringi_1.8.7      hms_1.1.3         
 [6] digest_0.6.37      magrittr_2.0.3     evaluate_1.0.4     grid_4.5.1         timechange_0.3.0  
[11] RColorBrewer_1.1-3 RJDBC_0.2-10       fastmap_1.2.0      jsonlite_2.0.0     jqr_1.4.0         
[16] DBI_1.2.3          httr_1.4.7         viridisLite_0.4.2  scales_1.4.0       stringdist_0.9.15 
[21] lazyeval_0.2.2     jquerylib_0.1.4    cli_3.6.5          rlang_1.1.6        withr_3.0.2       
[26] cachem_1.1.0       tools_4.5.1        parallel_4.5.1     tzdb_0.5.0         vctrs_0.6.5       
[31] R6_2.6.1           lifecycle_1.0.4    snakecase_0.11.1   htmlwidgets_1.6.4  pkgconfig_2.0.3   
[36] rJava_1.0-11       pillar_1.11.0      bslib_0.9.0        gtable_0.3.6       data.table_1.17.8 
[41] glue_1.8.0         xfun_0.52          tidyselect_1.2.1   knitr_1.50         farver_2.1.2      
[46] htmltools_0.5.8.1  rmarkdown_2.29     compiler_4.5.1  

PS - thanks for closing 1325, feels nice! :)

DataStrategist avatar Aug 07 '25 16:08 DataStrategist

@DataStrategist We do not officially support params used in this way in Positron yet, but you can try out the workaround as described above in https://github.com/posit-dev/positron/issues/4338#issuecomment-2943186159.

If you haven't yet, give a 👍 to this issue so we can know how to prioritize this feature request.

juliasilge avatar Aug 07 '25 17:08 juliasilge

@DataStrategist Thank you for sharing all of this, it's very helpful. In 2025.09.0 (Universal) build 18 and I get the following warning for the params.

Image

Additionally, I can reproduce the error you described as well.

Image

Does the workaround @juliasilge mentioned in https://github.com/posit-dev/positron/issues/4338#issuecomment-2943186159 work for you? Please let us know or, if you need additional help, please feel free to ping me again.

rodrigosf672 avatar Aug 07 '25 17:08 rodrigosf672

@juliasilge @rodrigosf672 Confirmed... that workaround works. At the risk of saying something stupid, I offer a suggested angle of attack. When an Rmd loads, the Problems lights up w/ a

No symbol named 'params' in scope.

Considering the uniqueness of params could some custom watcher see that and inject a quick params <- rmarkdown::yaml_front_matter(rstudioapi::getSourceEditorContext()$path)$params |> lapply(\\(x) if (is.list(x)) x$value else x) into the console?

It would even be tolerable if this is something baked into Positron that one could activate via the Command Palette: Load Params or some such.

Anyway thanks!

DataStrategist avatar Aug 07 '25 22:08 DataStrategist

@DataStrategist just FYI, this is a very limited work-around.

A) there are most certainly some edge cases with complex params: section. B) for a proper implementation much more logic need. At the very minimum rerun on save. And perhaps params needs to be cleared before calling rmarkdown::render().

I'm sure the positron team will work on a proper implementation at some point.

idavydov avatar Aug 08 '25 06:08 idavydov

As a possible workaround (at least for me), I have added the following to my rmd files in the top and then it works for interactive testing and when rendering

if (interactive()) { params <- rmarkdown::yaml_front_matter(rstudioapi::getSourceEditorContext()$path)$params }

pallevillesen avatar Oct 03 '25 06:10 pallevillesen