Skip to content

Commit ef19d93

Browse files
allisonhorstAllison HorstAllison HorstFil
authored
Technique example: data loader, R to ZIP (#1423)
* r to ZIP technique example * save * clearer column names in prediction table * only load individual files from ZIP, not entire archive in client * Update examples/loader-r-to-zip/src/data/penguin-mlr.zip.R Co-authored-by: Philippe Rivière <[email protected]> * Update examples/loader-r-to-zip/src/index.md Co-authored-by: Philippe Rivière <[email protected]> * Add to readme list --------- Co-authored-by: Allison Horst <[email protected]> Co-authored-by: Allison Horst <[email protected]> Co-authored-by: Philippe Rivière <[email protected]>
1 parent 7dcae8b commit ef19d93

File tree

9 files changed

+476
-0
lines changed

9 files changed

+476
-0
lines changed

examples/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
- [`loader-r-to-csv`](https://observablehq.observablehq.cloud/framework-example-loader-r-to-csv/) - Generate CSV from R
7272
- [`loader-r-to-jpeg`](https://observablehq.observablehq.cloud/framework-example-loader-r-to-jpeg/) - Generate JPEG from R
7373
- [`loader-r-to-json`](https://observablehq.observablehq.cloud/framework-example-loader-r-to-json/) - Generate JSON from R
74+
- [`loader-r-to-zip`](https://observablehq.observablehq.cloud/framework-example-loader-r-to-zip/) - Generate ZIP from R
7475
- [`loader-rust-to-json`](https://observablehq.observablehq.cloud/framework-example-loader-rust-to-json/) - Generate JSON from Rust
7576
- [`loader-snowflake`](https://observablehq.observablehq.cloud/framework-example-loader-snowflake/) - Load data from Snowflake
7677
- [`netcdf-contours`](https://observablehq.observablehq.cloud/framework-example-netcdf-contours/) - Convert NetCDF to GeoJSON with `netcdfjs` and `d3-geo-voronoi`

examples/loader-r-to-zip/.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.DS_Store
2+
/dist/
3+
node_modules/
4+
yarn-error.log
5+
.Rhistory
6+
.RData

examples/loader-r-to-zip/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[Framework examples →](../)
2+
3+
# R data loader to generate a ZIP archive
4+
5+
View live: <https://observablehq.observablehq.cloud/framework-example-loader-r-to-zip/>
6+
7+
This Observable Framework example demonstrates how to write an R data loader that accesses penguin body size measurements, performs multiple linear regression, then outputs model estimates and predictions as two separate CSV files in a ZIP archive.
8+
9+
The data loader lives in [`src/data/penguin-mlr.zip.R`](./src/data/penguin-mlr.zip.R).
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default {
2+
root: "src"
3+
};

examples/loader-r-to-zip/package.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"type": "module",
3+
"private": true,
4+
"scripts": {
5+
"clean": "rimraf src/.observablehq/cache",
6+
"build": "rimraf dist && observable build",
7+
"dev": "observable preview",
8+
"deploy": "observable deploy",
9+
"observable": "observable"
10+
},
11+
"dependencies": {
12+
"@observablehq/framework": "^1.7.0"
13+
},
14+
"devDependencies": {
15+
"rimraf": "^5.0.5"
16+
},
17+
"engines": {
18+
"node": ">=18"
19+
}
20+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/.observablehq/cache/
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Attach required packages (must be installed)
2+
library(readr)
3+
library(tidyr)
4+
library(dplyr)
5+
library(broom)
6+
7+
# Data access, wrangling and analysis
8+
penguins <- read_csv("src/data/penguins.csv") |>
9+
drop_na(body_mass_g, species, sex, flipper_length_mm, culmen_depth_mm)
10+
11+
penguins_mlr <- lm(body_mass_g ~ species + sex + flipper_length_mm + culmen_depth_mm, data = penguins)
12+
13+
mlr_est <- tidy(penguins_mlr)
14+
15+
mlr_fit <- penguins |>
16+
mutate(
17+
predicted_mass = penguins_mlr$fitted.values,
18+
residual = penguins_mlr$residuals
19+
)
20+
21+
# Write the data frames as CSVs to a temporary directory
22+
setwd(tempdir())
23+
write_csv(mlr_est, "estimates.csv")
24+
write_csv(mlr_fit, "predictions.csv")
25+
26+
# Zip the contents of the temporary directory
27+
system("zip - -r .")

0 commit comments

Comments
 (0)