Passing Metrics into Yardstick

I am trying to pass multiple metrics m into metric_set () but the function requires a list of functions. Suggestions how I can do this? I need this for a shiny app:

library(yardstick)

m <- c("ROC" = yardstick::roc_auc, "Accuracy" = yardstick::accuracy, "Balanced_Accuracy" = yardstick::bal_accuracy,   "Sensitivity" = yardstick::sensitivity)

m1 <- metric_set(m)

You can use rlang to splice them in:

library(yardstick)
library(rlang)

m <-
  c(
    "ROC" = yardstick::roc_auc,
    "Accuracy" = yardstick::accuracy,
    "Balanced_Accuracy" = yardstick::bal_accuracy,
    "Sensitivity" = yardstick::sensitivity
  )

m1 <- metric_set(!!!m)
m1
#> # A tibble: 4 × 3
#>   metric     class        direction
#>   <chr>      <chr>        <chr>    
#> 1 <prb_mtrc> prob_metric  maximize 
#> 2 <clss_mtr> class_metric maximize 
#> 3 <clss_mtr> class_metric maximize 
#> 4 <clss_mtr> class_metric maximize

Created on 2023-08-20 with reprex v2.0.2

Thanks Max,

My shiny app has an input for metrics called input$m8_metric and the available selections are m. Passing input$m8_metric using !!!m into metric_set() still results in an error.

A print of the selected input$m8_metric to the console looks correct:

print(paste0("Input = ",input$m8_metric))
"Input = function (data, ...) \n{\n    UseMethod(\"accuracy\")\n}"

however when passing m into the function I get this error:

m <- input$m8_metric
m1 <- metric_set(!!!m)

Warning: Error in : All inputs to `metric_set()` must be functions. These inputs are not: (1).

Any other suggestions?

I'm guessing that the input has the names as character strings. In the code that I sent, the function names are being invoked.

Here some code that you can adapt for your project:

library(yardstick)
library(purrr)
library(rlang)
#> 
#> Attaching package: 'rlang'
#> The following objects are masked from 'package:purrr':
#> 
#>     %@%, flatten, flatten_chr, flatten_dbl, flatten_int, flatten_lgl,
#>     flatten_raw, invoke, splice

# Get the names of the functions
chr_functions <- c("roc_auc", "accuracy", "bal_accuracy", "sensitivity")
# Get the actual functions from yarstick
metric_functions <- map(chr_functions, ~ getFromNamespace(.x, ns = "yardstick"))
# slice them in 
m1 <- metric_set(!!!metric_functions)
m1
#> # A tibble: 4 × 3
#>   metric     class        direction
#>   <chr>      <chr>        <chr>    
#> 1 <prb_mtrc> prob_metric  maximize 
#> 2 <clss_mtr> class_metric maximize 
#> 3 <clss_mtr> class_metric maximize 
#> 4 <clss_mtr> class_metric maximize

Created on 2023-08-21 with reprex v2.0.2

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.