quarto-dev / quarto-dev/quarto-cli

set seed for computation prior to rendering

Open
#13,003 4 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

computations engines-knitr enhancement
Dominant language
JavaScript
Stars
6k
Forks
458
Avg merge
1d 9h
Merged PRs (30d)
41

Description

With Rmarkdown, I can set a random seed prior to rendering, and I'll get the same document every time. Consider the following Rmd file (which I'm calling test_seed_rmd.Rmd):

---
title: "Untitled"
output: html_document
---

```{r}
rnorm(1)
```

If I use withr::with_seed to compile it:

withr::with_seed(
  seed = 123,
  {
    rmarkdown::render('test_seed_rmd.Rmd')
  }
)

then I get the output ## [1] -0.5604756 every time.

When I do the same with the Quarto document (test_seed_qmd.qmd):

---
title: "Untitled"
format: html
---

```{r}
rnorm(1)
```

I can use withr::with_seed again:

withr::with_seed(
  seed = 123,
  {
    quarto::quarto_render('test_seed_qmd.qmd')
  }
)

but this output of this is 1) different from the Rmd, and 2) different each time I run it. My understanding is that this is due to the fact that quarto renders in a separate process and supports multiple languages in code chunks, and obviously this is only for the R random seed only. So the quarto behaviour makes sense.

However, for my use case, I would like to be able to render a document by specifying the seed ahead of time. I don't want to set the seed (or have to add any new code or parameters) in the document itself. It's important that this happen outside the qmd document.

Is there a way of doing this? I had thought maybe of specifying a pre-render script, but when I'm not sure how that would work when compiling from R with quarto::quarto_render.

Thanks in advance. Session info is below.


> xfun::session_info()
R version 4.2.1 (2022-06-23)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS Ventura 13.2.1, RStudio 2022.12.0.353

Locale: en_US.UTF-8 / en_US.UTF-8 / en_US.UTF-8 / C / en_US.UTF-8 / en_US.UTF-8

Package version:
  askpass_1.1      base64enc_0.1.3  bslib_0.4.2      cachem_1.0.7     cli_3.6.0       
  compiler_4.2.1   curl_5.0.0       digest_0.6.31    ellipsis_0.3.2   evaluate_0.20   
  fastmap_1.1.1    fs_1.6.1         glue_1.6.2       graphics_4.2.1   grDevices_4.2.1 
  highr_0.10       htmltools_0.5.4  jquerylib_0.1.4  jsonlite_1.8.4   knitr_1.42      
  later_1.3.0      lifecycle_1.0.3  magrittr_2.0.3   memoise_2.0.1    methods_4.2.1   
  mime_0.12        openssl_2.0.5    packrat_0.9.1    processx_3.8.0   ps_1.7.2        
  quarto_1.2       R6_2.5.1         rappdirs_0.3.3   Rcpp_1.0.10      renv_0.17.0     
  rlang_1.0.6      rmarkdown_2.19   rsconnect_0.8.29 rstudioapi_0.14  sass_0.4.5      
  stats_4.2.1      stringi_1.7.12   stringr_1.5.0    sys_3.4.1        tinytex_0.44    
  tools_4.2.1      utils_4.2.1      vctrs_0.5.2      withr_2.5.0      xfun_0.37       
  yaml_2.3.7 

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the quarto::quarto_render call and the issue's description of Quarto rendering in a separate process. Determine whether an external seed can be passed into R execution without modifying the .qmd document; done means repeated renders produce the same R output for a specified seed.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, r
Domain
build-system, tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.