簡體   English   中英

在反應表中顯示百分位數(閃亮)

[英]display percentile in reactive table (shiny)

我希望顯示具有低 -hight 百分位數的表格來識別極端數據。 我的最終目標是在我的數據庫(閃亮)中顯示不同 tabPanel 中的每個選項卡((tab1 with percentile <20 ;tab2 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)

在我看來,您的代碼中有錯字。 你有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") })

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM