Leave an R script someone else can rerun¶
Draft: not yet validated
These steps were followed once inside MESA RStudio Geospatial on 2026-11-16 (R 4.6.2), by the AI assistant that drafted them. No person has yet followed them, so a screen may differ. Tell us what you find.
What you type in the Console stays in R's memory. A script that uses a table you loaded by hand runs for you and fails for the next person, who starts with nothing loaded.
You will: have a script that runs from an empty workspace, top to bottom, without you.
Time: 5 to 10 minutes. You need: the mesa-practice folder from
Load a file from your Data Store in RStudio, in
MESA RStudio Geospatial, with R's working folder still set
to it.
Steps¶
-
Choose File → New File → R Script.
You should see: an empty editor tab named Untitled1, above the Console.
-
Type the script. It says what it needs, reads its own data, and names files without a folder in front:
# Checks on the practice rain-gauge data. # Needs gauges.csv and readings.csv in the same folder as this script. # Both are at https://idss-mesa.github.io/trainings/tutorials/practice-data/ readings <- read.csv("readings.csv", na.strings = "") gauges <- read.csv("gauges.csv") stopifnot(nrow(readings) == 152, anyDuplicated(gauges$gauge_id) == 0) print(table(readings$flag, useNA = "always")) cat(R.version.string, "\n")You should see: the script in the editor. A path that starts at your own home folder works only for you; a bare file name works for anyone who has the file beside the script.
-
Choose File → Save As.... The Save File window opens on R's working folder,
mesa-practice. Typechecks.Ras the file name and click Save.You should see: the tab renamed checks.R.
-
In the Console, empty R's memory: run
rm(list = ls()).You should see: the Environment tab at the top right now empty. This is what the next person starts with.
-
In the Console, run
source("checks.R", echo = TRUE).You should see: the lines of the script, the flag counts (
S2,<NA>150), and a last line that names the version of R. No line beginsError. R shortens the opening comment and the line after it to[TRUNCATED]; they still ran. -
In the Console, run
list.files().You should see:
checks.R,gauges.csv, andreadings.csvamong the names. The folder holds everything the script needs, so the folder is what you share.
Check it worked¶
Step 5 ran to the version line from an empty Environment. stopifnot() stayed
silent, which means the table still has 152 rows and no gauge is listed twice.
If it didn't¶
Error: object '...' not found. The script uses something it never made, which
was in memory only because you had typed it in the Console. Add the line that makes
it above the line that failed, then repeat steps 4 and 5.
cannot open file 'readings.csv': No such file or directory. R's working folder
is not the folder that holds the script. Choose
Session → Set Working Directory → To Source File Location, then repeat step 5.
Next¶
- Record the software versions a result depends on
- Reference: Managing data, "Share data", for giving a colleague access to the folder.
- The same task in Python: Leave a notebook someone else can rerun.
Not yet validated by a person in MESA RStudio Geospatial. Drafted with AI assistance on 2026-11-16 and followed there once that day (R 4.6.2).
Machine-readable versions of this page: Markdown twin · raw source on GitHub · llms.txt · llms-full.txt (whole site). See For AI agents.