I have the following dataset:
df <- data.frame(x = gl(n = 2, k = 10, labels = c(0, 1)),
y = gl(n = 4, k = 5, labels = c('hi', 'bye', 'wee', 'whoa')))
I have created a shiny app that allows the user to select any of the variables of the data frame. Upon selecting a variable, radio buttons appear. I would like the user to be able to select values using the radio buttons that will then be used to subset the data frame, and, ultimately, print the result in a data table. The code I have so far is as follows:
server.R
library(tidyr)
library(dplyr)
df <- data.frame(x = gl(n = 2, k = 10, labels = c(0, 1)),
y = gl(n = 4, k = 5, labels = c('hi', 'bye', 'wee', 'whoa')))
function(input, output) {
# Select specification of features for subsetting
output$ui <- renderUI({
# Get length of features selected
numVar <- length(as.integer(input$in0))
# Create n radioButtons based on number of features selected
lapply(input$in0, function(x) {
list(radioButtons(paste0("dynamic", x), x,
choices = c("zero" = "0",
"one" = "1"),
selected = "0"))
})
})
output$mytable = renderDataTable({
df
})
}
ui.R
library(tidyr)
library(dplyr)
df <- data.frame(x = gl(n = 2, k = 10, labels = c(0, 1)),
y = gl(n = 4, k = 5, labels = c('hi', 'bye', 'wee', 'whoa')))
fluidPage(
br(),
fluidRow(
column(3,
h2('Test subset'),
# Drop down of all features
selectInput(inputId = 'in0', label = 'Choose features',
choices = colnames(df),
multiple = TRUE, selectize = TRUE),
# Radio buttons for features
wellPanel(uiOutput("ui"))
),
column(9,
dataTableOutput('mytable')
)
)
)
Specifically, I do not know how to access the user selections from the radio buttons so as to subset the data table df
in the server.R
file. In this case, it is only necessary that the subset work with the x
variable. That is, one that consists of 0's and 1's, but it should potentially be able to work with many variables. Thus, the subset code has to take into account how many variables the user has selected.