Skip to content

Commit

Permalink
Merge pull request #20 from Metropolitan-Council/rm-rlang-fix-tests
Browse files Browse the repository at this point in the history
Remove rlang, fix tests
  • Loading branch information
eroten authored Oct 31, 2022
2 parents f3d2e8a + f261b2e commit 20d9047
Show file tree
Hide file tree
Showing 23 changed files with 324 additions and 295 deletions.
1 change: 1 addition & 0 deletions .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,4 @@ jobs:
- uses: r-lib/actions/check-r-package@v2
with:
upload-snapshots: true
error-on: '"error"'
5 changes: 2 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Type: Package
Package: tc.sensors
Title: Retrieve Loop Detector Data from the MnDOT JSON Feed
Version: 0.2.1
Date: 2022-10-26
Version: 0.2.2
Date: 2022-10-31
Authors@R: c(
person("Metropolitan Council", role = "cph"),
person("Liz", "Roten", , "[email protected]", role = c("cre", "aut"),
Expand All @@ -27,7 +27,6 @@ Imports:
jsonlite (>= 1.6.1),
magrittr (>= 1.5),
purrr (>= 0.3.4),
rlang (>= 0.4.6),
sf (>= 0.9.5),
tibble (>= 3.0.1),
tidyr (>= 1.1.0),
Expand Down
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ importFrom(jsonlite,fromJSON)
importFrom(magrittr,"%>%")
importFrom(purrr,map)
importFrom(purrr,map2)
importFrom(rlang,.data)
importFrom(sf,st_as_sf)
importFrom(sf,st_cast)
importFrom(sf,st_length)
Expand Down
4 changes: 2 additions & 2 deletions R/add_weather.R
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ add_weather <- function(sensor_data,

weather_sensor_agg <- clean_weather[, as.list(unlist(lapply(.SD, function(x) {
list(
sum = sum(x, na.rm = T),
mean = round(mean(x, na.rm = T), 1)
sum = sum(x, na.rm = TRUE),
mean = round(mean(x, na.rm = TRUE), 1)
)
}))),
by = .(date, interval_bin),
Expand Down
18 changes: 12 additions & 6 deletions R/aggregate_sensor.R
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,20 @@ aggregate_sensor <- function(sensor_data, config, interval_length,
if (interpolate_missing == TRUE) {
sensor_data <- sensor_data[
, `:=`(volume.rollmean = data.table::shift(
data.table::frollapply(volume, 3, mean, align = "center", na.rm = T, hasNA = T)
data.table::frollapply(volume, 3, mean,
align = "center",
na.rm = TRUE, hasNA = TRUE
)
)),
by = .(sensor)
][
, volume := ifelse(is.na(volume), volume.rollmean, volume)
][
, `:=`(occupancy.rollmean = data.table::shift(
data.table::frollapply(occupancy, 3, mean, align = "center", na.rm = T, hasNA = T)
data.table::frollapply(occupancy, 3, mean,
align = "center", na.rm = TRUE,
hasNA = TRUE
)
)),
by = .(sensor)
][
Expand Down Expand Up @@ -158,8 +164,8 @@ aggregate_sensor <- function(sensor_data, config, interval_length,

sensor_data_agg <- sensor_data[, as.list(unlist(lapply(.SD, function(x) {
list(
sum = round(mean(x, na.rm = T) * n_rows_expected),
mean = mean(x, na.rm = T),
sum = round(mean(x, na.rm = TRUE) * n_rows_expected),
mean = mean(x, na.rm = TRUE),
pct.null = round(100 * sum(is.na(x)) / length(x))
)
}))),
Expand All @@ -186,8 +192,8 @@ aggregate_sensor <- function(sensor_data, config, interval_length,

sensor_data_agg <- sensor_data[, as.list(unlist(lapply(.SD, function(x) {
list(
sum = round(mean(x, na.rm = T) * n_rows_expected),
mean = mean(x, na.rm = T),
sum = round(mean(x, na.rm = TRUE) * n_rows_expected),
mean = mean(x, na.rm = TRUE),
pct.null = round(100 * sum(is.na(x)) / length(x))
)
}))),
Expand Down
2 changes: 1 addition & 1 deletion R/generate_spatial_lines.R
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ generate_spatial_lines <- function(config) {
dplyr::summarise(do_union = FALSE, .groups = "keep") %>%
sf::st_cast("LINESTRING") %>%
sf::st_set_crs(4326) %>%
dplyr::mutate(length_miles = as.numeric(sf::st_length(.data$geometry)) * 0.00062137)
dplyr::mutate(length_miles = as.numeric(sf::st_length(geometry)) * 0.00062137)

return(lines_sf)
}
37 changes: 18 additions & 19 deletions R/pull_configuration.R
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@
#' @importFrom purrr map2
#' @importFrom data.table fwrite
#' @importFrom utils download.file
#' @importFrom rlang .data
#'
#' @export
pull_configuration <- function(return_opt = "in_memory", .quiet = TRUE) {
Expand All @@ -85,42 +84,42 @@ pull_configuration <- function(return_opt = "in_memory", .quiet = TRUE) {
xml2::xml_find_all(metro_config, "//detector")
)
) %>%
dplyr::mutate(detector_path = .data$value) %>%
tidyr::separate(.data$detector_path, into = c(
dplyr::mutate(detector_path = value) %>%
tidyr::separate(detector_path, into = c(
"front", "tms_config",
"device", "rnode",
"detector"
), sep = "/") %>%
tidyr::unite(rnode_path, .data$front,
.data$tms_config, .data$device,
.data$rnode,
tidyr::unite(rnode_path, front,
tms_config, device,
rnode,
sep = "/"
) %>%
dplyr::mutate(rnode_path = trimws(.data$rnode_path)) %>%
dplyr::mutate(corridor_path = .data$rnode_path) %>%
tidyr::separate(.data$corridor_path, into = c(
dplyr::mutate(rnode_path = trimws(rnode_path)) %>%
dplyr::mutate(corridor_path = rnode_path) %>%
tidyr::separate(corridor_path, into = c(
"front", "tms_config",
"device", "rnode"
), sep = "/") %>%
tidyr::unite(corridor_path, .data$front,
.data$tms_config, .data$device,
tidyr::unite(corridor_path, front,
tms_config, device,
sep = "/"
) %>%
dplyr::mutate(corridor_path = trimws(.data$corridor_path)) %>%
dplyr::select(-.data$name) %>%
dplyr::rename(detector_path = .data$value)
dplyr::mutate(corridor_path = trimws(corridor_path)) %>%
dplyr::select(-name) %>%
dplyr::rename(detector_path = value)

# Rnode paths
rnode_paths <- tibble::enframe(
xml2::xml_path(xml2::xml_find_all(metro_config, "//r_node"))
) %>%
dplyr::transmute(rnode_path = .data$value)
dplyr::transmute(rnode_path = value)

# Corridor paths
corridor_paths <- tibble::enframe(
xml2::xml_path(xml2::xml_find_all(metro_config, "//corridor"))
) %>%
dplyr::transmute(corridor_path = .data$value)
dplyr::transmute(corridor_path = value)

# ------------------
# ATTRIBUTES (rnodes & detectors)
Expand Down Expand Up @@ -178,9 +177,9 @@ pull_configuration <- function(return_opt = "in_memory", .quiet = TRUE) {

config_tidy <- configuration %>%
dplyr::select(
-.data$rnode, -.data$rnode_path,
-.data$detector, -.data$detector_path,
-.data$corridor_path
-rnode, -rnode_path,
-detector, -detector_path,
-corridor_path
) %>%
dplyr::mutate(date = Sys.Date())

Expand Down
38 changes: 19 additions & 19 deletions R/pull_sensor.R
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@
#' @importFrom tibble enframe as_tibble
#' @importFrom jsonlite fromJSON
#' @importFrom dplyr bind_cols rename
#' @importFrom rlang .data
#' @importFrom cli cli_alert
#'
#' @family loop sensor functions
Expand Down Expand Up @@ -134,25 +133,26 @@ extension_pull <- function(ext, ext_name, sensor, pull_date, quiet = TRUE) {

df_default <- tibble::as_tibble(NA)

try(df_default <- tibble::enframe(
jsonlite::fromJSON(
txt = paste0(
"http://data.dot.state.mn.us/trafdat/metro/",
pull_year,
"/",
pull_year,
pull_month,
pull_day,
"/",
sensor,
".",
ext,
"30.json"
try(
df_default <- tibble::enframe(
jsonlite::fromJSON(
txt = paste0(
"http://data.dot.state.mn.us/trafdat/metro/",
pull_year,
"/",
pull_year,
pull_month,
pull_day,
"/",
sensor,
".",
ext,
"30.json"
)
)
)
) %>%
dplyr::select(.data$value),
silent = quiet
) %>%
dplyr::select(value),
silent = quiet
)
names(df_default) <- ext_name

Expand Down
3 changes: 1 addition & 2 deletions R/pull_sensor_ids.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
#' @importFrom dplyr transmute
#' @importFrom tibble enframe
#' @importFrom utils download.file
#' @importFrom rlang .data
#'
#' @export
pull_sensor_ids <- function(.quiet = TRUE) {
Expand All @@ -24,5 +23,5 @@ pull_sensor_ids <- function(.quiet = TRUE) {
metro_config <- xml2::read_xml(gzfile(tmp))

tibble::enframe(trimws(xml2::xml_attr(xml2::xml_find_all(metro_config, "//detector"), "name"))) %>%
dplyr::transmute(detector = .data$value)
dplyr::transmute(detector = value)
}
1 change: 0 additions & 1 deletion R/scrub.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#' @export
#'
scrub_sensor <- function(sensor_data, interval_length = NA) {

sensor_data[!duplicated(sensor_data, by = c("date", "hour", "min", "sensor"), fromLast = TRUE)]
}

Expand Down
69 changes: 40 additions & 29 deletions data-raw/logo.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,43 +29,54 @@ counties <- tigris::counties(
"Washington"
))

roads_sf <- roads(state = "MN",
year = 2020,
county = c(
"Hennepin",
"Dakota",
"Carver",
"Ramsey",
"Anoka",
"Scott",
"Washington")) %>%
roads_sf <- roads(
state = "MN",
year = 2020,
county = c(
"Hennepin",
"Dakota",
"Carver",
"Ramsey",
"Anoka",
"Scott",
"Washington"
)
) %>%
sf::st_simplify()


map_plot <- ggplot() +
geom_sf(data = counties,
color = "gray80",
fill = "white",
lwd = 0) +
geom_sf(data = roads_sf %>%
filter(RTTYP %in% c(
"U",
"I",
"S"
)),
mapping = aes(color = MTFCC),
size = 0.7,
show.legend = F) +
scale_color_brewer(direction = -1,
palette = "PuBu") +
geom_sf(
data = counties,
color = "gray80",
fill = "white",
lwd = 0
) +
geom_sf(
data = roads_sf %>%
filter(RTTYP %in% c(
"U",
"I",
"S"
)),
mapping = aes(color = MTFCC),
size = 0.7,
show.legend = F
) +
scale_color_brewer(
direction = -1,
palette = "PuBu"
) +
theme_void() +
theme_transparent()

map_plot
ggsave(plot = map_plot,
width = 7.36,
height = 6.92,
filename = "data-raw/subplot.png", device = "png", dpi = 300)
ggsave(
plot = map_plot,
width = 7.36,
height = 6.92,
filename = "data-raw/subplot.png", device = "png", dpi = 300
)

print(sticker(
subplot = "data-raw/subplot.png",
Expand Down
2 changes: 2 additions & 0 deletions man/tc.sensors-package.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion tests/testthat.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
library(testthat)
library(tc.sensors)

set.seed(as.numeric(format(Sys.Date(), "%d")) + 24601)
test_check("tc.sensors")
5 changes: 5 additions & 0 deletions tests/testthat/setup.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# setup

yesterday <- as.Date(Sys.Date() - 3)

config <- pull_configuration()
4 changes: 1 addition & 3 deletions tests/testthat/test-add_distance.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
testthat::skip_if_offline()


test_that("distance is calculated correctly", {
config <- as.data.table(pull_configuration())
Expand All @@ -24,5 +22,5 @@ test_that("distance is calculated correctly", {
testthat::expect_true(nrow(th22) == nrow(pulled))

# max distance no greater than 3 miles
testthat::expect_true(max(pulled$distance, na.rm = T) < 3)
testthat::expect_true(max(pulled$distance, na.rm = TRUE) < 3)
})
7 changes: 0 additions & 7 deletions tests/testthat/test-add_weather.R
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@

testthat::skip_if_offline(host = "metrocouncil.org")
testthat::skip_on_ci()

test_that("Weather data functions as expected", {
config <- pull_configuration()


yesterday <- as.Date(Sys.Date() - 3)


config_sample <- dplyr::filter(config, config$detector_abandoned == "f") %>%
dplyr::sample_n(1)

Expand Down
Loading

0 comments on commit 20d9047

Please sign in to comment.