Skip to content

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

  1. Choose File → New File → R Script.

    You should see: an empty editor tab named Untitled1, above the Console.

  2. 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.

  3. Choose File → Save As.... The Save File window opens on R's working folder, mesa-practice. Type checks.R as the file name and click Save.

    You should see: the tab renamed checks.R.

  4. 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.

  5. In the Console, run source("checks.R", echo = TRUE).

    You should see: the lines of the script, the flag counts (S 2, <NA> 150), and a last line that names the version of R. No line begins Error. R shortens the opening comment and the line after it to [TRUNCATED]; they still ran.

  6. In the Console, run list.files().

    You should see: checks.R, gauges.csv, and readings.csv among 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

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.