Skip to content

Commit

Permalink
Merge pull request #69 from tamnva/joss_manuscript
Browse files Browse the repository at this point in the history
feat: add more vignettes and update help
  • Loading branch information
tamnva authored Feb 11, 2024
2 parents 1ac8270 + 5ed13c2 commit b0b151a
Show file tree
Hide file tree
Showing 16 changed files with 872 additions and 88 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ export(updateCalibrationFile)
export(updateMultiFile)
export(updateSingleFile)
export(updatedFileContent)
export(userObjFunction)
export(userReadSwatOutput)
export(yearlyOutputLoc)
importFrom(doParallel,registerDoParallel)
importFrom(foreach,"%dopar%")
Expand Down
58 changes: 58 additions & 0 deletions R/userObjFunction.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@

#' User-defined objective function
#'
#' @description
#' This is a dummy function. There are countless number of objective functions,
#' which RSWAT cannot include all of them in here. Instead, RSWAT only
#' provides some of the most commonly used objective functions, such as the NSE,
#' KGE, aBIAS, R2. User can implement their own objective function and replace
#' this function. For more detail see RSWAT vignettes (user_objective_function).
#'
#' @examples
#' \donttest{
#' # Create example data of observed and simulated
#' obs <- list()
#' obs[[1]] <- runif(100)
#' obs[[2]] <- runif(100)
#'
#' # Simulated data
#' sim <- list()
#' sim[[1]] <- runif(100)
#' sim[[2]] <- runif(100)
#'
#' # Lets say now our objective function is the correlation R2 and the weight
#' # for the first variable (obs[[1]]) is 1 and for the second variable is 2
#'
#' # Create a custom objective function for this task
#' updatedUserObjFunction <- function(obs, sim){
#'
#' # Define output variable
#' output <- list()
#' output$perCriteria <- list()
#'
#' # R2 of the first variable
#' output$perCriteria[[1]] <- cor(obs[[1]], sim[[1]])**2
#'
#' # R2 of the second variable
#' output$perCriteria[[2]] <- cor(obs[[2]], sim[[2]])**2
#'
#' # Final objective function value (with different weights), must be with the name "output"
#' output$overalPerCriteria <- (1 * output$perCriteria[[1]] + 2 * output$perCriteria[[2]])/3
#'
#' for (i in 1:length(output$perCriteria)){
#' output$perCriteria[[i]] <- data.frame(userObjFunc = output$perCriteria[[i]])
#' }
#'
#' return(output)
#' }
#'
#' # Overwrite the userObjFunction with our updatedUserObjFunction
#' environment(updatedUserObjFunction) <- asNamespace('RSWAT')
#' assignInNamespace("userObjFunction", updatedUserObjFunction, ns = "RSWAT")
#' }
#'
#' @export

userObjFunction <- function(obs, sim){
output <- NA
}
18 changes: 18 additions & 0 deletions R/userReadSwatOutput.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

#' User-defined output extraction
#'
#' @description
#' This is a dummy function. As SWAT/SWAT+ have many outputs, RSWAT only provides
#' some standard output extraction. Users want to extract other model outputs can
#' implement their own function and replace this function. For more detail see
#' RSWAT vignettes (userReadSwatOutput).
#'
#' @examples
#'
#' # Please see RSWAT vignettes for an example
#'
#' @export

userReadSwatOutput <- function(){
output <- NA
}
10 changes: 10 additions & 0 deletions inst/R-SWAT/HTML/helpCheckCurrentSimulation.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
</head>
<body>

<p>You can only open this files when all SWAT simulations are finished. If SWAT is running on background, you can manually go to the working folder ./Output/CurrentSimulationReport.log and open it with any text editor (e.g., notepad)</p>

</body>
</html>
5 changes: 2 additions & 3 deletions inst/R-SWAT/HTML/helpOutputExtraction.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,15 @@ <h4><span style="color: #3366ff;">SWAT</span> project</h4>
<li>&rarr; Input to the <span style="color: #ff00ff;">Column</span> should be '<strong>7, 8</strong>', to the Reach should be <br />'<strong>1, 2 * 1, 2, 3 * 3</strong>'</li>
</ul>
</li>
<li>If you want to <strong><span style="color: #3366ff;">read from other files</span></strong>, please modified the source code file <strong><span style="color: #3366ff;">./R/userReadSwatOutput.R</span></strong>, more information about this could be found in the <strong><span style="color: #3366ff;">Section 4</span></strong> of R-SWAT wiki page https://github.com/tamnva/R-SWAT/wiki/R-SWAT-User-Manual</li>
<li>If you want to <strong><span style="color: #3366ff;">read from other files</span></strong>, please create a "<strong><span style="color: #3366ff;">userReadSwatOutput.R" </span></strong>and load this file to RSWAT (by click the check box below this table. For detail on how to create this file, please see RSWAT vignettes "userReadSwatOutput".</li>
<li>Please check the tick box below (Display corresponding observed file names) to ensure that all numbers you enter here appear in the display table. <strong><span style="color: #ff0000;">The information from this text box is very important, as the observed data files should have the same name as listed in here</span></strong></li>
</ul>
<h4><span style="color: #ff9900;">SWAT+</span> project</h4>
<ul>
<li>Currently, R-SWAT can extract output from different channel files (e.g., channel_sd_day.txt, channel_sd_month.txt, and so on). The <span style="color: #ff00ff;">Column</span> number indicates <span style="color: #0000ff;">which columns</span> in this output file you want to extract and the <span style="color: #ff00ff;">Reach</span> number indicates <span style="color: #0000ff;">which reaches</span> you want to extract. For example, if you want to extract from column <span style="color: #0000ff;">48</span> and for reach numbers <span style="color: #ff0000;">1</span> and <span style="color: #ff0000;">2</span>, and column <span style="color: #339966;">53</span> for reach numbers <span style="color: #339966;">1,2,3</span>. You write in the Column '<span style="color: #0000ff;">48</span>, <span style="color: #339966;">53</span>' and in the Reach '<span style="color: #ff0000;">1, 2</span> * <span style="color: #339966;">1, 2, 3</span>'</li>
<li>IMPORTANT: <strong> <span style="color: #3366ff;">Reach number is the "unit" in column 5 of the output file, you can see this when open the output file for in SWAT+ documentation https://swatplus.gitbook.io/io-docs/swat+-output-files/channel</span></strong></li>
<li>You can also read from other files by modifying the source code file<strong> <span style="color: #3366ff;">./R/userReadSwatOutput.R</span></strong></li>
<li>You can also read from other files by creating "<strong><span style="color: #3366ff;">userReadSwatOutput.R" </span></strong>file and load this file to RSWAT (by click the check box below this table. For detail on how to create this file, please see RSWAT vignettes "userReadSwatOutput".</li>
</ul>

</span>
</body>
</html>
10 changes: 10 additions & 0 deletions inst/R-SWAT/HTML/helpRunSWAT.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
</head>
<body>

<p>When you click this button, all settings are saved to the file 'RSWATObject.rds' in the working directory folder. The parameter sets are generated and SWAT are run in parallel. Running can take time, R is busy while calling SWAT on the background therefore, it might not response to any anything. Don't turn of R. You can check the current simulation status in the file ./Output/CurrentSimulationReport.log You still go to step 4 and click the 'Open file CurrentSimulationReport.log'. When all simulations are finished, you will see a table appear</p>

</body>
</html>
137 changes: 69 additions & 68 deletions inst/R-SWAT/R/runSWATUI.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,112 +5,117 @@ runSwatUI <- function(id) {

# shiny::NS(id) returns a namespace function, which was save as `ns` and will
# invoke later.

ns <- shiny::NS(id)

tagList(

fluidRow(
#-------------------------------------------------------------------------
# 1. Define model outputs for extraction
#-------------------------------------------------------------------------
#-------------------------------------------------------------------------
column(width = 10,
HTML("<b>","1. Define model outputs for extraction","</b>"),
),

column(width = 1,
actionButton("helpOutputExtraction",
actionButton("helpOutputExtraction",
"Help",
buttonType = "default",
style="background-color: none; border-color: none",
class = NULL),
),

column(width = 10,
excelR::excelOutput("tableOutputExtraction",
width = "100%",
excelR::excelOutput("tableOutputExtraction",
width = "100%",
height = "1px"),
span(textOutput("messageUserReadSwatOutput"), style="color:red"),
),

column(width = 5,
checkboxInput('checkOutputExtractionDisplayOnly',
'Display corresponding observed file names',
checkboxInput('checkOutputExtractionDisplayOnly',
'Display corresponding observed file names',
value = FALSE, width = NULL),
),


conditionalPanel(
condition = "input.checkOutputExtractionDisplayOnly == 1",
column(width = 10,
actionButton("getUserReadSwatOutput",
"Load userReadSwatOutput.R",
buttonType = "default",
style="background-color: #87CEFA; border-color: #0d0c0c",
class = NULL),
),
# Display file name
column(width = 10,
verbatimTextOutput("userReadSwatOutputFile", placeholder = TRUE),
),

column(width = 10,
dataTableOutput('tableOutputExtractionDisplayOnly'),
),
),

#-------------------------------------------------------------------------
# 2. Select date range for calibration
#-------------------------------------------------------------------------
#-------------------------------------------------------------------------
column(width = 10,
dateRangeInput("dateRangeCali", "2. Select date range",
start = "2001-01-01",
end = "2010-12-31"),

),

column(width = 1,
actionButton("helpDateRangeCali",
actionButton("helpDateRangeCali",
"Help",
buttonType = "default",
style="background-color: none; border-color: none",
class = NULL),
),

#-------------------------------------------------------------------------
# 3. Select number of parallel runs (cores)
#-------------------------------------------------------------------------
#-------------------------------------------------------------------------
column(width = 10,
sliderInput("ncores",
"3. Select number of parallel runs (threads)",
value = 4,
min = 1,
sliderInput("ncores",
"3. Select number of parallel runs (threads)",
value = 4,
min = 1,
max = parallel::detectCores(),
step = 1,
round = TRUE),
),

column(width = 1,
actionButton("helpNumberofThreads",
actionButton("helpNumberofThreads",
"Help",
buttonType = "default",
style="background-color: none; border-color: none",
class = NULL),
),

#-------------------------------------------------------------------------
# 4. Run SWAT
#-------------------------------------------------------------------------
div( style = "margin-top: 5em",
#-------------------------------------------------------------------------
div( style = "margin-top: 5em",
column(width = 10,
HTML("<b>","4. Run SWAT","</b>"),
),
),

column(width = 1,
tippy::tippy("Help", tooltip = "<span style='font-size:15px;'>
When you click this button, all settings are saved to the file
'RSWATObject.rds' in the working directory folder. The parameter
sets are generated and SWAT are run in parallel. Running can
can take time, R is busy while calling SWAT on the background
therefore, it might not response to any anything. Don't turn of R.
You can check the current simulation status in the file
./Output/CurrentSimulationReport.log You still go to step 4 and click
the 'Open file CurrentSimulationReport.log'. When all simulations
are finished, you will see a table appear
<span>",
allowHTML = TRUE,
trigger = "click",
theme = "translucent"),
),

actionButton("helpRunSWAT",
"Help",
buttonType = "default",
style="background-color: none; border-color: none",
class = NULL),
),


column(width = 10,
actionButton("runSWAT",
"Click here to run SWAT",
Expand All @@ -120,54 +125,50 @@ runSwatUI <- function(id) {

#-------------------------------------------------------------------------
# 5. See simulation report
#-------------------------------------------------------------------------
div( style = "margin-top: 15em",
#-------------------------------------------------------------------------
div( style = "margin-top: 15em",
column(width = 10,
HTML("<b>","5. See simulation report","</b>"),
),
),

column(width = 1,
tippy::tippy("Help", tooltip = "<span style='font-size:15px;'>
You can only open this files when all SWAT simulations are
finished. If SWAT is running on background, you can manually
go to the working folder .\\Output\\CurrentSimulationReport.log
and open it with any text editor (e.g., notepad)
<span>",
allowHTML = TRUE,
trigger = "click",
theme = "translucent"),
),

actionButton("helpCheckCurrentSimulation",
"Help",
buttonType = "default",
style="background-color: none; border-color: none",
class = NULL),
),

column(width = 5,
checkboxInput('checkCurrentSimulation',
'Open file CurrentSimulationReport.log',
checkboxInput('checkCurrentSimulation',
'Open file CurrentSimulationReport.log',
value = FALSE, width = NULL),
),

column(width = 5,
checkboxInput('checkDisplayParameterSet',
'Display all parameter sets',
checkboxInput('checkDisplayParameterSet',
'Display all parameter sets',
value = FALSE, width = NULL),
),


conditionalPanel(
condition = "input.checkCurrentSimulation == 1",
column(width = 10,
uiOutput('tableCurrentSimulation'),

),
),

conditionalPanel(
condition = "input.checkDisplayParameterSet == 1",
column(width = 10,
dataTableOutput("tableDisplayParameterSet"),
),
),
#-------------------------------------------------------------------------

)
)}

Loading

0 comments on commit b0b151a

Please sign in to comment.