Skip to content

Commit

Permalink
Linting with lintr v3.1.1
Browse files Browse the repository at this point in the history
- [scalar_in_linter] Use == to match length-1 scalars, not %in%. Note that == preserves NA where %in% does not.
- [if_not_else_linter] In a simple if/else statement, prefer `if (A) x else y` to the less-readable `if (!A) y else x`.
- [keyword_quote_linter] Only quote named arguments to functions if necessary, i.e., if the name is not a valid R symbol (see ?make.names)
- [unreachable_code_linter] Code inside a conditional loop with a deterministically false condition should be removed
  • Loading branch information
dschlaep committed Dec 4, 2023
1 parent 72d4bf2 commit 5e57a8b
Show file tree
Hide file tree
Showing 15 changed files with 74 additions and 99 deletions.
2 changes: 1 addition & 1 deletion R/A_swGenericMethods.R
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ check_version <- function(
expected[[c(1, 3)]] <- 0
}

if (level %in% "major") {
if (identical(level, "major")) {
# zero the minor-level
has[[c(1, 2)]] <- 0
expected[[c(1, 2)]] <- 0
Expand Down
2 changes: 1 addition & 1 deletion R/B_swFiles.R
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ setMethod(
# Maintenance:
# update `do_upgrade` when `n_exp` changes or new upgrades required!
do_upgrade <- c(
from_v230 = n_has == 22L && n_exp %in% 23L
from_v230 = n_has == 22L && n_exp == 23L
)

do_upgrade <- do_upgrade[do_upgrade]
Expand Down
8 changes: 4 additions & 4 deletions R/D_swCloud.R
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,12 @@ swCloud <- function(...) {
# We don't set values for slot `Cloud` (except SnowDensity and RainEvents)
# if not passed via ...; this is to prevent simulation runs with
# accidentally incorrect values
if (!("Cloud" %in% dns)) {
ids <- 4:5
def@Cloud[- ids, ] <- NA_real_
} else {
if ("Cloud" %in% dns) {
# Guarantee names
dimnames(dots[["Cloud"]]) <- dimnames(def@Cloud)
} else {
ids <- 4:5
def@Cloud[- ids, ] <- NA_real_
}

tmp <- lapply(
Expand Down
8 changes: 4 additions & 4 deletions R/D_swWeather.R
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,12 @@ sw_upgrade_MonthlyScalingParams <- function( # nolint: object_length_linter.
vars_exp <- colnames(default@MonthlyScalingParams)
vars_has <- colnames(MonthlyScalingParams)

if (!all(vars_exp %in% vars_has)) {
if (all(vars_exp %in% vars_has)) {
MonthlyScalingParams

Check warning on line 137 in R/D_swWeather.R

View check run for this annotation

Codecov / codecov/patch

R/D_swWeather.R#L137

Added line #L137 was not covered by tests
} else {
res <- default@MonthlyScalingParams
res[, vars_has] <- MonthlyScalingParams[, vars_has]
res
} else {
MonthlyScalingParams
}
}

Expand Down Expand Up @@ -243,7 +243,7 @@ setValidity(
sns <- setdiff(slotNames("swWeather"), inheritedSlotNames("swWeather"))

for (sn in sns) {
n_exp <- if (sn %in% "dailyInputFlags") {
n_exp <- if (identical(sn, "dailyInputFlags")) {
rSW2_glovars[["kSOILWAT2"]][["kINT"]][["MAX_INPUT_COLUMNS"]]
} else {
1L
Expand Down
28 changes: 14 additions & 14 deletions R/D_swWeatherData.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,20 @@ weather_dataColumns <- function() {
#' @export
weather_dataAggFun <- function() {
c(
"Tmax_C" = mean,
"Tmin_C" = mean,
"PPT_cm" = sum,
"cloudCov_pct" = mean,
"windSpeed_mPERs" = mean,
"windSpeed_east_mPERs" = mean,
"windSpeed_north_mPERs" = mean,
"rHavg_pct" = mean,
"rHmax_pct" = mean,
"rHmin_pct" = mean,
"specHavg_pct" = mean,
"Tdewpoint_C" = mean,
"actVP_kPa" = mean,
"shortWR" = mean
Tmax_C = mean,
Tmin_C = mean,
PPT_cm = sum,
cloudCov_pct = mean,
windSpeed_mPERs = mean,
windSpeed_east_mPERs = mean,
windSpeed_north_mPERs = mean,
rHavg_pct = mean,
rHmax_pct = mean,
rHmin_pct = mean,
specHavg_pct = mean,
Tdewpoint_C = mean,
actVP_kPa = mean,
shortWR = mean
)
}

Expand Down
6 changes: 3 additions & 3 deletions R/F_swSite.R
Original file line number Diff line number Diff line change
Expand Up @@ -211,14 +211,14 @@ swSite <- function(...) {
tmp <- c("Longitude", "Latitude", "Altitude", "Slope", "Aspect")
def@IntrinsicSiteParams[tmp] <- NA_real_
}
if (!("TranspirationRegions" %in% dns)) {
def@TranspirationRegions[, "layer"] <- NA_integer_
} else {
if ("TranspirationRegions" %in% dns) {
# Guarantee names
dimnames(dots[["TranspirationRegions"]]) <- list(
NULL,
colnames(def@TranspirationRegions)
)
} else {
def@TranspirationRegions[, "layer"] <- NA_integer_
}

if ("swrc_flags" %in% dns) {
Expand Down
18 changes: 9 additions & 9 deletions R/F_swSoils.R
Original file line number Diff line number Diff line change
Expand Up @@ -185,24 +185,24 @@ swSoils <- function(...) {

# We don't set values for slot `Layers` if not passed via ...; this
# is to prevent simulation runs with accidentally incorrect values
if (!("Layers" %in% dns)) {
def@Layers <- def@Layers[1, , drop = FALSE]
def@Layers[] <- NA_real_
ntmp <- 1
} else {
if ("Layers" %in% dns) {
# Guarantee names
dimnames(dots[["Layers"]]) <- list(NULL, colnames(def@Layers))
ntmp <- nrow(dots[["Layers"]])
} else {
def@Layers <- def@Layers[1, , drop = FALSE]
def@Layers[] <- NA_real_
ntmp <- 1
}

# We don't set values for slot `SWRCp` if not passed via ...; this
# is to prevent simulation runs with accidentally incorrect values
if (!("SWRCp" %in% dns)) {
def@SWRCp <- def@SWRCp[rep.int(1, ntmp), , drop = FALSE]
def@SWRCp[] <- NA_real_
} else {
if ("SWRCp" %in% dns) {
# Guarantee names
dimnames(dots[["SWRCp"]]) <- list(NULL, colnames(def@SWRCp))
} else {
def@SWRCp <- def@SWRCp[rep.int(1, ntmp), , drop = FALSE]
def@SWRCp[] <- NA_real_
}

# Copy from SOILWAT2 "testing" (defaults), but dot arguments take precedence
Expand Down
2 changes: 1 addition & 1 deletion R/G_swOut.R
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ setMethod(
# update `do_upgrade` when `n_exp` changes or new upgrades required!
do_upgrade <- c(
from_v230 = n_has == 30L && n_exp %in% 31L:32L,
from_v310 = n_has == 31L && n_exp %in% 32L
from_v310 = n_has == 31L && n_exp == 32L
)

do_upgrade <- do_upgrade[do_upgrade]
Expand Down
17 changes: 9 additions & 8 deletions R/Rsw.R
Original file line number Diff line number Diff line change
Expand Up @@ -502,15 +502,8 @@ set_requested_flags <- function(swIn, tag, use, values, fun, reset = TRUE,
# Check dimensional agreement
ndim_gt1_vals <- sum(dim(data.frame(vals)) > 1)
ndim_gt1_def <- sum(dim(data.frame(def)) > 1)
if (!(ndim_gt1_vals == 1 && ndim_gt1_def == 1)) {
stop(
"ERROR: ",
toString(shQuote(val_names)),
" are not represented as 1-dimensional objects in",
" class 'swInputData'."
)

} else {
if (ndim_gt1_vals == 1 && ndim_gt1_def == 1) {
# Transfer values
itemp <- sapply(names(def), function(x) {
k <- grep(substr(x, 1, 4), val_names)
Expand All @@ -522,6 +515,14 @@ set_requested_flags <- function(swIn, tag, use, values, fun, reset = TRUE,
}

swIn <- get(paste0(fun, "<-"))(swIn, def)

} else {
stop(
"ERROR: ",
toString(shQuote(val_names)),
" are not represented as 1-dimensional objects in",
" class 'swInputData'."
)
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions R/swWeatherGenerator.R
Original file line number Diff line number Diff line change
Expand Up @@ -1705,10 +1705,7 @@ dbW_fixWeather <- function(
dif_wd3 <- rSOILWAT2::calc_dailyInputFlags(weatherData3)
vars_wd3 <- names(dif_wd3)[dif_wd3]

if (!any(is_missing_weather(weatherData3[, vars_wd3]))) {
weatherData4 <- weatherData3

} else {
if (any(is_missing_weather(weatherData3[, vars_wd3]))) {
daymeans <- data.frame(
Year = NA,
aggregate(
Expand Down Expand Up @@ -1773,6 +1770,9 @@ dbW_fixWeather <- function(

is_miss4 <- is_missing_weather(weatherData4[, weather_dataColumns()])
meta[!is_miss4 & is_miss3] <- "longTermDailyMean"

} else {
weatherData4 <- weatherData3
}


Expand Down
6 changes: 3 additions & 3 deletions R/sw_Pedotransfer_Functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -769,7 +769,7 @@ rSW2_SWRC_PTF_estimate_parameters <- function( # nolint: object_length_linter.
fail = TRUE,
...
) {
ptf_name <- std_ptf(ptf_name)[1]
ptf_name <- std_ptf(ptf_name)[[1L]]
has_ptf <- ptf_name %in% ptfs_implemented_by_rSW2()

list_soilargs <- list(
Expand All @@ -778,7 +778,7 @@ rSW2_SWRC_PTF_estimate_parameters <- function( # nolint: object_length_linter.
bdensity = bdensity
)

if (has_ptf && ptf_name %in% "Rosetta3") {
if (has_ptf && ptf_name == "Rosetta3") {
dots <- list(...)
dots[["version"]] <- if ("version" %in% names(dots)) {
as.character(dots[["version"]])
Expand All @@ -791,7 +791,7 @@ rSW2_SWRC_PTF_estimate_parameters <- function( # nolint: object_length_linter.
args = c(list_soilargs, dots)
)

} else if (has_ptf && ptf_name %in% "neuroFX2021") {
} else if (has_ptf && ptf_name == "neuroFX2021") {
do.call(
ptf_neuroFX2021_for_FXW,
args = c(list_soilargs, list(...))
Expand Down
30 changes: 8 additions & 22 deletions tests/testthat/test_exec_and_aggregate.R
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,14 @@ for (it in tests) {

#------ Run SOILWAT2
test_that("Simulate and aggregate", {
rd <- sw_exec(
inputData = sw_input,
weatherList = sw_weather,
echo = FALSE,
quiet = TRUE
# Run silently
expect_silent(
rd <- sw_exec(
inputData = sw_input,
weatherList = sw_weather,
echo = FALSE,
quiet = TRUE
)
)

# Check rSOILWAT2 output object
Expand All @@ -177,23 +180,6 @@ for (it in tests) {
expect_false(has_soilTemp_failed())
expect_true(all(sw_out_flags() %in% slotNames(rd)))

# Run silently/verbosely
expect_silent(sw_exec(
inputData = sw_input,
weatherList = sw_weather,
echo = FALSE, quiet = TRUE
))

# This doesn't work; apparently, testthat::expect_message and similar
# functions don't capture text written by LogError directly to the console.
if (FALSE) {
expect_message(sw_exec(
inputData = sw_input,
weatherList = sw_weather,
echo = FALSE, quiet = FALSE
))
}


# Check that input weather is identical to output weather
# (don't check missing days that the weather generator filled in)
Expand Down
9 changes: 0 additions & 9 deletions tests/testthat/test_iOUT_macros.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,6 @@ test_that("Tests of iOUT and iOUT2", {
for (i in seq_len(n_vars)) {
# test `iOUT` for 'key = 1'
icol <- ncol_TimeOUT[pd] + i
if (FALSE) {
print(paste("key =", key, "var =", i, "irow =", irow_OUT[pd],
"icol =", icol, "iOUT =", iOUT(i, pd, irow_OUT)))
}
p_OUT[key, pd][[1]][irow_OUT[pd], icol] <- iOUT(i, pd, irow_OUT)
}

Expand All @@ -70,11 +66,6 @@ test_that("Tests of iOUT and iOUT2", {
for (k in seq_len(NVEGTYPES)) {
for (i in seq_len(n_layers)) {
icol <- ncol_TimeOUT[pd] + i + n_layers * (k - 1)
if (FALSE) {
print(paste("key =", key, "veg =", k, "slyr =", i, "irow =",
irow_OUT[pd], "icol =", icol, "iOUT =",
iOUT2(i, k, pd, irow_OUT)))
}
p_OUT[key, pd][[1]][irow_OUT[pd], icol] <-
iOUT2(i, k, pd, irow_OUT)
}
Expand Down
8 changes: 4 additions & 4 deletions tests/testthat/test_pedotransferfunctions.R
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ vwc_fix <- data.frame(
row.names(vwc_fix) <- row.names(texture)

ftemp <- file.path("..", "test_data", "swp_values.rds")
if (FALSE) {
if (file.exists(ftemp)) {
swp_vals <- readRDS(ftemp)

} else {
swp_vals <- unlist(lapply(
row.names(texture),
function(itext) {
Expand All @@ -70,9 +73,6 @@ if (FALSE) {
row.names(texture)
)
saveRDS(swp_vals, file = ftemp)

} else {
swp_vals <- readRDS(ftemp)
}

#--- Tests
Expand Down
21 changes: 9 additions & 12 deletions vignettes/rSOILWAT2_demo.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -189,16 +189,14 @@ options(tinytex.verbose = TRUE)

## Create base rSOILWAT2 input object
```{r, input_object}
# Preferred option:
## Quick option
# Note: This approach is discouraged because it is very easy to miss to
# set all relevant parameters and variables to site-specific values
sw_in__bad <- rSOILWAT2::sw_exampleData ## Don't do this!
## Preferred option
# All relevant site-specific parameters and variables are set to NA
sw_in <- rSOILWAT2::swInputData()
# Quick option:
# This approach is discouraged because it is very easy to miss to
# set all relevant parameters and variables to site-specific values
if (FALSE) {
sw_in <- rSOILWAT2::sw_exampleData
}
```

## Simulation time and location
Expand Down Expand Up @@ -644,10 +642,9 @@ by `SOILWAT2` with the selected pedotransfer function.
# Declare that parameter values are already estimated
rSOILWAT2::swSite_hasSWRCp(sw_in) <- TRUE
if (FALSE) {
# Alternatively, set soil properties and SWRC parameters at once
rSOILWAT2::set_swSoils(sw_in) <- list(Layers = soil_new, SWRCp = swrcp)
}
# Alternatively (and equivalently),
# set soil properties and SWRC parameters with one assignment
rSOILWAT2::set_swSoils(sw_in) <- list(Layers = soil_new, SWRCp = swrcp)
```


Expand Down

0 comments on commit 5e57a8b

Please sign in to comment.