0

EDIT : Stefan's advice integrated

My initial problem was that when I made a box plot (ggplot) I got the values by groups (for example, by months) but when I made the same thing in reactive mode, I only got a global boxplot. The solution was to replace the field "display period" (durperiode() ) by : .data[[durperiode()]]

#data
data.duree <- data.frame(stringsAsFactors=FALSE,
       um=c("A1234","A1234","C1345","C1345","Z4453","C1345","C1345","Z4453","A1234"),
       ghm=c("ABC","ABC","C1345","C1345","Z4453","C1345","C1345","Z4453","A1234"),
       duree<- 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"),
      in_a = c('15-01-2020','20-03-2020','21-03-2020', '03-02-2020', '16-01-2020', '03-02-2020', '19-03-2020', '08-02-2020','14-02-2020' ))

library(shiny)
library(shinydashboard)
library(ggplot2)

ui<- dashboardPage(
                   dashboardHeader(), 
                   dashboardSidebar( 
                     menuItem("test", tabName = "test", badgeColor = "green")),
      dashboardBody(
           tabItems(
tabItem(tabName = "test",
        fluidRow(
          column(2,
                 selectInput(inputId = "dur_um", 
                             label = "UM",
                             choices = as.character(unique(data.duree$um)),
                             multiple=TRUE,
                             width = validateCssUnit(200))),
          
          column(3,
                 selectInput(inputId = "dur_ghm",
                             label = "GHM",
                             choices = as.character(unique(data.duree$ghm)),
                             multiple=TRUE,
                             width = validateCssUnit(200))),

weekstart = 1 )), 
          column(4,  selectInput("dur_periode",
                                 label="Display by:",
                                 choices = c("Week_"= "week",
                                             "Month_"   = "month",
                                             "Year_"  = "year1"),
                                 selected   = "month",
                                 width = validateCssUnit(100))),

          plotOutput("graph1", width = "400px", height = "200px")))
)))


server <- function(input, session, output) {

#filtre affichage periode
durperiode<- reactive({
  if (input$dur_periode == "week") { "week" 
  }   else if (input$dur_periode == "month") { "month"
  }   else { "year1" } 
})

dataduree <- reactive({
  dur_um   <- if (is.null(input$dur_um))   unique(data.duree$um) else input$dur_um
  dur_ghm  <- if (is.null(input$dur_ghm))  unique(data.duree$ghm) else input$dur_ghm
  # Filter
  data.duree %>%   group_by_(durperiode()) %>%
    filter( um %in% dur_um, ghm %in% dur_ghm)
})


# graph 
output$graph1 <- renderPlot({    
  # req(nrow(dataduree()) > 0) 
  ggplot(dataduree(), aes(**.data[[durperiode()]]**, y =duree  )) + 
    geom_boxplot(aes(fill = stage(**.data[[durperiode()]]**, after_scale = alpha(fill, 0.4))))
})
} 
shinyApp(ui,server)

Result :one box plot by month -> https://i.stack.imgur.com/4jH0a.png

AST_
  • 21
  • 1
  • 1
  • 5
  • In the ggplot call replace both instances of `durperiode()` (which is a string) with `.data[[durperiode()]]` (which tells ggplot that you mean the variable with name `durperiode()`. See e.g. https://stackoverflow.com/questions/64620046/shiny-not-displaying-ggplot-data – stefan Nov 19 '20 at 10:40
  • It's work very well. Thank you for your advice – AST_ Nov 19 '20 at 12:39

0 Answers0