Shiny / dplyr and non-standard evaluation

I have a Shiny app that programmatically creates widgets from an sqlite3 databases and uses those widgets to filter data from that database. I can get the current values of these widgets like so:

problem_param_values <- function(experiments, prefix, input) {
    params <- problem_params(experiments)
    values <- lapply(params,
        function(p)
            eval(parse(text = sprintf("input$%s",
                paste0(prefix, "problem_param", p)))))
    names(values) <- params
    return(values)
}

I am trying to create a filter from this, but I am having a hard time finding the right way to quote/unquote things. This is what I have so far:

problem_param_select <- function(param, val) {
        # Use fuzzy matching when comparing numbers because precision is lost
        # when real-valued parameter values are converted to strings for
        # parameter selection widget.
        expr(!!sprintf("abs(%s - %s) < 0.0000001", param, val))
}

...

param_values <- problem_param_values(experiments(), "perf", input)
filter_expr <- mapply(
                problem_param_select, names(param_values), param_values)
data <- performance() %>% filter(!!!filter_expr)

This doesn't work. If I add this before the last statement I can see that the wrong SQL query is generated:

cat(performance() %>% filter(!!!filter_expr) %>% show_query())

Any suggestions on how to fix / debug this?

1 Like

Hi, this is how I would approach the problem_param_select() function:

problem_param_select <- function(param, val) {
  param <- enquo(param)
  val <- enquo(val)
  expr(abs(!! quo_expr(param) - !! quo_expr(val)) < 0.0000001)
}

After reading it a few times, I'm still not able to tell what the other functions do, or supposed to do. Hopefully this gets you closer.

1 Like

You can see what I am trying to do at plannerarena. org. If you download this file: Dropbox - xxl.db - Simplify your life and upload it to plannerarena. org (via the "change database" link), you'll see the attached widgets. I am trying to select the data from the experiments table that matches the selected parameters. This used to work with older versions of the tidyverse packages (before it was called the tidyverse; I have dplyr 0.5.0 and shiny 0.14 running on that server). I am trying to update the code to work with the latest versions.

The full source can be found at bitbucket.org/ompl/ompl/src/default/scripts/plannerarena/. A minimal working example would take a bit more time.