shinyTree Checkboxes using Dates

137 Views Asked by At

I'm trying to create a branching checkbox input using dates similar to the picture below.

enter image description here

The final selections will be unique observations from the prior selected Name. Each Name could have many observations so I'd like to be able to use dates to choose specific ones. An example of my current code is below. I'm able to update the checkbox input based on the name to show all the Name's observations.

ui.r

library(shiny)
library(dplyr)

shinyUI(
    fluidPage(
        navbarPage(inverse = TRUE,
                   tabPanel("Page Title",
                            sidebarPanel(width = 4,
                                         selectizeInput("Name",
                                                        label = "Name",
                                                        choices = sort(unique(mydata$Name))
                                         ),
                                         checkboxGroupInput("Observation",
                                                            label = "Observation",
                                                            choices = sort(unique(mydata$Observation))
                                         )
                            )
                            ,
                            mainPanel(
                                tableOutput("RepDimTable")
                            ))
                   
        )))

server.r

library(shiny)
library(dplyr)

shinyServer(function(input, output, session){
    
    dat <- reactive({
        
        d <- mydata %>%
            filter(Name == input$Name)
        
        updateCheckboxGroupInput(session, "Observation", choices = unique(d$Observation))
        
        d
        
    })
    
    
    output$RepDimTable = renderTable({

        repDimReactive = dat()   %>%
            filter(Observation %in% input$Observation) %>%
            select(Observation, Date, Name, Colour, Score)
        
        repDimReactive
        
    })
})

I'm unsure how to create the branching checkbox from the Date and Observation columns. I've attempted shinyTree solutions but didn't know how to nest the dates and observations into a useable list form.

Data

mydata <- structure(list(Observation = 1:8, Date = c("2020-12-01", "2020-12-01", 
"2020-12-01", "2021-01-01", "2021-01-01", "2021-01-01", "2021-01-15", 
"2021-01-15"), Name = c("Bob", "Fred", "George", "Bob", "Bob", 
"George", "Fred", "George"), Score = c(1L, 4L, 1L, 2L, 2L, 3L, 
2L, 1L), Colour = c("Red", "Blue", "Blue", "Green", "Blue", "Blue", 
"Green", "Red"), Year = c(2020L, 2020L, 2020L, 2021L, 2021L, 
2021L, 2021L, 2021L), Month = c(12L, 12L, 12L, 1L, 1L, 1L, 1L, 
1L), Day = c(1L, 1L, 1L, 1L, 1L, 1L, 15L, 15L)), row.names = c(NA, 
8L), class = "data.frame", na.action = structure(9:22, .Names = c("9", 
"10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", 
"21", "22"), class = "omit"))
1

There are 1 best solutions below

0
On

I found a solution for creating a shinyTree from Dates. Code below. I have not figured out how to filter the reactive df based on the date input but the code answers the original question. The data is the same as above.

mydata = mydata %>%
    mutate(Year = factor(Year),
                      Month = factor(Month),
                      Day = factor(Day))

treelist = list()

library(dplyr)
library(shiny)
library(shinyTree)

ui <- shinyUI(
    fluidPage(
        navbarPage(inverse = TRUE,
                   tabPanel("Page Title",
                            sidebarPanel(width = 4,
                                         selectizeInput("Name",
                                                        label = "Name",
                                                        choices = sort(unique(mydata$Name))
                                         ),
                                         shinyTree("tree")
                            )
                            ,
                            mainPanel(
                                tableOutput("RepDimTable")
                            ))
                   
        )))


server <- shinyServer(function(input, output, session){
    
    dat <- reactive({
        
        d <- mydata %>%
            filter(Name == input$Name)
        
        for (j in unique(d$Year)) {
            tmp <- d[d$Year == j, ]
            subtreelist <- list()
            for (i in unique(tmp$Month)) {
                childs <- as.list(rep("", length(tmp[tmp$Month == i, 1])))
                names(childs) <- tmp[tmp$Month == i, "Day"]
                subtreelist[[i]] <- childs
            }
            treelist[[j]] <- subtreelist
        }
        
        updateTree(session, treeId = ("tree"), data = treelist)
        
        d
        
    })
    
    output$tree <- renderTree({
        treelist
    })
    
    
    output$RepDimTable = renderTable({
        
        repDimReactive = dat()   %>%
            filter(Observation %in% input$Observation) %>%
            select(Observation, Date, Name, Colour, Score)
        
        repDimReactive
        
    })
    
})


shinyApp(ui = ui, server = server)