pharmaverse / examples

End to end examples of pharmaverse packages for common safety clinical reporting analyses
https://pharmaverse.github.io/examples/
Apache License 2.0
8 stars 7 forks source link

Extract R code from qmd files for easier running #31

Closed bms63 closed 7 months ago

bms63 commented 9 months ago

Extract R code from qmd files for easier running of examples

vedhav commented 8 months ago

Was the idea to replace the code button icon to get the R code instead of the Rmarkdown code used to generate the page? Screenshot 2024-02-14 at 7 32 21 PM

bms63 commented 8 months ago

I was thinking inside the posit cloud installation - the entire ADSL R script would be available free of any markdown and code chunks

can-taslicukur commented 8 months ago

{knitr} package has the purl function that extracts R code from the .rmd files (works for .qmd as well). The documentation argument also allows you to specify whether you want pure code (including comments) or code + markdown content as Roxygen comments.

vedhav commented 8 months ago

@rossfarrugia Do we already have the posit cloud installation ready for use?

rossfarrugia commented 8 months ago

@vedhav yes - its linked on the site. from what @can-taslicukur suggests, would we just add here to our guidance a tip for users to use knitr::purl() if they wanted to exract only the R code to run? i prefer that option than trying to maintain 2 versions of same thing or anything like that.

bms63 commented 8 months ago

I'd really prefer to have the code ready for the users as a .R file rather than .qmd file and then telling them to run knitr::purl() to extract the code. Wearing R-newb hat I would find that off-putting and confusing introduction to jump into.

I understand the sentiment to not maintain two files!!! I was hoping some sort of CI thing could be set up to extract the code and drop it into the user bucket on the posit cloud if possible.

rossfarrugia commented 8 months ago

ok fair, maybe better to explore a CI solution then as you say...

can-taslicukur commented 8 months ago

I think the biggest handicap of automizing the generation of .R files with knitr::purl is the file paths in the code.

From: https://quarto.org/docs/projects/code-execution.html#working-dir

By default, the working directory for rendering files within a project is the directory of the file itself. If you prefer to use the main project directory instead, you can add the execute-dir: project option to your config:

So one needs to set the execute_dir and update the file paths in the quarto documents to make sure Rscripts and quarto documents run the code in the same working directory.

rossfarrugia commented 7 months ago

No further comments added from the team on this one and as I don't see it as an easy solution I'm going to say let's leave it with the current guidance of the knitr::purl() option. I really feel like these examples are just a starter for users and they should then explore the packages themselves further, so even if they manually wanted to cut and paste the R code sections into a new script they could. In reality they're likely to be adding extra code from other places anyway to also test out, like the admiral templates/vignettes.