Skip to content
Snippets Groups Projects
Commit 70ce2ee7 authored by Hayley Corson-Dosch's avatar Hayley Corson-Dosch
Browse files

Add fish as food pipeline

parent e394d89b
No related branches found
No related tags found
1 merge request!21Draft circle packing visualization
library(targets)
options(tidyverse.quiet = TRUE)
tar_option_set(packages = c("tidyverse",
"readxl",
"jsonlite"))
source('src/data_utils.R')
p0 <- list(
tar_target(
p0_sb_id,
'644ae0e0d34e45f6ddccf773'
)
)
p1 <- list(
tar_target(
p1_data_xlsx,
{
data_file <- 'Rec fish food_20230509_for USGS data release.xlsx'
out_file <- file.path('in', data_file)
sbtools::item_file_download(sb_id = p0_sb_id,
names = data_file,
destinations = out_file)
return(out_file)
},
format = 'file'
)
)
p2 <- list(
tar_target(
p2_data,
clean_input_data(data_file = p1_data_xlsx)
),
tar_target(
p2_price_json,
build_nested_json(
data = p2_data,
focal_columns = c('name' = 'admin', 'value' = 'total_value_species'),
out_file = '../public/total_price.json'
),
format = 'file'
)
)
c(p0, p1, p2)
clean_input_data <- function(data_file) {
data <- read_xlsx(data_file, na="NA")
names(data)<-make.names(names(data),unique = TRUE)
data |>
mutate(
family = ifelse(family == 'Salmonindae', 'Salmonidae', family),
species_common = ifelse(species_common == 'Bey?ehir bleak',
'Beyşehir bleak',
species_common)
)
}
build_nested_json <- function(data, focal_columns, out_file) {
data_list <- data |>
filter(!is.na(focal_columns[['value']])) |>
group_by(name = family) |>
group_modify(~ {
species_list <- .x |>
group_by(name = species_common) |>
group_modify(~ {
country_list <- .x |>
select(!!focal_columns) |>
split(nrow(.x))
return(tibble('children' = country_list))
})
return(tibble('children' = list(species_list)))
})
json_data <- jsonlite::toJSON(data_list, auto_unbox = TRUE, pretty = TRUE)
json_data <- paste0('{\n "name": "fish", "children":', json_data, '\n}')
write(json_data, file = out_file)
return(out_file)
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment