How can I save data from temporary monitors to a CSV file?

If you have AirMonitor installed, you should be able to copy and paste this code into the RStudio console.

The coding style takes advantage of the “pipe” operator, %>%, which uses the output of the preceding function as the first argument of the next function. Package functions are specifically designed to work well in this manner, encouraging readable and understandable code.

Think of each chunk as a recipe that begins with what you want to make and is followed by the steps needed to make it.

Enjoy!

library(AirMonitor)

# AIRSIS in California
#  - start with all AIRSIS monitors in 2019
#  - subset for those where stateCode is one of "CA"
airnow_ca <- 
  airnow_loadAnnual(2019) %>%
  monitor_filter(stateCode == "CA")

# Interactive map to pick a monitor
monitor_leaflet(airnow_ca)

# Select a single monitor by deviceDeploymentID
#  - start with airnow_ca
#  - subset for the "Mariposa" monitorID
Mariposa <-
  airnow_ca %>%
  monitor_select("9d60fd2e746019a5_840MMMPC1000")

# Interactive graph to pick some time limits
monitor_dygraph(Mariposa)

# Trim empty periods from the beginning and end of this time series
#  - start with Mariposa
#  - trim periods with missing data
Mariposa <- 
  Mariposa %>%
  monitor_trimDate()

monitor_timeRange(Mariposa)

# Subset this time series to October through December
#  - start with Mariposa
#  - subset based on a date range
Mariposa <- 
  Mariposa %>%
  monitor_filterDate(20191008, 20200101)

monitor_timeRange(Mariposa)

# A quick plot for October through December
monitor_timeseriesPlot(
  Mariposa,
  addAQI = TRUE,
  main = "Mariposa 2019"
)
addAQILegend("topright")

# Dump out a meta/data combined CSV file for a subset of Mariposa
Mariposa %>%
  monitor_filterDate(20191101, 20191102) %>%
  monitor_toCSV() %>%
  cat(file = "")
  
# Review the metadata at the command line
dplyr::glimpse(Mariposa$meta)

# Alternatively, View the data (or metadata) in the RStudio table viewer:
View(Mariposa$data)

# Set the output directory
outputDir <- tempdir()

# Dump out all of airnow_ca meta to a CSV file
airnow_ca %>%
  monitor_toCSV(includeData = FALSE) %>%
  cat(file = file.path(outputDir, "airnow_CA_meta.csv"))

# Dump out all of airnow_ca data to a CSV file
airnow_ca %>%
  monitor_toCSV(includeMeta = FALSE) %>%
  cat(file = file.path(outputDir, "airnow_CA_data.csv"))

# Alternatively, View() the metadata and data in RStudio:
View(airnow_ca$meta)
View(airnow_ca$data)

# ==============================================================================

# Everything above also applies to monitoring data from other sources.
# Just start with one of the following:

# All of AIRSIS for 2019
#airsis_loadAnnual(2019)

# All of WRCC for 2019
#wrcc_loadAnnual(2019)

Finally, to emphasize the compact, expressiveness of “recipe”-style syntax, the following calculates daily mean timeseries for each California monitor and displays the data dataframe in the RStudio viewer:

airnow_loadAnnual(2019) %>%
  monitor_filter(stateCode == "CA") %>%
  monitor_filterDate(20191001, 20201101, timezone = "America/Los_Angeles") %>%
  monitor_trimDate(timezone = "America/Los_Angeles") %>%
  monitor_dailyStatistic(mean) %>% 
  monitor_mutate(round, 1) %>%
  monitor_getData() %>%
  View()