Last active
November 22, 2017 13:49
-
-
Save slarge/c4f7cd05ffdaec72c3792a8a0c55ede2 to your computer and use it in GitHub Desktop.
Function to get stocks for an advice year
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Print list of stocks for a year | |
#advice_year(2018) | |
advice_year <- function(year = 2018, path = "~/") { | |
list_of_packages <- c("dplyr", "glue", "jsonlite", "flextable", "officer") | |
new_packages <- list_of_packages[!(list_of_packages %in% installed.packages()[,"Package"])] | |
if(length(new_packages)) install.packages(new_packages) | |
library(dplyr) | |
library(glue) | |
library(jsonlite) | |
library(flextable) | |
library(officer) | |
rawsd <- jsonlite::fromJSON("http://sd.ices.dk/services/odata3/StockListDWs3")$value %>% | |
filter(ActiveYear == 2017, | |
YearOfNextAssessment == year) %>% | |
mutate(stock_code = dplyr::case_when(YearOfLastAssessment <= 2016 ~ PreviousStockKeyLabel, | |
YearOfLastAssessment >= 2017 ~ StockKeyLabel, | |
TRUE ~ NA_character_), | |
stock_code = dplyr::case_when(stock_code == "cod.27.25-32" ~ "cod.27.24-32", | |
stock_code == "pan-flad" ~ "pand-flad", | |
stock_code == "ple.27.7fg" ~ " ple.27.7f-g", | |
!stock_code %in% c("cod.27.25-32", "pan-flad", "ple.27.7fg") ~ stock_code, | |
TRUE ~ NA_character_), | |
advice_url = glue::glue("www.ices.dk/sites/pub/Publication%20Reports/Advice/{YearOfLastAssessment}/{YearOfLastAssessment}/{stock_code}.pdf"), | |
valid_url = purrr::map(advice_url, httr::http_error) == FALSE) %>% | |
select(StockKeyDescription, | |
StockKeyLabel, | |
YearOfLastAssessment, | |
advice_url, | |
valid_url) | |
clean_table <- bind_rows(rawsd %>% | |
filter(valid_url == FALSE) %>% | |
mutate(advice_url = gsub("-", "–", advice_url)), | |
rawsd %>% | |
filter(valid_url != FALSE)) %>% | |
arrange(StockKeyLabel) %>% | |
select(-valid_url) | |
table_info <- data.frame(col_keys = colnames(clean_table), | |
col = c("Stock Description", "Stock code", | |
"Year of last assessment", "Most recent advice"), | |
stringsAsFactors = FALSE) | |
## Default body style ## | |
def_text_body <- fp_text(color = "black", font.size = 9, font.family = "Calibri", bold = FALSE, shading.color = "transparent") | |
def_cell_body <- fp_cell(background.color = "transparent", border = fp_border(color = "black")) | |
def_par_body <- fp_par(text.align = "left", padding.left = 3) | |
## Default header style ## | |
def_text_head <- update(def_text_body) # NO CHANGE | |
def_cell_head <- update(def_cell_body, background.color = "#E8EAEA") | |
def_par_head <- update(def_par_body, text.align = "center", padding = 0) | |
ft_style <- flextable(clean_table) %>% | |
set_header_df(mapping = table_info, key = "col_keys") %>% | |
flextable::style(pr_t = def_text_body, # fp_text | |
pr_p = def_par_body, | |
pr_c = def_cell_body, | |
part = "body") %>% | |
flextable::style(j = 3, | |
pr_p = fp_par(text.align = "center", padding.left = 3), | |
part = "body") %>% | |
flextable::style(pr_t = def_text_head , # fp_text | |
pr_p = def_par_head, | |
pr_c = def_cell_head, | |
part = "header") %>% | |
flextable::width(j = NULL, width = 18/2.54) %>% | |
flextable::height(i = NULL, height = 0.4, part = "all") %>% | |
flextable::autofit() | |
doc <- read_docx() %>% | |
body_add_flextable(value = ft_style) | |
print(doc, target = sprintf("%sadvice_%s.docx", path, year)) %>% invisible() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment