Skip to content

Commit b2ac90a

Browse files
HillHill
authored andcommitted
create_report accepts character parameter names, and isn't limited to those in the params object.
1 parent 487dfb6 commit b2ac90a

File tree

4 files changed

+30
-9
lines changed

4 files changed

+30
-9
lines changed

R/create_report.R

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#' @param org organization (short name in WQP) whose data should be used to build the report. Multiple organization names can be input as a character vector; names will be homogenized and data will be pooled for analysis purposes. A list of acceptable organization names is available in this csv: https://cdx.epa.gov/wqx/download/DomainValues/Organization.CSV.
66
#' @param startDate start date for data used in the report, in format `\%m-\%d-\%Y`
77
#' @param endDate final date for data used in the report, in format `\%m-\%d-\%Y`
8-
#' @param parameters parameters to use in report. Must be entered as an index of acceptable parameters listed in params$params (e.g., `parameters = c(1:4,7)`)
8+
#' @param parameters parameters to use in report. Can be entered as exact characteristic names (for a complete list, see: https://cdx.epa.gov/wqx/download/DomainValues/Characteristic.CSV). Alternatively, this argument can be a numeric index of parameters listed in params$params (e.g., `parameters = c(1:4,7)`)
99
#' @param extFile name of report-generating script, located in the inst/extdata folder of the R8WD R package
1010
#' @param prompt_user if TRUE, user is prompted to use one of the organization short names in the `tribes` object provided with `R8WD`. .
1111
#' @param output either 'docx' or 'html'
@@ -21,7 +21,7 @@
2121
create_report <- function(org = 'TURTLEMT',
2222
startDate = '01-01-2015',
2323
endDate = '12-31-2022',
24-
parameters = 1:11,
24+
parameters = c('Total Phosphorus, mixed forms', 'Total Nitrogen, mixed forms', 'Escherichia coli', 'Dissolved oxygen (DO)', 'Temperature', 'Temperature, water', 'pH', 'Turbidity', 'Conductivity', 'Total suspended solids'),
2525
extFile = 'script_generateReport.qmd',
2626
prompt_user = TRUE,
2727
output = 'html',
@@ -70,7 +70,13 @@ create_report <- function(org = 'TURTLEMT',
7070
### add params
7171
# parameters
7272
# REPLACE_PARAMS
73-
newParams <- paste0(paste0(gsub(x = as.character(parameters), pattern = "\'|\"", replacement = ''), collapse = ','))
73+
if (all(is.numeric(parameters))) {
74+
newParams <- paste0(paste0(gsub(x = as.character(parameters), pattern = "\'|\"", replacement = ''), collapse = ','))
75+
} else if (all(is.character(parameters))) {
76+
# newParams <- dput(parameters)
77+
newParams <- paste0('"', x = paste0(gsub(x = as.character(parameters), pattern = "\'|\"", replacement = "'"), collapse='","'), '"')
78+
}
79+
7480
# tst <- gsub(x = readLines(targetFile), pattern = token, replacement = newParams)
7581
newText <- gsub(x = newText, pattern = '\'REPLACE_PARAMS\'', replacement = newParams)
7682

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ remotes::install_github("USEPA/R8WD")
3838
```
3939

4040

41-
## Example
41+
## A brief example
4242

4343
After installing `R8WD`, it can be loaded and a quarto report generated. A list of organization names available in the Water Quality Portal is available in this csv: https://cdx.epa.gov/wqx/download/DomainValues/Organization.CSV. Note that a given organization may not have data available for the time period or parameters selected.
4444

@@ -47,7 +47,10 @@ After installing `R8WD`, it can be loaded and a quarto report generated. A list
4747
library(R8WD)
4848

4949
# generate a report
50-
create_report('BLCKFEET')
50+
org_name <- 'BLCKFEET'
51+
params <- c("Total Phosphorus, mixed forms","Total Nitrogen, mixed forms","Escherichia coli","Dissolved oxygen (DO)")
52+
53+
create_report(org = org_name, parameters = params)
5154
```
5255

5356
This will create and open a quarto document in RStudio. Render the quarto document to html output by pressing `Ctrl+Shift+K`.

inst/extdata/script_generateReport.qmd

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@ replicate_variation_tolerance <- 30
3232
#| echo: false
3333
#| output: false
3434
#| include: false
35+
if (all(is.numeric(params_to_use))) {
36+
### accommodate character parameters or numeric index of params object
37+
params_used <- params$params[params_to_use]
38+
unique_params <- unique(params$new_param[params_to_use])
39+
} else {
40+
params_used <- params_to_use
41+
### unique parameters are only defined for those in params$params
42+
unique_params <- unique(c(params_to_use[which(!(params_to_use) %in% params$params)], params$new_param[na.omit(match(params_to_use, params$params))]))
43+
}
3544
3645
plot_summary_period <- 'month'
3746
if (draft_report) {
@@ -42,7 +51,7 @@ if (draft_report) {
4251
4352
tribeData2 <- getWQP(
4453
organization = Tribal_org,
45-
characteristicName = params$params[params_to_use],
54+
characteristicName = params_used, # params$params[params_to_use],
4655
startDate = start_date,
4756
endDate = end_date,
4857
multiplier = 0.5)
@@ -79,7 +88,8 @@ if (any(unique(allSummary$data[allSummary$data$CharacteristicName == 'TN', 'Resu
7988
8089
included_params <- sentencify(unique(tribeData2$CharacteristicName))
8190
82-
params_not_found <- unique(params$new_param[params_to_use])[!(unique(params$new_param[params_to_use]) %in% unique(tribeData2$CharacteristicName))]
91+
# params_not_found <- unique(params$new_param[params_to_use])[!(unique(params$new_param[params_to_use]) %in% unique(tribeData2$CharacteristicName))]
92+
params_not_found <- unique_params[!(unique_params %in% unique(tribeData2$CharacteristicName))]
8393
if (length(params_not_found) == 0) {
8494
params_not_found_message <- "All core parameters sought for this report were available in the organization's WQP data."
8595
} else if (length(params_not_found) == 1) {

man/create_report.Rd

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)