【问题标题】:shinyTree Checkboxes using Dates使用日期的 shinyTree 复选框
【发布时间】:2021-01-31 04:02:15
【问题描述】:

我正在尝试使用类似于下图的日期创建一个分支复选框输入。

最终选择将是来自先前选择的名称的独特观察。每个名称可能有很多观察结果,所以我希望能够使用日期来选择特定的。下面是我当前代码的一个示例。我可以根据姓名更新复选框输入以显示姓名的所有观察结果。

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
        
    })
})

我不确定如何从日期和观察列创建分支复选框。我尝试了 shinyTree 解决方案,但不知道如何将日期和观察结果嵌套到可用的列表形式中。

数据

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"))

【问题讨论】:

    标签: r checkbox shiny reactive shinytree


    【解决方案1】:

    我找到了从 Dates 创建闪亮树的解决方案。代码如下。我还没有弄清楚如何根据日期输入过滤反应性 df,但代码回答了原始问题。数据同上。

    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)
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-22
      • 1970-01-01
      • 1970-01-01
      • 2023-03-14
      • 1970-01-01
      相关资源
      最近更新 更多