AUB
AUB

Reputation: 83

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 8 essays (named 1 to 8) 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:8,"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,])
    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,])
   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 which 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 simple (I am not very good with R yet) examples of CheckboxGroupInput I could use as reference?

Thanks in advance for your time and ideas

Upvotes: 4

Views: 1702

Answers (2)

Joris Meys
Joris Meys

Reputation: 108523

Your problem isn't Shiny but a misunderstanding of the selection.

TrameTest[input$Essais,]

checks whether it finds rownames that correspond to input$Essais, and selects those rows. But from your code and your own answer it's clear that you want those rows where input$Essais appears in the second column. That's something completely different, starting with the fact that rownames have to be unique whereas your new code allows for multiple occurences of input$Essais.

That said, using subset() inside functions is adviced against in ?subset:

This is a convenience function intended for use interactively. For programming it is better to use the standard subsetting functions like [, and in particular the non-standard evaluation of argument subset can have unanticipated consequences.

So to have optimal code, do:

output$plot5 <- renderPlot({
  id <- TrameTest[, 2] %in% input$Essais
  dataTest <- TrameTest[ id , ] 
  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)
  })

Upvotes: 0

AUB
AUB

Reputation: 83

I have found something that works ! In the server part, I start by subsetting my dataframe (TrameTest), keeping only the lines with names corresponding to input$Essais. Then the plot is easy, working with my subsetted dataframe (dataTest).

output$plot5 <- renderPlot({
dataTest<-subset(TrameTest, TrameTest[,2] %in% input$Essais)
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)
  })

Upvotes: 1

Related Questions