Skip to content

Parametrize thresholds #23

Description

@maurolepore

Thresholds require special treatment. It's very tricky to pass a complicated expression like ifelse() because it must be evaluated in the right environment, and that may need a change tiltIndicator.

Maybe for now it's best to do NULL for the default and allows only a single number as alternative input. To give finer grained control to the user we could take the thresholds directly from scenarios -- i.e. the user modifies scenarios to add the values of low_ and high_threshold they want.

---
title: "test"
output: github_document
params:
  low_threshold: !r expression(ifelse(scenarios$year == 2030, 1/9, 1/3))
  # !r 1/3 also works but maybe best to always call `eval()`
  high_threshold: !r expression(1/3)
---
params
#> $low_threshold
#> expression(ifelse(scenarios$year == 2030, 1/9, 1/3))
#> 
#> $high_threshold
#> expression(1/3)

f <- function(scenarios, low_threshold) {
  dplyr::mutate(scenarios, low_threshold = low_threshold)
}

scenarios <- tibble::tibble(year = c(2030, 2031, 2050))

f(scenarios, eval(params$low_threshold))
#> # A tibble: 3 × 2
#>    year low_threshold
#>   <dbl>         <dbl>
#> 1  2030         0.111
#> 2  2031         0.333
#> 3  2050         0.333


g <- function(scenarios, high_threshold) {
  dplyr::mutate(scenarios, high_threshold = high_threshold)
}

g(scenarios, eval(params$high_threshold))
#> # A tibble: 3 × 2
#>    year high_threshold
#>   <dbl>          <dbl>
#> 1  2030          0.333
#> 2  2031          0.333
#> 3  2050          0.333

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions