Calling Functions from selectInput

I have a large number of functions that I would like to apply by calling them from selectInput, and with the arguments for the function coming from other selectInputs. Unfortunately, when I try and pass the arguments they are treated as characters as opposed to variables. This produces errors, such as " no applicable method for 'arrange_' applied to an object of class "character"" Am I missing something simple, here?
Ultimately, I want the results output to a DT::datatable.

# libraries ---------------------------------------------------------------
library(tidyverse)
library(shiny)
library(DT)
library(janitor)

# data --------------------------------------------------------------------
data <- tribble(
  ~ds, ~fn,
  "mtcars", "fn1",
  "mtcars", "fn2",
  "iris", "fn2",
  "iris", "fn3"
)

# functions ---------------------------------------------------------------
fn1 <- function(y){
  output <- arrange(y, mpg)
  return("output" = output)
}

fn2 <- function(y){
  output <- y %>%
    get_dupes()
}

fn3 <- function(y){
  output <- y %>%
    filter(Species == "setosa")
}

# ui ----------------------------------------------------------------------
ui <- fluidPage(
  selectInput("dataset", "Select a Dataset", choices = unique(data$ds)),
  selectInput("fun", "Select a Function", choices = NULL),
  DT::DTOutput("table"),
  actionButton("save", "Save Changes")  
)

# server ------------------------------------------------------------------
server <- function(input, output, session){
  vals <- reactiveValues(x = NULL)
  
  dm <- reactive({
    filter(data, ds == input$dataset)
  })
  
  observeEvent(dm(), {
    choices <- unique(dm()$fn)
    updateSelectInput(session, "fun", choices = choices)
  })
  
  data1 <- reactive({
    if(is.null(input$fun)) {
      return()
    }
    switch(input$fun,
           fn1 = fn1(input$dataset),
           fn2 = fn2(input$dataset),
           fn3 = fn3(input$dataset)
    )
  })
  
  output$table <- DT::renderDT({
    req(data1()$output)
    vals$x <- data.frame(data1()$output, check.names = FALSE)
    datatable(vals$x,
              escape = FALSE,
              selection = "none",
              editable = TRUE,
              options = list(
                dom = "Bfrtip",
                paging = TRUE
              )
    )
  }, server = FALSE)  
}

shinyApp(ui, server)
```r

Your choices for "fun" is NULL should this be unique(data$fn)?

And in your switch statement try to change fn1 = fn1(input$dataset) to "fn1" = fn1(input$dataset).

The choices for "fun" populate once the "dataset" has been selected.

Unfortunately, adding the quotes to fn1, does not resolve the issue either.

Thank you for the input, though.

This topic was automatically closed 54 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.