Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error in if (revision < 57) { : missing value where TRUE/FALSE needed #42

Closed
gopalpenny opened this issue Sep 13, 2021 · 3 comments
Closed

Comments

@gopalpenny
Copy link

TLDR:
The string pattern in the check_revision function may require a question mark (i.e., question mark inserted after \\. as: "[:digit:]{1,}\\.?[:digit:]") so that it still matches if there is no period in the SWAT version number. I attempted to debug the problem while drafting the full post, which is why it's much longer.

Full post:

I've been having trouble getting SWATplusR to run, in particular the function run_swatplus. I had to install using remotes::install_github("chrisschuerz/SWATplusR") because I got an error when attemping to install from CRAN (the error indicated that the package would not work with my version of R, which is 4.0.3). With that said, the github version seemed to install without problem. The following works:

library(SWATplusR)

# Loading a SWAT+ project
path_plus <- load_demo(dataset = "project",
                       version = "plus",
                       path = demo_path,
                       revision = 57)

q_obs <- load_demo(dataset = "observation")

But when I run the code:

q_sim_plus <- run_swatplus(project_path = path_plus,
                           output = define_output(file = "channel",
                                                  variable = "flo_out",
                                                  unit = 1))

I get the following error message (and warning):

Error in if (revision < 57) { : missing value where TRUE/FALSE needed
In addition: Warning message:
Missing column names filled in: 'X6' [6] 

The error message occurs within the translate_outfile_names() function. But the problem seems to be with the check_revision() function, which returns NA. If it's helpful, the values of the function input variables for check_revision() are:

Browse[4]> project_path
[1] "C:/Users/gopenny/Documents/SWAT models/SWATplusR_demos/swatplus_rev57_demo"
Browse[4]> run_path
[1] "C:/Users/gopenny/Documents/SWAT models/SWATplusR_demos/swatplus_rev57_demo/.model_run"
Browse[4]> os
[1] "win"
Browse[4]> swat_exe
[1] "swatplus_rev57.exe"

Finally, a bit more digging suggests that the version number only matches if there is a period ".". Instance, the result of check_revision up until str_extract(., "[:digit:]{1,}\\.[:digit:]") is " 57 ". Running the str_extract code as is produces:

foo <- "                57           "
str_extract(foo, "[:digit:]{1,}\\.[:digit:]")
[1] NA

For me this is fixed by adding a question mark as:

foo <- "                57           "
str_extract(foo, "[:digit:]{1,}\\.?[:digit:]")
[1] "57"
@mgf-unige
Copy link

mgf-unige commented Oct 31, 2021

I get the same error while following the demo (https://chrisschuerz.github.io/SWATplusR/articles/SWATplusR.html#loading-demos), and running:

q_sim_plus <- run_swatplus(project_path = path_plus,
output = define_output(file = "channel",
variable = "flo_out",
unit = 1))

@chrisschuerz
Copy link
Owner

@gopalpenny Thanks for pointing that out! You are right. and I will add the fix in the code. check_revision() is imho just a messy workaround that was necessary with older SWAT+ revisions. In future versions of SWATplusR I guess that I will get rid of it unless there will be significant changes in the SWAT file structure.

@gopalpenny
Copy link
Author

Makes sense, thanks for adding the fix!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants