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