0

I am looking to display tables with low -hight percentile to identify extreme datas. My final aim is to display in my DB (shiny) each tab in different tabPanel ( (tab1 with percentile <20 ;tab2 with percentile>80) . I tried a lot of thing but not working or bad.

To explain, I give a minimal code to get :

  1. a boxplot to display data by filter and period
  2. a main tab that display all data that appears in the box plot
  3. a tab that display 20 percentile (not work)

Do you have any suggestions to fix the problem and possibly simplify the programming?

Thanks a lot for your contribution

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

AST_
  • 21
  • 1
  • 1
  • 5

1 Answers1

0

Looks to me that you had a typo in your code. You had unique(data.duree$um).

Here is the chunk:

#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") })
Eric Krantz
  • 1,854
  • 15
  • 25
  • Very good review ! But how do i do to get the same output format than the last tab (global data filtered) ? I tried by deleting the "group_by" but not work – AST_ Nov 21 '20 at 10:08