r - Creating a generic function generating new objects from selected list of columns in a data frame -
i'm working on simple shinyapp enable end-users interrogate data use of various filters. of filters correspond unique values in data frame columns created using code corresponding example below:
# source sample data data(mtcars) # put menu element mtcars$cyl <- as.character(mtcars$cyl) ui_list_cyl <- unique(mtcars$cyl)
i' subsequently introducing elements created in manner shiny drop down widget, in code below:
# sample widget elements provides access unique column values selectinput("select", label = h3("select box"), choices = ui_list_cyl, selected = 1),
i'm repeating same procedure number of columns , creating function address requirement appears natural choice. in particular, envisage function to:
- be applicable set of columns pass onto it
- create objects corresponding interface elements names
ui_list_originalcolumname
my question be: how create function can dynamically applied arbitrary selection of columns in data frame , return objects names incorporating passed column names?
there's several ways write function want. here's example of such function accepts dataset , column name , creates associated select input.
myselectinput <- function(data, col) { id <- sprintf("select_%s", col) label <- sprintf("select %s", col) choices <- sort(unique(data[[col]])) selectinput(id, label, choices) }
you can use in shiny app so
runapp(shinyapp( ui = fluidpage( lapply(c("cyl", "gear", "carb"), function(x) myselectinput(mtcars, x)) ), server = function(input, output, session) { } ))
this create 3 select inputs cyl
, gear
, carb
variables. hope helps, can take here , apply need
Comments
Post a Comment