Write an R script that runs the whole monitor on a folder of station exports, and schedule it to run every day:
on Windows it is registered in the Task Scheduler with the taskscheduleR package;
on Linux and macOS the line to add to the crontab (crontab -e) is
printed.
Every run reads all exports in folder (read_station()), runs
run_monitor(), and writes write_monitor_report() and
write_monitor_tables() into out_dir/<last day>/.
schedule_monitor(
folder,
out_dir,
time = "07:30",
taskname = "tidypptsdm_monitor",
script_dir = out_dir,
format = c("xlsx", "csv"),
register = TRUE,
...,
verbose = 1
)Folder with the station exports.
Folder for the results; one subfolder per day.
Time of the daily run, "HH:MM". Default "07:30".
Name of the scheduled task and of the script. Letters,
digits, _ and - only. Default "tidypptsdm_monitor".
Folder for the script. Default out_dir.
Format of the tables, see write_monitor_tables().
Default "xlsx".
Register the task (Windows) or print the crontab line.
Default TRUE. With FALSE only the script is written, e.g. to run
it by hand with Rscript.
Arguments of run_monitor() such as days,
house_width, low_feed_kg or station.
0 (silent) or 1 (summary, default).
Invisibly, the path of the script.
folder <- file.path(tempdir(), "exports")
dir.create(folder, showWarnings = FALSE)
data.table::fwrite(tidypptsda::nedap, file.path(folder, "nedap.csv"))
out <- file.path(tempdir(), "monitor_runs")
script <- schedule_monitor(folder, out, days = 7, register = FALSE)
#> ✔ Monitor script written to /tmp/RtmpBRLQC3/monitor_runs/tidypptsdm_monitor.R
cat(readLines(script), sep = "\n")
#> # Written by tidypptsdm::schedule_monitor() on 2026-10-31 06:38
#> # Daily monitor of the testing stations: edit the settings below if needed.
#> mon <- tidypptsdm::run_monitor("/tmp/RtmpBRLQC3/exports", days = 7)
#> out <- file.path("/tmp/RtmpBRLQC3/monitor_runs", format(mon$settings$end_date))
#> tidypptsdm::write_monitor_report(mon, out)
#> tidypptsdm::write_monitor_tables(mon, out, format = "xlsx")
# run once by hand
source(script)
#>
#> ── Reading 1 file ──────────────────────────────────────────────────────────────
#> ℹ Detected station type "nedap"
#> ✔ Read 31863 rows from 1 file (NEDAP)
#>
#> ── Preparing NEDAP visits ──────────────────────────────────────────────────────
#> Visits read 31,863
#> └ unidentified (kept, identified = FALSE) 211 0.7%
#> └ body weight 0 (set to NA) 391 1.2%
#> ℹ No pig was seen at more than one station
#> ✔ 30 pigs at 2 stations, 31,863 visits over 97 days 0.2s
#> ℹ No breed column. To use breed in the feed intake model and breed-specific limits, add it with `add_breed()`.
#>
#> ── Monitoring NEDAP stations: 2024-05-19 to 2024-05-25 (7 days) ────────────────
#> ℹ 2 stations, 28 pigs; on 2024-05-25: 366 visits, 84.8 kg feed
#> ✔ No alerts
#> ✔ Report of 2 stations written to /tmp/RtmpBRLQC3/monitor_runs/2024-05-25/index.html
#> ✔ Wrote /tmp/RtmpBRLQC3/monitor_runs/2024-05-25/monitor.xlsx (16 sheets)
list.files(out, recursive = TRUE)
#> [1] "2024-05-25/img/station_101_hours.png"
#> [2] "2024-05-25/img/station_101_intake.png"
#> [3] "2024-05-25/img/station_101_trend.png"
#> [4] "2024-05-25/img/station_101_weight.png"
#> [5] "2024-05-25/img/station_102_hours.png"
#> [6] "2024-05-25/img/station_102_intake.png"
#> [7] "2024-05-25/img/station_102_trend.png"
#> [8] "2024-05-25/img/station_102_weight.png"
#> [9] "2024-05-25/index.html"
#> [10] "2024-05-25/monitor.xlsx"
#> [11] "tidypptsdm_monitor.R"
unlink(c(folder, out), recursive = TRUE)
folder <- file.path(tempdir(), "exports")
dir.create(folder, showWarnings = FALSE)
data.table::fwrite(tidypptsda::nedap, file.path(folder, "nedap.csv"))
out <- file.path(tempdir(), "monitor_runs")
script <- schedule_monitor(folder, out, days = 7, register = FALSE)
#> ✔ Monitor script written to /tmp/RtmpBRLQC3/monitor_runs/tidypptsdm_monitor.R
cat(readLines(script), sep = "\n")
#> # Written by tidypptsdm::schedule_monitor() on 2026-10-31 06:38
#> # Daily monitor of the testing stations: edit the settings below if needed.
#> mon <- tidypptsdm::run_monitor("/tmp/RtmpBRLQC3/exports", days = 7)
#> out <- file.path("/tmp/RtmpBRLQC3/monitor_runs", format(mon$settings$end_date))
#> tidypptsdm::write_monitor_report(mon, out)
#> tidypptsdm::write_monitor_tables(mon, out, format = "xlsx")
unlink(c(folder, out), recursive = TRUE)