1

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.

zx8754
  • 52,746
  • 12
  • 114
  • 209
babylinguist
  • 392
  • 1
  • 13
  • 1
    I think you have to store `input$in0` as a static nonreactive variable using `isolate`, so that you know what the names of the radio buttons are. ie. you need to access `input$dynamicx`, where you had defined dynamicx in the lapply function. – Jean Dec 27 '16 at 02:38

1 Answers1

1

Something like this?

server.R

function(input, output) {
  # Select specification of features for subsetting
  output$ui <- renderUI({
    lapply(input$in0, function(var) {
      list(radioButtons(paste0("dynamic_", var), label = var, 
                        choices = levels(df[[var]])))
    })
  })
  
  output$mytable = renderDataTable({
    if(is.null(input$in0)) return(df)
    sub <- function(data, var) {
      idx <- data[[var]] == input[[paste0("dynamic_", var)]]
      data[idx, ]
    }
    Reduce(f = sub, init = df, x = input$in0)
  })
}
Community
  • 1
  • 1
user5029763
  • 1,903
  • 1
  • 15
  • 23
  • Beautiful. This is exactly what I was looking for. Is there a non-trivial way to have the data table output only the first column of the data frame? – babylinguist Dec 27 '16 at 14:15
  • 1
    There are a lot of ways to do that. The easier would be adding a `['x']`. But you could use the DT package (http://stackoverflow.com/questions/32409194/hide-certain-columns-in-a-responsive-data-table-using-dt-package) – user5029763 Dec 27 '16 at 15:31