【问题标题】:display percentile in reactive table (shiny)在反应表中显示百分位数(闪亮)
【发布时间】:2021-03-04 09:03:41
【问题描述】:

我希望显示具有低-高百分位数的表格以识别极端数据。 我的最终目标是在我的数据库(闪亮)中显示不同 tabPanel 中的每个选项卡( (tab1 with percentile 80) 。 我尝试了很多东西,但没有工作或不好。

为了解释,我给出了一个最少的代码:

  1. 按过滤器和周期显示数据的箱线图
  2. 显示箱形图中出现的所有数据的主选项卡
  3. 显示 20% 的选项卡(无效)

您有什么建议可以解决这个问题并可能简化编程吗?

非常感谢您的贡献

#data
data<- data.frame(stringsAsFactors=FALSE,
                 id=c("1", "2", "3", "4", "5", "6", "7","8","9"),
                  um = c("A1234","A1234","C1345","C1345","Z4453","C1345","C1345","Z4453","A1234"),
                  time= c(1, 12 , 11, 34, 47, 3, 5, 8, 12),
                  week =c("2020-002","2020-011","2020-011","2020-005","2020-004","2020-005","2020-011","2020-005","2020-006"),
                  month =c("2020-01","2020-03","2020-03","2020-02","2020-01","2020-02","2020-03","2020-02","2020-02"),
                  year1 = c("2020","2020","2020","2020","2020","2020","2020","2020","2020"))

library(shiny)
library(dplyr)
library(ggplot2)
library(plotly)
library(DT)

ui<- shinyUI(  fluidRow(
  column(2,
         selectInput(inputId = "dur_um", 
                     label = "UM",
                     choices = as.character(unique(data$um)),
                     multiple=TRUE,
                     width = validateCssUnit(200))),
  
  column(2,  selectInput("period",
                         label="Display by:",
                         choices = c("Week_"= "week",
                                     "Month_"   = "month",
                                     "Year_"  = "year1"),
                         selected   = "month",
                         width = validateCssUnit(100))),
  
  plotOutput("graph1", width = "400px", height = "200px"), br(),
  DT::dataTableOutput("tab"),  br(),    
  DT::dataTableOutput("tab_h")) )





server <- function(input, session, output) {
  
  periode<- reactive({
    if (input$period == "week") { "week" 
    }   else if (input$period == "month") { "month"
    }   else { "year1" } 
  })
  datatime <- reactive({
    dur_um   <- if (is.null(input$dur_um)) unique(data$um) else input$dur_um
    
    data %>%   
      filter( um %in% dur_um) %>%
      group_by_(periode())
  })
  
  
  # global graph
  output$graph1 <- renderPlot({    
    ggplot(datatime(), aes(.data[[periode()]], y =time  )) + 
      geom_boxplot(aes(fill = stage(.data[[periode()]], after_scale = alpha(fill, 0.4))))
  })
  
  # Global tab (without filter on percentile)
  output$tab <- DT::renderDataTable({
    datatable(datatime(),  
              options = list( 
                pageLength = 50, 
                filter = 'top', 
                class = "hover"))
  }) 
  
 
  
   
  #data to filt on 20 percentile
  data_h <- reactive({
    dur_um  <- if (is.null(input$dur_um)) unique(data$um) else input$dur_um
    data %>%  
      filter( um %in% dur_um) %>%
      group_by_(periode()) %>%  
      summarise(p20 = quantile(time, probs = c(0.20), na.rm = FALSE, type=2),
                .groups = "drop") })   
  
  #tab with filt on perc
  output$tab_h <- DT::renderDataTable({
    datatable(data_h(), 
              options = list(
                pageLength = 50, 
                filter = 'top', 
                class = "hover")) }) 
} 


shinyApp(ui,server)

【问题讨论】:

    标签: r reactive dt


    【解决方案1】:

    在我看来,您的代码中有错字。你有unique(data.duree$um)

    这是块:

    #data to filt on 20 percentile
    data_h <- reactive({
        dur_um  <- if (is.null(input$dur_um)) unique(data$um) else input$dur_um
        data %>%  
            filter( um %in% dur_um) %>%
            group_by_(periode()) %>%  
            summarise(p20 = quantile(time, probs = c(0.20), na.rm = FALSE, type=2),
                      .groups = "drop") })
    

    【讨论】:

    • 非常好的评论!但是我该如何获得与最后一个选项卡(全局数据过滤)相同的输出格式?我尝试删除“group_by”但不起作用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-14
    • 1970-01-01
    • 2020-04-30
    • 1970-01-01
    • 2014-05-13
    • 2014-06-07
    • 1970-01-01
    相关资源
    最近更新 更多