vendredi 23 juin 2017

checkboxGroupInput use in R shiny for ggplot

I am trying to include checkboxes in a shiny app supposed to draw graphics. My problem seems simple but I have not been able to solve it by myself :( , despite the multiple CheckboxGroupInput questions here on StackOverflow or the R help tool...

So, I have a scientific study including 22 essays (named 1 to 22) and 5 control essays (named PC1 to PC5). I would like my app to ask through checkboxes which essays you want to look at, and to draw them in the same graphic. Please note I already have a dropdown menu asking which two variables you wish to see (in 2 graphics), and this is working like a charm.

Here is a simplified version of my ui :

ui <- dashboardPage(
  dashboardHeader(title="Test",
              dropdownMenu(type = "notifications",
                           notificationItem(
                             text = "Verifier les points centraux",
                             icon = icon("exclamation-triangle"),
                             status = "warning"
                           )
              )             
  ),
  dashboardSidebar(
   sidebarMenu(
     menuItem("Suivi par Variable", tabName = "SuiviVariable", icon = 
icon("area-chart"))
  )
  ),
  dashboardBody(
  # Suivi par Variable tab content
    tabItem(tabName = "SuiviVariable",
          h2("Graphique de Suivi pour Chaque Variable d'Intérêt"),
          fluidRow(
            box(title = "Choisir deux variables à comparer", status = 
"info",br(),
                selectInput("Variable1", label = "Variable 1",
                            choices = list_variables, 
                            selected = list_variables[1]),
                selectInput("Variable2", label = "Variable 2",
                            choices = list_variables, 
                            selected = list_variables[1]),
                checkboxGroupInput("Essais", "Essais à afficher :",c(1:22,"PC1", "PC2", "PC3", "PC4", "PC5")),
                plotOutput("plot5", height = 600),
                plotOutput("plot6", height = 600)
            )
          )
  )  
  ))

The ui part seems okay, as the checkboxes appear correctly.

Here is the server part :

server<-function(input, output){ 
      output$plot5 <- renderPlot({
    a<- as.vector(input$Essais)
    dataTest <- as.data.frame(TrameTest[a,])
    bifrost <- c("purple","orange", "darksalmon", 
 "cadetblue","dimgray","forestgreen", 
"honeydew","navy","plum","tomato","chocolate","deeppink","lightgoldenrod")
 ggplot(data=dataTest, aes(x= N_ByEssai,y=dataTest[,input$Variable1], group= Nom, colour=Nom))+
  geom_line()+
  xlab("Duree de l'Essai")+
  ylab(input$Variable1)
  })
  output$plot6 <- renderPlot({
   dataTest <- as.data.frame(TrameTest[input$Essais,])
   bifrost <- c("purple","orange", "darksalmon", 
   "cadetblue","dimgray","forestgreen", 
  "honeydew","navy","plum","tomato","chocolate","deeppink","lightgoldenrod")
   ggplot(data=dataTest, aes(x= N_ByEssai,y=dataTest[,input$Variable2],group= Nom, colour=Nom))+
   geom_line()+
   xlab("Duree de l'Essai")+
   ylab(input$Variable2)
  })}

There you can see I have tried to use input$Essais, in vain. I have read that CheckboxGroupOutput returns a character vector and I felt it may have been my problem, so I tried to trick it around with as.vector but it doesn't change the result : Depending on wich boxes are checked, I get an empty graphic or a graphic with the legend PC1/NA and one only line at values I don't recognize (?!).

Do you know why I can't just use TrameTest[input$Essais,] to be my x values? Is it because it is not a vector ? Do you have simle (I am not very good with R yet) examples of CheckboxGroupInput I could use as reference ?

Thanks in advance for your time and ideas




Aucun commentaire:

Enregistrer un commentaire