Scripts

Saving and reusing scripts

The script that --dry-run prints is not a throwaway. It is a complete, self-contained ir script. Redirect it to a file and you have a reproducible artifact you can version, share, or run again later:

rush run --dry-run 'df |> dplyr::filter(body_mass_g > 5000)' penguins.csv > analysis.R
head -3 analysis.R
#> #!/usr/bin/env -S ir run
#> #| packages:
#> #|   - rush

Run it with ir run. The #| frontmatter declares the script’s dependencies, so ir installs any missing packages and executes it. The saved script carries its own dependencies and needs nothing but ir on the target machine:

ir run analysis.R
#> species,island,bill_length_mm,bill_depth_mm,flipper_length_mm,body_mass_g,sex,year
#> Gentoo,Biscoe,50,16.3,230,5700,male,2007
#> Gentoo,Biscoe,50,15.2,218,5700,male,2007
#> Gentoo,Biscoe,47.6,14.5,215,5400,male,2007
#> Gentoo,Biscoe,46.7,15.3,219,5200,male,2007
#> Gentoo,Biscoe,46.8,15.4,215,5150,male,2007
#> Gentoo,Biscoe,49,16.1,216,5550,male,2007
#> Gentoo,Biscoe,48.4,14.6,213,5850,male,2007
#> Gentoo,Biscoe,49.3,15.7,217,5850,male,2007
#> Gentoo,Biscoe,49.2,15.2,221,6300,male,2007
#> Gentoo,Biscoe,48.7,15.1,222,5350,male,2007
#> Gentoo,Biscoe,50.2,14.3,218,5700,male,2007
#> Gentoo,Biscoe,46.3,15.8,215,5050,male,2007
#> Gentoo,Biscoe,46.1,15.1,215,5100,male,2007
#> Gentoo,Biscoe,47.8,15,215,5650,male,2007
#> Gentoo,Biscoe,50,15.3,220,5550,male,2007
#> Gentoo,Biscoe,47.3,15.3,222,5250,male,2007
#> Gentoo,Biscoe,45.1,14.5,207,5050,female,2007
#> Gentoo,Biscoe,59.6,17,230,6050,male,2007
#> Gentoo,Biscoe,49.1,14.8,220,5150,female,2008
#> Gentoo,Biscoe,48.4,16.3,220,5400,male,2008
#> Gentoo,Biscoe,44.4,17.3,219,5250,male,2008
#> Gentoo,Biscoe,48.7,15.7,208,5350,male,2008
#> Gentoo,Biscoe,49.6,16,225,5700,male,2008
#> Gentoo,Biscoe,50.5,15.9,222,5550,male,2008
#> Gentoo,Biscoe,50.5,15.9,225,5400,male,2008
#> Gentoo,Biscoe,44.9,13.3,213,5100,female,2008
#> Gentoo,Biscoe,45.2,15.8,215,5300,male,2008
#> Gentoo,Biscoe,48.5,14.1,220,5300,male,2008
#> Gentoo,Biscoe,45,15.4,220,5050,male,2008
#> Gentoo,Biscoe,50.4,15.3,224,5550,male,2008
#> Gentoo,Biscoe,46.2,14.9,221,5300,male,2008
#> Gentoo,Biscoe,54.3,15.7,231,5650,male,2008
#> Gentoo,Biscoe,49.8,16.8,230,5700,male,2008
#> Gentoo,Biscoe,49.5,16.2,229,5800,male,2008
#> Gentoo,Biscoe,50.7,15,223,5550,male,2008
#> Gentoo,Biscoe,48.2,15.6,221,5100,male,2008
#> Gentoo,Biscoe,46.5,14.8,217,5200,female,2008
#> Gentoo,Biscoe,48.6,16,230,5800,male,2008
#> Gentoo,Biscoe,51.1,16.3,220,6000,male,2008
#> Gentoo,Biscoe,45.2,16.4,223,5950,male,2008
#> Gentoo,Biscoe,52.5,15.6,221,5450,male,2009
#> Gentoo,Biscoe,50,15.9,224,5350,male,2009
#> Gentoo,Biscoe,50.8,17.3,228,5600,male,2009
#> Gentoo,Biscoe,51.3,14.2,218,5300,male,2009
#> Gentoo,Biscoe,52.1,17,230,5550,male,2009
#> Gentoo,Biscoe,52.2,17.1,228,5400,male,2009
#> Gentoo,Biscoe,49.5,16.1,224,5650,male,2009
#> Gentoo,Biscoe,50.8,15.7,226,5200,male,2009
#> Gentoo,Biscoe,51.1,16.5,225,5250,male,2009
#> Gentoo,Biscoe,55.9,17,228,5600,male,2009
#> Gentoo,Biscoe,49.1,15,228,5500,male,2009
#> Gentoo,Biscoe,46.8,16.1,215,5500,male,2009
#> Gentoo,Biscoe,53.4,15.8,219,5500,male,2009
#> Gentoo,Biscoe,48.1,15.1,209,5500,male,2009
#> Gentoo,Biscoe,49.8,15.9,229,5950,male,2009
#> Gentoo,Biscoe,51.5,16.3,230,5500,male,2009
#> Gentoo,Biscoe,55.1,16,230,5850,male,2009
#> Gentoo,Biscoe,48.8,16.2,222,6000,male,2009
#> Gentoo,Biscoe,50.4,15.7,222,5750,male,2009
#> Gentoo,Biscoe,45.2,14.8,212,5200,female,2009
#> Gentoo,Biscoe,49.9,16.1,213,5400,male,2009

The #!/usr/bin/env -S ir run shebang also makes the file directly executable (chmod +x analysis.R && ./analysis.R). This is handy for tweaking: dump a rush one-liner to a file, edit the generated R to do something rush does not express directly, and run it later.

Formatting with air

If the air formatter is on your PATH, rush runs the --dry-run output through it, so the printed script is neatly formatted and ready to save. This affects only what --dry-run shows; the script that actually runs is never reformatted.

Running without ir

By default rush runs each script with ir, which resolves the packages declared in the frontmatter on the fly. If you would rather run against the packages already installed in your library (for speed, to work offline, or in an environment where dependencies are pinned), pass --no-ir (-I) and rush runs the script with plain Rscript instead:

rush run --no-ir '6 * 7'
#> 42

In this mode the #| frontmatter is left as inert comments and nothing is installed, so every package the script needs must already be present. The generated script itself is unchanged, so the same file still runs under ir later.