-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil_functions.R
83 lines (61 loc) · 2.31 KB
/
util_functions.R
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
# File: utils.R
createDownloadHandler <- function(input, file_map_name, file_timeseries_name, map_data, timeseries_data) {
downloadHandler(
filename = function() {
if (input$table_data == "map") {
file_map_name
} else {
file_timeseries_name
}
},
content = function(file) {
library(openxlsx)
data_to_write <- if (input$table_data == "map") {
map_data
} else {
timeseries_data
}
write.xlsx(data_to_write, file)
}
)
}
# File: utils.R
createNavObserver <- function(input, session, pages) {
lapply(names(pages), function(page) {
observeEvent(input[[paste0("nav_", page)]], {
updateQueryString(paste0("?page=", pages[[page]]), mode = "push")
})
})
}
# Helper function to format numbers with commas and appropriate decimal places
format_number <- function(number) {
# Check if the number is an integer
if (number %% 1 == 0) {
return(format(number, big.mark = ",", scientific = FALSE))
}
# Calculate the number of significant figures
sig_digits <- nchar(gsub("0+$", "", gsub("\\.", "", as.character(number))))
if (sig_digits > 3) {
# Round to nearest integer if more than 3 significant figures
rounded_number <- round(number)
return(format(rounded_number, big.mark = ",", scientific = FALSE))
} else {
# Otherwise, round to 2 decimal places
rounded_number <- round(number, 2)
return(format(rounded_number, big.mark = ",", scientific = FALSE, nsmall = 2))
}
}
#module_data_functions.R
# Function to handle variable select and button events
handle_variable_select_and_buttons <- function(id_prefix, variables, input, output, session) {
selected_variables <- reactive({ vars <- variables(); vars[vars != "Total"] })
output[[paste0("variable_select_", id_prefix)]] <- renderUI({
checkboxGroupInput(paste0("variables_", id_prefix), "Choose variables to add to chart", choices = variables(), selected = selected_variables())
})
observeEvent(input[[paste0("select_all_button_", id_prefix)]], {
updateCheckboxGroupInput(session, paste0("variables_", id_prefix), selected = variables())
})
observeEvent(input[[paste0("deselect_all_button_", id_prefix)]], {
updateCheckboxGroupInput(session, paste0("variables_", id_prefix), selected = character(0))
})
}