Skip to content

Commit

Permalink
French changes to table of prop female weights
Browse files Browse the repository at this point in the history
  • Loading branch information
cgrandin committed Oct 21, 2024
1 parent 0a672f0 commit cc47d0a
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 108 deletions.
117 changes: 117 additions & 0 deletions R/table-prop-female-weights.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
#' Create a table of weights used in the proportion female analysis
#'
#' @param samples Output from either [gfdata::get_commercial_samples()] or
#' [gfdata::get_survey_samples()] depending on the value of `type`
#' @param type Which type to return, "commercial" or "survey"
#' @param ord A vector of survey names as they appear in the output data frame,
#' in the order you want them in the output. If `NULL`, order will be ignored
#' @param return_df If `TRUE`, return a data .frame instead of a
#' @param col_widths Widths for columns, except the Parameter column
#' the [csasdown::csas_table()]
#' @param end_yr The last year to include in the table
#' [csasdown::csas_table()]
#' @param bold_headers If `TRUE`, make all column headers bold
#' @param ... Arguments passed to [csasdown::csas_table()]
#'
#' @return A [csasdown::csas_table()]
#' @export
table_prop_female_weights <- function(samples = NULL,
type = c("commercial", "survey"),
end_yr = 2019,
ord = c("QCS", "HS", "WCVI", "WCHG"),
return_df = FALSE,
col_widths = NULL,
bold_headers = TRUE,
...){

type <- match.arg(type)
if(is.null(samples)){
if(type == "commercial"){
stop("`samples` is `NULL`. It must be output of ",
"gfdata::get_commercial_samples()", call. = FALSE)
}else{
stop("`samples` is `NULL`. It must be output of ",
"gfdata::get_survey_samples()", call. = FALSE)
}
}
if(type == "commercial"){
d <- props_comm_data_summary(samples)
}else{
d <- props_surv_data_summary(samples)
}

d <- d |>
filter(d$Year <= end_yr)

if(type == "survey"){
# HACK
d <- d |>
mutate(Survey = ifelse(is.na(Survey), "HS", Survey))
}

if(type == "survey" && !is.null(ord)){
d <- imap(ord, ~{
j <- d |>
filter(Survey == .x)
}) |>
map_df(~{.x})
}

if(return_df){
return(d)
}
# Translate column names
survey_sym <- sym(tr("Survey"))
year_sym <- sym(tr("Year"))
num_trips_sym <- sym(tr("Number of trips"))
num_samples_sym <- sym(ifelse(fr(),
"Nombre d'échant-illons",
"Number of samples"))
num_weights_m_sym <- sym(tr("Number of weights - Male"))
num_weights_f_sym <- sym(tr("Number of weights - Female"))

if(type == "survey"){
d <- d |>
mutate(Survey = tr(Survey)) |>
rename(!!survey_sym := `Survey`,
!!year_sym := `Year`,
!!num_samples_sym := `Number of samples`,
!!num_weights_m_sym := `Number of weights - Male`,
!!num_weights_f_sym := `Number of weights - Female`)
}else{
d <- d |>
rename(!!year_sym := `Year`,
!!num_trips_sym := `Number of trips`,
!!num_samples_sym := `Number of samples`,
!!num_weights_m_sym := `Number of weights - Male`,
!!num_weights_f_sym := `Number of weights - Female`)
}

names(d) <- names(d) |>
str_wrap(16, whitespace_only = FALSE) |>
linebreak()

if(bold_headers){
names(d) <- paste0("\\textbf{", names(d), "}")
}

tab <- csas_table(d,
format = "latex",
...)

if(type == "survey"){
survs <- unique(d$Survey)
wch <- map(survs, ~{which(d$Survey == .x)})
end_rows <- map_dbl(wch, ~{tail(.x, 1)})
end_rows <- end_rows[-length(end_rows)]
tab <- tab |>
row_spec(end_rows, hline_after = TRUE)
}

if(!is.null(col_widths)){
tab <- tab |>
column_spec(2:ncol(d), width = col_widths)
}

tab
}
108 changes: 1 addition & 107 deletions R/table-prop-female.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ table_prop_female <- function(prop_lst,
filter(Year <= end_yr) |>
mutate(Year = as.character(Year))

means <- vec2df(c(tr("Mean"), f(map_dbl(d[-1], ~{mean(.x, na.rm = TRUE)}), 2)),
means <- vec2df(c(tr("Mean"), f(map_.dbl(d[-1], ~{mean(.x, na.rm = TRUE)}), 2)),
nms = names(k))

x <- bind_rows(k, means)
Expand Down Expand Up @@ -95,109 +95,3 @@ table_prop_female <- function(prop_lst,
}
out
}

#' Create a table of weights used in the proportion female analysis
#'
#' @param samples Output from either [gfdata::get_commercial_samples()] or
#' [gfdata::get_survey_samples()] depending on the value of `type`
#' @param type Which type to return, "commercial" or "survey"
#' @param ord A vector of survey names as they appear in the output data frame,
#' in the order you want them in the output. If `NULL`, order will be ignored
#' @param return_df If `TRUE`, return a data .frame instead of a
#' @param col_widths Widths for columns, except the Parameter column
#' the [csasdown::csas_table()]
#' @param end_yr The last year to include in the table
#' [csasdown::csas_table()]
#' @param ... Arguments passed to [csasdown::csas_table()]
#'
#' @return A [csasdown::csas_table()]
#' @export
table_prop_female_weights <- function(samples = NULL,
type = c("commercial", "survey"),
end_yr = 2019,
ord = c("QCS", "HS", "WCVI", "WCHG"),
return_df = FALSE,
col_widths = NULL,
...){

type <- match.arg(type)
if(is.null(samples)){
if(type == "commercial"){
stop("`samples` is `NULL`. It must be output of ",
"gfdata::get_commercial_samples()", call. = FALSE)
}else{
stop("`samples` is `NULL`. It must be output of ",
"gfdata::get_survey_samples()", call. = FALSE)
}
}
if(type == "commercial"){
d <- props_comm_data_summary(samples)
}else{
d <- props_surv_data_summary(samples)
}

d <- d |>
filter(d$Year <= end_yr)

if(type == "survey"){
# HACK
d <- d |>
mutate(Survey = ifelse(is.na(Survey), "HS", Survey))
}

if(type == "survey" && !is.null(ord)){
d <- imap(ord, ~{
j <- d |>
filter(Survey == .x)
}) |>
map_df(~{.x})
}

if(return_df){
return(d)
}
# Translate column names
survey_sym <- sym(tr("Survey"))
year_sym <- sym(tr("Year"))
num_trips_sym <- sym(tr("Number of trips"))
num_samples_sym <- sym(tr("Number of samples"))
num_weights_m_sym <- sym(tr("Number of weights - Male"))
num_weights_f_sym <- sym(tr("Number of weights - Female"))

if(type == "survey"){
d <- d |>
mutate(Survey = tr(Survey)) |>
rename(!!survey_sym := `Survey`,
!!year_sym := `Year`,
!!num_samples_sym := `Number of samples`,
!!num_weights_m_sym := `Number of weights - Male`,
!!num_weights_f_sym := `Number of weights - Female`)
}else{
d <- d |>
rename(!!year_sym := `Year`,
!!num_trips_sym := `Number of trips`,
!!num_samples_sym := `Number of samples`,
!!num_weights_m_sym := `Number of weights - Male`,
!!num_weights_f_sym := `Number of weights - Female`)
}

tab <- csas_table(d,
format = "latex",
...)

if(type == "survey"){
survs <- unique(d$Survey)
wch <- map(survs, ~{which(d$Survey == .x)})
end_rows <- map_dbl(wch, ~{tail(.x, 1)})
end_rows <- end_rows[-length(end_rows)]
tab <- tab |>
row_spec(end_rows, hline_after = TRUE)
}

if(!is.null(col_widths)){
tab <- tab |>
column_spec(2:ncol(d), width = col_widths)
}

tab
}
5 changes: 4 additions & 1 deletion man/table_prop_female_weights.Rd

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

0 comments on commit cc47d0a

Please sign in to comment.