R shiny: Interactive ggbarplot. Checkboxgroup within a variable

Viewed 17

I would like to make an interactive barplot showing the number of samples collected per Group, i.e. plot 1. I would like to be able to choose how many groups to plot with a check box group in shiny. (find an example of my data at the end of the post)

I created this app, but it doesn't matter what I try to change I get error or the barplot not plotting. I am sure there is an easy fix but I can´t find it.

You can also see that I would also like to select my data in relation with time (i created a dateRangeInput) but i am trying to tackle one problem at the time, if you also know how to do it and want to solve it, it would be amazing!

Thanks for all the help in advance!

#directory
setwd("/Users/mariagranell/Repositories/data_summaries/")

#Creating a shiny app from sratch
library(shiny)
library(magrittr) # needs to be run every time you start R and want to use %>%
library(dplyr)    # alternatively, this also loads %>%
library(ggplot2)

#data
d <- read.csv("swabs-20-09-22.CSV")
d<-within(d,{
  ManualDate<-as.Date(ManualDate)
  Date<-as.Date(format(as.POSIXct(Date, format="%d/%m/%Y"), "%Y-%m-%d"))
})

#Define the UI. The layout of the app
ui <- fluidPage(
  titlePanel("Saliva sampling"),
  
  sidebarLayout(
    sidebarPanel(
      Position = "Left",
      
      #text before the choices
      "Choose which parameters you want to plot",
      
      #create a checkbox
      checkboxGroupInput("troup", 
                         h4("Select a Group"), 
                         choices = c("Ankhase", 
                                        "Kubu", 
                                        "Noha"), selected = "Noha"),
      #date range
      dateRangeInput("dates", h4("Date range"))),
    mainPanel("main panel",
       
      #output to put in the main panel
      plotOutput(outputId = "barplot")
              
              )
  ),
)
    
#Define the server logic
server <- function(input, output) {
  output$barplot <- renderPlot({
    
      table_summary<-d%>%
        group_by(Group)%>%
        tally()
      table_summary<- fortify(table_summary)
      table_summary[which(table_summary$Group %in% input$troup),]%>%
        ggplot(aes(x=Group, y=n))+
        geom_col()
  })
}
 



#Run the app
shinyApp(ui = ui,server = server)
```




>this is a reproduction of my data: 
structure(list(ManualDate = structure(c(-718931, -718931, -711261, 
-711261, -710896, -710896, -709800, -708704, -707974, -718535, 
-718535, -717439, -717074, -717074, -717074, -717074, -717074, 
-717074, -717074, -717074), class = "Date"), Date = structure(c(19254, 
19254, 19254, 19254, 19254, 19254, 19254, 19254, 19254, 19237, 
19237, 19240, 19241, 19241, 19241, 19241, 19241, 19241, 19241, 
19241), class = "Date"), Group = c("Noha", "Noha", "Baie Dankie", 
"Baie Dankie", "Noha", "Baie Dankie", "Ankhase", "Noha", "Noha", 
"Noha", "Noha", "Lemon Tree", "Noha", "Noha", "Noha", "Noha", 
"Noha", "Noha", "Baie Dankie", "Baie Dankie"), Data = c("Swabs Form", 
"Swabs Form", "Swabs Form", "Swabs Form", "Swabs Form", "Swabs Form", 
"Swabs Form", "Swabs Form", "Swabs Form", "Swabs Form", "Swabs Form", 
"Swabs Form", "Swabs Form", "Swabs Form", "Swabs Form", "Swabs Form", 
"Swabs Form", "Swabs Form", "Swabs Form", "Swabs Form"), IDIndividual1 = c("Gri", 
"Xih", "Oup", "Pix", "Raba", "Oerw", "Ndaw", "Roc", "Ram", "Gri", 
"Griv", "Rat", "Xih", "Xop", "Riva", "Prat", "Gri", "Gree", "Piep", 
"Sirk"), Time = c(NA, NA, 13.5, 13.37, 11.2, 12, NA, NA, NA, 
13.43, 17.4, 11.56, 6.37, 8.27, 8.27, 8.27, 13.08, 13.1, 13.55, 
16.45), SwabsDescription = c("Rope", "Rope", "Rope", "Rope", 
"Rope", "Rope", "Rope", "Rope", "Salimetrics", "Salimetrics", 
"Salimetrics", "Rope", "Rope", "Rope", "Rope", "Rope", "Rope", 
"Rope", "Rope", "Rope"), Substance = c("", "Corn; Marmalade", 
"Corn", "Corn", "Corn", "Corn", "Corn", "Corn", "Corn", "Corn; Mermelaide", 
"Corn; Mermelaide", "Corn", "Corn; Mermelaide", "Corn; Mermelaide", 
"Corn; Mermelaide", "Corn; Mermelaide", "Corn; Mermelaide", "Corn; Mermelaide", 
"Corn; Other", "Corn; Other"), OtherSubstance = c("", "", "", 
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "juice", 
"juice"), SwabsResponse = c("", "", "", "", "", "", "", "", "", 
"Only took corn", "Smell", "Ignore", "Only took corn", "Smell", 
"Smell", "Only took corn", "Only took corn; Lick", "Only took corn", 
"Chew; Lick", "Only took corn; Lick"), OtherResponse = c("", 
"", "", "", "", "", "", "", "", "", " ", "", "licking its hand", 
" ", " ", "", "", "", "", ""), SwabCollected = c("Yes", "Yes", 
"Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "No", "No", 
"No", "No", "No", "No", "No", "Yes", "No", "Yes", "No"), Observers = c("", 
"", "", "", "", "", "", "", "", "Maria G; Michael; Michelle", 
"Nokubonga; Michael; Michelle", "Maria C; L\xe9a; Jonas", "Maria G", 
"Maria G", "Maria G", "Maria G", "Veronicca", "Giulia; Veronicca", 
"L\xe9a", "L\xe9a"), Remarks = c("", "", "", "", "", "", "", 
"", "", "", "", "", "", "", "", "", "", "", "", ""), Nbswab = c(NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA)), row.names = c(NA, 20L), class = "data.frame")
0 Answers
Related