As an example I will be using the built-in diamonds data in ggplot2.
I want to display the dataframe according to cut, color and clarity. However, I wanted to select the items through deduction. I would want to select the colors in the drop-down menu that are available and so on with clarity.
The method below does not refresh when you wanted to look at another item. Is there an easier way of doing this in shiny using plyr?
The diamonds dataframe may not be the best example but I cant find any other data.
server.R
library(plyr)
dm<-dlply(diamonds, .(cut))
for(x in 1:length(dm)){
assign(eval(parse(text = paste("names(dm)[x]"))),dm[[x]])
}
shinyServer(function(input, output) {
output$choose_cut <- renderUI({
selectInput("cut", "Cut", as.list(names(dm), multiple = TRUE))
})
output$choose_color <- renderUI({
if(is.null(input$cut)) return()
dat <- get(input$cut)
dm2<-dlply(dat, .(color))
for(x in 1:length(dm2)){
assign(eval(parse(text = paste("names(dm2)[x]"))),dm2[[x]], envir = globalenv()
)}
selectInput("color", "Color", as.list(names(dm2)))})
output$choose_clarity <- renderUI({
if(is.null(input$color)) return()
dat <- get(input$color)
dm3<-dlply(dat, .(clarity))
for(x in 1:length(dm3)){
assign(eval(parse(text = paste("names(dm3)[x]"))),dm3[[x]], envir = globalenv())
}
selectInput("clarity", "Clarity", as.list(names(dm3)))
})
output$table <- renderTable({
if (is.null(input$clarity)) return()
dat <- get(input$clarity)
dat
})
})
ui.R
shinyUI(pageWithSidebar(
headerPanel(""),
sidebarPanel(
uiOutput("choose_cut"),
uiOutput("choose_color"),
uiOutput("choose_clarity"),
br()
),
mainPanel(
"Data", tableOutput("table"))))