Home > Mobile >  How to select specific interesting groups for the boxplot in R Shiny app?
How to select specific interesting groups for the boxplot in R Shiny app?

Time:03-07

I have some information in a file called data.csv. Here is the link to the file enter image description here

To this, I would like to add some more radio buttons/select input where I would like to select the Group (Type1 to Type10), based on my interest.

Along with the above picture, I want to add some options for Group so that I can select only the interesting Group comparisons and download them.

For eg: I want to see the boxplot comparison between Type1 vs Type7 and it should show boxplot only for this comparison and download it.

Another eg: Type1 vs Type5 vs Type4 and it should show boxplot only for this comparison and download it

How do I do this? Can anyone please help me? Thank you.

CodePudding user response:

You can use a selectizeInput with multiple = TRUE to select the groups you want to compare. This input can then be used to filter the dataset, the axis limit, and the comparisons you want to test.

I've just pasted the parts below, where I made changes to your code (selectizeInputin the ui, and your renderPlot expression)

ui <- fluidPage(
  titlePanel("values"),
  sidebarLayout(
    sidebarPanel(
      selectInput(inputId = "thegene", label = "Gene", choices = choi, selected = "geneC"),
      selectizeInput(inputId = "group", label = "Group", choices = positions, 
                     multiple = TRUE, selected=positions),
      radioButtons(inputId = "colour", label = "Colour", choices=c("white"),selected="white"),
      radioButtons(inputId = "FileType", label = "file type", choices = list("png", "pdf"), selected = "pdf"),
      width = 3),
    mainPanel(
      plotOutput("boxplot"),
      downloadButton(outputId = "downloadPlot", label = "Download"),
      width = 9
    )
  )
)

output$boxplot <- renderPlot({
    
    # make sure we remove comparisons that are not possible
    comparisons_reduced <- purrr::map(my_comparisons, function(m) {
        if(sum(m %in% input$group) == 2) {
          m
        } else {
          NULL
        }
      }
    )
    comparisons_reduced <- comparisons_reduced[lengths(comparisons_reduced)!=0]
    
    gg <- ggboxplot(data = dat() %>% 
                      dplyr::filter(Group %in% input$group), 
                    x = "Group", y = "value", color = "Group", 
                    add = "jitter")   
      xlab("")   ylab("values")  
      stat_compare_means(comparisons = comparisons_reduced, label = "p.signif", method = "wilcox.test")
    
    gg2 <- gg   scale_x_discrete(limits = positions[positions %in% input$group]) 
      theme_bw(base_size = 14)   stat_n_text()  
      theme(axis.text=element_text(size=13, face = "bold", color = "black"),
            axis.title=element_text(size=13, face = "bold", color = "black"),
            strip.text = element_text(size=13, face = "bold", color = "black"),
            legend.text = element_text(size=13, face = "bold", color = "black"),
            legend.title = element_text(size=13, face = "bold", color = "black"),
            legend.position = "none",
            axis.text.x = element_text(angle = 90))
    
    vals$gg2 <- gg2
    
    print(gg2)
  })

enter image description here

  • Related