Reputation: 3126
Not knowing where else to ask the question, I ask here (risking downvotes). I have a dataframe, df, with a numeric string code, num, and its associated text label in another string variable, str. The total unique different values here are more than 100.
Now, I´d like to filter approx 50 of these variables, but entering the 50 different values is error-prone and tedious.
Seeing the Rstudio regexplain addin, I was wondering if there is an addin somewhere that accepts a dataframe, allows me to tick a box for the intended variables and finally returns the generated string that I can subsequently use for categorization/filtering.
------Trying to explain better---
data(mtcars)
mtcars$make <- rownames(mtcars)
rownames(mtcars) <- NULL
Now I´d like to send mtcars to an addin (shiny based, I presume) that displays mtcars and allows me to tick a box for selecting the make column and subsequently highlight the desired categories of the make column. Ultimately I want the addin to generate a concatenated string that can be used for filtering or categorization:
c("Fiat 128","Camaro Z28", "Volvo 142E")
select_app <- shinyApp(
ui = fluidPage(
sidebarLayout(
sidebarPanel(
checkboxGroupInput(inputId = "selected_values",
label = "select variable",
choices = names(mtcars))
),
mainPanel(
checkboxGroupInput(inputId = "cat",
label = "select categories",
choices = "cat"),
actionButton("finish", "stop selection")
)
)
),
server = function(input, output, session) {
observeEvent(input$selected_values, {
x <- reactive(unique(mtcars[input$selected_values]))
updateCheckboxGroupInput(session,inputId = "cat",
choices=x())
})
observeEvent(input$finish, {
stopApp(input$cat)
})
}
)
Upvotes: 1
Views: 147
Reputation: 10365
Is this what you had in mind? The steps are the following:
df
that contains a make
columnmy_selection
contains the selected values# define the dataframe
mtcars$make <- rownames(mtcars)
rownames(mtcars) <- NULL
df <- mtcars
# define the selection app
library(shiny)
library(DT)
library(shinyWidgets)
select_app <- shinyApp(
ui = fluidPage(
sidebarLayout(
sidebarPanel(
pickerInput(inputId = "selected_values",
label = "select the values",
choices = unique(df$make),
multiple = TRUE),
actionButton("finish", "stop selection")
),
mainPanel(
DTOutput("table")
)
)
),
server = function(input, output, session) {
output$table <- renderDT(
datatable(df)
)
observeEvent(input$finish, {
stopApp(input$selected_values)
})
}
)
# do the selection
my_selection <- runApp(select_app)
You can now pass the dataframe as argument to the function. I've changed the left checkboxGroupInput
to radioButtonGroupInput
because on the right you can always only select the categories from one variable, so it doesn't make sense that you can select multiple variables. Note that the returned categories are a character vector, even if the input is numeric.
# define the dataframe
mtcars$make <- rownames(mtcars)
rownames(mtcars) <- NULL
# define the selection app
library(shiny)
start_select_app <- function(df) {
select_app <- shinyApp(
ui = fluidPage(
sidebarLayout(
sidebarPanel(
radioGroupButtons(inputId = "selected_values",
label = "select variable",
choices = names(df),
direction = "vertical")
),
mainPanel(
checkboxGroupInput(inputId = "cat",
label = "select categories",
choices = NULL),
actionButton("finish", "stop selection")
)
)
),
server = function(input, output, session) {
observeEvent(input$selected_values, {
new_choices <- unique(df[, input$selected_values])
updateCheckboxGroupInput(session,
inputId = "cat",
choices = new_choices)
})
observeEvent(input$finish, {
stopApp(input$cat)
})
}
)
runApp(select_app)
}
# do the selection
my_selection <- start_select_app(mtcars)
Upvotes: 1