-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathglobal.R
More file actions
145 lines (112 loc) · 4.33 KB
/
global.R
File metadata and controls
145 lines (112 loc) · 4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
library(shiny)
library(shinycssloaders)
library(promises)
library(future)
library(leaflet)
library(DT)
library(dplyr)
library(purrr)
library(glue)
library(fcds)
future::plan(multiprocess)
# Setup -------------------------------------------------------------------
cancer_vars_ignore <- c("cancer_site_specific", "cancer_ICDO3_conversion")
cancer_vars_include <- setdiff(fcds_vars("cancer"), cancer_vars_ignore)
if (!exists("fcds_base")) {
fcds_shiny_cache <- file.path(fcds_default_data_path(), "fcds-shiny.rds")
if (!file.exists(fcds_shiny_cache)) {
fcds_base <- fcds_load() %>%
mutate(cancer_ICDO3_histology = sub("\\d$", "", cancer_ICDO3_histology)) %>%
group_by(year_group, year, county_name, county_fips, age_group, sex, race, origin,
!!!rlang::syms(cancer_vars_include)) %>%
count_fcds() %>%
ungroup()
message("Caching base shiny data to ", fcds_shiny_cache)
saveRDS(fcds_base, fcds_shiny_cache, compress = FALSE)
} else {
fcds_base <- readRDS(fcds_shiny_cache)
}
}
add_all <- function(x, ...) UseMethod("add_all")
add_all.character <- function(x, all = "") c("All" = all, x)
add_all.list <- function(x, all = "") c(list("All" = all), x)
add_all.NULL <- function(x, ...) NULL
# Initial Cancer Selection Options ----
c_opt_vars <- setdiff(fcds_vars("cancer"), cancer_vars_ignore)
fcds_cancer_types <- fcds_base %>%
select(c_opt_vars, n) %>%
group_by(!!!rlang::syms(c_opt_vars)) %>%
summarize(n = sum(n)) %>%
ungroup()
c_opt <- c_opt_vars %>%
set_names() %>%
map(possibly(fcds_const, NULL)) %>%
map(sort)
c_opt$cancer_ICDO3_morphology <-
fcds::seer_icd_o_3 %>%
distinct(histology_description, morphology, morphology_description) %>%
mutate_at(quos(starts_with("morphology")), as.character) %>%
split(.$histology_description) %>%
map(~ {
.x$morphology_description <- glue_data(.x, "{morphology} - {morphology_description}")
.x <- .x %>% arrange(morphology_description)
set_names(.x$morphology, paste(.x$morphology_description))
})
c_opt$cancer_ICDO3_histology <-
fcds::seer_icd_o_3 %>%
mutate_all(as.character) %>%
mutate(histology = sub("\\d$", "", histology)) %>%
distinct(histology, histology_description) %>%
mutate(choice = glue("{histology} {histology_description}")) %>%
arrange(choice) %>%
{set_names(.$histology, .$choice)}
filter_cancer_icdo3_histology <- function(data, values = NULL) {
if (is.null(values)) return(values)
values <- paste0("^(", paste(values, collapse = "|"), ")")
data[grepl(values, data$cancer_ICDO3_histology), ]
}
# Initial Demographic Exploration Options ----
opt_year_group <- fcds_const("year_group") %>% rev() %>% add_all()
opt_cancer_site_group <- fcds_const("cancer_site_group", full = TRUE) %>%
{ set_names(.$label, glue_data(., "{label} ({value})")) } %>%
sort() %>%
add_all()
opt_sex <- fcds_const("sex") %>% add_all()
opt_race <- fcds_const("race") %>% add_all()
opt_age_group <- c("Age-Adjusted" = "", fcds_const("age_group"))
opt_origin <- fcds_const("origin") %>% add_all()
opt_county_name <- c("Moffitt Catchment Area" = "moffitt_catchment",
fcds_const("county_name")) %>% add_all()
# Helpers -----------------------------------------------------------------
select_multiple <- function(choices, ...) {
choices <- add_all(choices, all = "")
selectizeInput(..., choices = choices, multiple = TRUE, options = list(plugins = list('remove_button')))
}
select_single <- function(choices, ...) {
choices <- add_all(choices, all = "All")
selectizeInput(..., choices = choices, multiple = FALSE)
}
cat_ts <- function(...) {
cat("\n", strftime(Sys.time(), "[%F %T] "), ..., sep = "")
}
fl_county_boundaries <- fcds::county_fips_fl %>%
fcds:::join_boundaries_fl()
points2sf <- function(lng, lat) {
map2(lng, lat, ~ sf::st_point(c(.x, .y))) %>%
sf::st_sfc(crs = 4326)
}
contains_point <- function(data, pt) {
if (is.null(pt)) return(data[0, ])
quiet_st_within <- fcds:::quietly(sf::st_within)
rows_containing <-
quiet_st_within(points2sf(pt$lng, pt$lat), data) %>%
map(as.numeric) %>%
reduce(union)
data[rows_containing, ]
}
moffitt_blue <- function(n = 3) {
colorspace::sequential_hcl(n, 250, 64, c(37, 90), power = 1, rev = TRUE)
}
moffitt_red <- function(n = 3) {
colorspace::sequential_hcl(n, 4.350477, 142.1399, c(50.48059, 90), power = 1, rev = TRUE)
}