Skip to content

Commit 0bbffa2

Browse files
committed
Update documentation and internal functions; change package title and improve README descriptions
1 parent 306d140 commit 0bbffa2

File tree

80 files changed

+12972
-2030
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

80 files changed

+12972
-2030
lines changed

NAMESPACE

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
# Generated by roxygen2: do not edit by hand
22

3+
export(adorn_dates)
34
export(age_sex_pyramid)
5+
export(base_plotly)
46
export(col_chart)
57
export(epi_curve)
68
export(epi_map)
79
export(line_chart)
810
export(llm_interpret)
11+
export(palette_gen)
12+
export(param_rename)
13+
export(plotly_legend_pos)
914
export(point_chart)
1015
import(ISOweek)
1116
import(RColorBrewer)

R/age_sex_pyramid.R

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#' Generate an Age-Sex Pyramid
22
#'
3-
#' This function creates an age-sex pyramid visualization, either as a static ggplot or an interactive plotly chart.
3+
#' This function creates an age-sex pyramid visualisation, either as a static ggplot or an interactive plotly chart.
44
#' The function can take either a line list (ungrouped data) or already grouped data as input.
55
#' When using a line list, the function processes the data, groups it by age and sex, and then generates the pyramid.
66
#' If grouped data is provided, it directly creates the pyramid.
@@ -163,14 +163,14 @@ age_sex_pyramid <- function(
163163
if (is.null(var_map$age_group) || is.null(var_map$sex) || is.null(var_map$value)) {
164164
stop("For grouped data, var_map must include 'age_group', 'sex', and 'value'")
165165
}
166-
166+
167167
# Create a new data frame with standardized column names
168168
.grp_df <- data.frame(
169169
age_group = params$df[[var_map$age_group]],
170170
sex = params$df[[var_map$sex]],
171171
value = params$df[[var_map$value]]
172172
)
173-
173+
174174
# Add confidence limits if they exist and are requested
175175
if (params$conf_limits) {
176176
if (is.null(var_map$lowercl) || is.null(var_map$uppercl)) {
@@ -188,7 +188,7 @@ age_sex_pyramid <- function(
188188
.grp_df$lowercl <- .grp_df$value
189189
.grp_df$uppercl <- .grp_df$value
190190
}
191-
191+
192192
# Ensure age_group is ordered correctly
193193
# Extract numeric part from age group labels for sorting
194194
age_order <- order(as.integer(sub("^(\\d+).*", "\\1", sub("[<+]", "", .grp_df$age_group))))
@@ -227,10 +227,10 @@ age_sex_pyramid <- function(
227227
return(result)
228228
}else{
229229
# plotly implementation of dynamic age-sex-pyramid
230-
230+
231231
# Process data similarly to static version
232232
var_map <- params$var_map
233-
233+
234234
if (params$grouped == FALSE) {
235235
.grp_df <- process_line_list_for_age_sex_pyramid(
236236
df = params$df,
@@ -243,14 +243,14 @@ age_sex_pyramid <- function(
243243
if (is.null(var_map$age_group) || is.null(var_map$sex) || is.null(var_map$value)) {
244244
stop("For grouped data, var_map must include 'age_group', 'sex', and 'value'")
245245
}
246-
246+
247247
# Create a new data frame with standardized column names
248248
.grp_df <- data.frame(
249249
age_group = params$df[[var_map$age_group]],
250250
sex = params$df[[var_map$sex]],
251251
value = params$df[[var_map$value]]
252252
)
253-
253+
254254
# Add confidence limits if they exist and are requested
255255
if (params$conf_limits) {
256256
if (is.null(var_map$lowercl) || is.null(var_map$uppercl)) {
@@ -268,43 +268,43 @@ age_sex_pyramid <- function(
268268
.grp_df$lowercl <- .grp_df$value
269269
.grp_df$uppercl <- .grp_df$value
270270
}
271-
271+
272272
# Ensure age_group is ordered correctly
273273
# Extract numeric part from age group labels for sorting
274274
age_order <- order(as.integer(sub("^(\\d+).*", "\\1", sub("[<+]", "", .grp_df$age_group))))
275275
.grp_df$age_group <- factor(.grp_df$age_group, levels = unique(.grp_df$age_group[age_order]))
276276
}
277-
278-
# Create the plotly visualization
277+
278+
# Create the plotly visualisation
279279
male_data <- .grp_df[.grp_df$sex == "Male", ]
280280
female_data <- .grp_df[.grp_df$sex == "Female", ]
281-
282-
# Convert values for males to negative for visualization
281+
282+
# Convert values for males to negative for visualisation
283283
male_data$value <- -male_data$value
284284
if (params$conf_limits) {
285285
male_data$lowercl <- -male_data$lowercl
286286
male_data$uppercl <- -male_data$uppercl
287287
}
288-
288+
289289
# Calculate maximum range for symmetric axis
290290
if (params$conf_limits) {
291-
all_x <- c(male_data$value, female_data$value, male_data$lowercl, male_data$uppercl,
291+
all_x <- c(male_data$value, female_data$value, male_data$lowercl, male_data$uppercl,
292292
female_data$lowercl, female_data$uppercl)
293293
} else {
294294
all_x <- c(male_data$value, female_data$value)
295295
}
296296
min_x <- min(all_x)
297297
max_x <- max(all_x)
298298
max_range <- max(abs(min_x), max_x)
299-
299+
300300
# Generate symmetric tick values and positive labels
301301
positive_ticks <- pretty(c(0, max_range), n = ceiling(params$x_breaks / 2))
302302
tickvals <- sort(unique(c(-positive_ticks, positive_ticks)))
303303
ticktext <- as.character(abs(tickvals))
304-
304+
305305
# Create the plot
306306
p <- plot_ly(showlegend = TRUE)
307-
307+
308308
# Add male bars
309309
p <- add_trace(p,
310310
x = male_data$value,
@@ -315,7 +315,7 @@ age_sex_pyramid <- function(
315315
orientation = 'h',
316316
hoverinfo = "text",
317317
hovertext = paste0("Male: ", round(abs(male_data$value), 2), "<br>Age: ", male_data$age_group))
318-
318+
319319
# Add female bars
320320
p <- add_trace(p,
321321
x = female_data$value,
@@ -326,7 +326,7 @@ age_sex_pyramid <- function(
326326
orientation = 'h',
327327
hoverinfo = "text",
328328
hovertext = paste0("Female: ", round(female_data$value, 2), "<br>Age: ", female_data$age_group))
329-
329+
330330
# Add confidence limits if requested
331331
if (params$conf_limits) {
332332
p <- add_trace(p,
@@ -370,7 +370,7 @@ age_sex_pyramid <- function(
370370
hoverinfo = "text",
371371
hovertext = paste0("Female Upper CI: ", round(female_data$uppercl, 2), "<br>Age: ", female_data$age_group))
372372
}
373-
373+
374374
# Update layout with corrected titles and custom ticks
375375
p <- layout(p,
376376
title = params$legend_title,
@@ -400,7 +400,7 @@ age_sex_pyramid <- function(
400400
showlegend = TRUE,
401401
legend = get_plotly_legend_position(params$legend_position)
402402
)
403-
403+
404404
return(p)
405405
}
406406
}

R/agesex_pyramid_grouped.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#' If TRUE, the data frame should include the lowercl and uppercl columns.
1919
#'
2020
#' @return Returns an age-sex pyramid as a ggplot object
21+
#' @keywords internal
2122
#'
2223
#' @import ggplot2
2324
#' @import dplyr

R/base_gg.R

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
2-
#' Creates base ggplot object for use across other functions.
3-
#' Parameters are not passed explicitly to the function, so
4-
#' function call needs to be proceeded by environment(base_gg) <- environment()
1+
#' Base ggplot object for line_chart, point_chart, and epi_curve
52
#'
6-
#' @return ggplot object
3+
#' @description Internal function to create base ggplot object. Note that the
4+
#' function call needs to be proceeded by environment(base_gg) <- environment()
5+
#' to ensure that the function can find variables in the calling environment.
76
#'
87
#' @examples
98
#' \dontrun{
109
#' environment(base_gg) <- environment()
1110
#' base <- base_gg()
1211
#' }
12+
#' @keywords internal
1313
base_gg <- function() {
1414

1515

R/base_plotly.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
21
#' Creates base plotly object for use across other functions.
32
#' Parameters are not passed explicitly to the function, so
43
#' function call needs to be proceeded by environment(base_plotly) <- environment()
54
#'
65
#' @return plotly object
6+
#' @keywords internal
7+
#' @export
78
#'
89
#' @examples
910
#' \dontrun{

R/data.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#' \item{SHAPE_Area}{}
2626
#' \item{geometry}{}
2727
#' }
28+
#' @keywords internal
2829
"PHEC_boundaries_2016"
2930

3031
#' Local Authority Districts (May 2023) Boundaries for London BGC
@@ -58,6 +59,7 @@
5859
#' \item{SHAPE_Area}{}
5960
#' \item{geometry}{}
6061
#' }
62+
#' @keywords internal
6163
"London_LA_boundaries_2023"
6264

6365
#' Countries (December 2023) Boundaries UK BUC
@@ -88,4 +90,5 @@
8890
#' \item{SHAPE_Area}{}
8991
#' \item{geometry}{}
9092
#' }
93+
#' @keywords internal
9194
"UK_boundaries_2023"

R/llm_intepret.R

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
#' Interpret Epidemiological Data or Visualisations using LLMs
2-
#'
2+
#'
33
#' @description
44
#' `r lifecycle::badge("experimental")`
5-
#'
5+
#'
66
#' This function interprets a given data frame or ggplot visualisation by sending it to a language model API via the elmer package. It supports multiple LLM providers, allowing users to specify the desired provider and model through environment variables.
77
#'
8-
#' @param input An input object, either a data frame or a ggplot object, representing the data or visualization to be interpreted.
8+
#' @param input An input object, either a data frame or a ggplot object, representing the data or visualisation to be interpreted.
99
#' @param word_limit Integer. The desired word length for the response. Defaults to 100.
1010
#' @param prompt_extension Character. Optional additional instructions to extend the standard prompt. Defaults to NULL.
1111
#' @return A character string containing the narrative or interpretation of the input object as generated by the LLM.
@@ -36,35 +36,35 @@ llm_interpret <- function(input,
3636
word_limit = 100,
3737
prompt_extension = NULL) {
3838
lifecycle::signal_stage("experimental", "llm_interpret()")
39-
39+
4040
# Validate input parameters
4141
if (!is.numeric(word_limit) || word_limit <= 0) {
4242
stop("word_limit must be a positive number")
4343
}
44-
44+
4545
if (!is.null(prompt_extension) && !is.character(prompt_extension)) {
4646
stop("prompt_extension must be NULL or a character string")
4747
}
48-
48+
4949
# Check for required environment variables with informative messages
5050
tryCatch({
5151
provider <- Sys.getenv("LLM_PROVIDER")
5252
if (provider == "") {
5353
stop("LLM_PROVIDER environment variable is not set. ",
5454
"Please set it to one of: 'openai', 'gemini', or 'claude'")
5555
}
56-
56+
5757
api_key <- Sys.getenv("LLM_API_KEY")
5858
if (api_key == "") {
5959
stop("LLM_API_KEY environment variable is not set. ",
60-
sprintf("For %s, please set the appropriate API key.",
60+
sprintf("For %s, please set the appropriate API key.",
6161
toupper(provider)))
6262
}
63-
63+
6464
model <- Sys.getenv("LLM_MODEL")
6565
if (model == "") {
6666
stop("LLM_MODEL environment variable is not set. ",
67-
sprintf("For %s, please set an appropriate model identifier.",
67+
sprintf("For %s, please set an appropriate model identifier.",
6868
toupper(provider)))
6969
}
7070
}, error = function(e) {
@@ -83,7 +83,7 @@ llm_interpret <- function(input,
8383
}, error = function(e) {
8484
stop("Failed to initialize chat client: ", conditionMessage(e))
8585
})
86-
86+
8787
# Set the system prompt with error handling
8888
tryCatch({
8989
chat$set_system_prompt(
@@ -114,7 +114,7 @@ llm_interpret <- function(input,
114114
if (nrow(input) == 0) {
115115
stop("Input data frame is empty")
116116
}
117-
117+
118118
# Convert data frame to JSON with error handling
119119
json_data <- tryCatch({
120120
jsonlite::toJSON(input, pretty = TRUE, auto_unbox = TRUE)
@@ -132,7 +132,7 @@ llm_interpret <- function(input,
132132
}
133133
else if (inherits(input, "ggplot")) {
134134
temp_file <- NULL
135-
135+
136136
# Use withCallingHandlers for cleanup even if error occurs
137137
withCallingHandlers({
138138
# Save the ggplot object as a temporary image

R/process_line_list_for_asp.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
#' @return A data frame that is aggregated by age group and sex, with columns for age group, sex, value (count),
2020
#' lower confidence limit, and upper confidence limit.
2121
#'
22+
#' @keywords internal
23+
#'
2224
#' @examples
2325
#' \dontrun{
2426
#' df <- epiviz::lab_data

0 commit comments

Comments
 (0)