I would like to set up a quarto document (or it could be an rmd as well) in connect such that it exits early or doesn't run unless a pin has been updated. But I am finding that I have am having to write more code that I expected. Specifically I am feel like I am doing something wrong that I have to write a hash to its own pin and then use that for logic to see if a pin has changed. This qmd will successfully fetch new data and do something (make a plot) if the data has changed but exit the qmd early if that data has not changed:
---
title: "Untitled"
format:
html:
self-contained: true
---
```{r}
library(pins)
board <- board_connect()
# Get the hash of the pin
pin_hash <- pin_meta(board, "sam/quakes")$pin_hash
# Load the previous hash from a pin
if (pin_exists(board, "sam/prev_pin_hash")) {
prev_pin_hash <- pin_read(board, "sam/prev_pin_hash")
} else {
prev_pin_hash <- "no hash"
}
# Check if the hash has changed
if (pin_hash != prev_pin_hash) {
# Run the chunk of code that uses the pin
results_proc <- pin_read(board, "sam/quakes")
message("data downloaded")
print(paste0("n rows: ", nrow(results_proc)))
print(pin_meta(board, "sam/quakes"))
# Update the previous hash with the current hash
prev_pin_hash <- pin_hash
# Save the updated previous hash to a pin
pin_write(board, prev_pin_hash, "sam/prev_pin_hash")
} else {
message("Nothing new downloaded")
results_proc <- pin_read(board, "sam/quakes")
print(paste0("n rows: ", nrow(results_proc)))
knitr::knit_exit()
}
```
```{r}
plot(results_proc$mag, results_proc$depth, col = "red", pch = 20, cex = 3)
```
Is there some functionality in pins that I am missing that provides this? I am wanting some function that is is_pin_changed()
that returns a logical. The relation to Connect here is that I am expecting this to be a piece of connect content that is run on a schedule and watches for changes in a pin and then only executes some code if it is changed. I am also not set on using pins though it does seem nice and convenient.