make a reactive dataframe by the Input , then choose variables for graphs

Hello everyone!

I learned how to use Shiny last week and I’m creating an app, but I need some help about it ..
My dataset has 2320 observations for 74 variables (numeric and character).
I have in my UI seven input (picker, checkboxgroup,slider..) and in my server there are about
10-15 panels each with a graph.
For the moment they are created directly from the data, without using all the filters/input.
(My input are selecting observations on column, there is an input for one variable/column that i selected)

I am now blocking on two things:

  • I would like if it is possible to create a reactive dataframe, which will adapt herself to the different filters, and create a "sub data frame" that would be used by the server’s graphics.
    (i tried with subset , %in% , filter )

  • when the reactive dataframe is created,
    How can i target a variable/column in this new dataframe?
    For example in a basic graph like barplot, or spineplot/boxplot which use 2 variable, if i wouldlike to select one or 2 column in the new dataframe?
    (I tried to create a reactive dataframe but like the graphics don’t show,
    I don’t know if the problem comes from reactive dataframe, or column targeting.. )
    i've tried to call the reactive function "newdata()", and the data i created inside but it doesnt work.

the problem is the same everywhere so I put an excerpt here, I can put it all if u need
excerpt :

ui <- fluidPage (

  titlePanel("Enquête FCPE déconfinement"),
  
  sidebarLayout(
    sidebarPanel(
      
      pickerInput("VilleEtablissement1","Ville :", 
                  choices = sort(unique(na.omit(unlist(donnees[46])))),
                  options = list(`actions-box` = TRUE,`select-All-Text` = "Sélectionner tout", `deselect-All-Text` = "Désélectionner tout", `None-selected-Text` = "Aucune sélection"), 
                  multiple = T,
                  selected = sort(unique(na.omit(unlist(donnees[46]))))),
  
      checkboxGroupInput("TypeEtablissement1", "Cycle scolaire :",
                         choices =c("Ecole maternelle" , "Ecole élémentaire" , "Collège" , "Lycée", "Lycée professionnel"),
                         selected =c("Ecole maternelle" , "Ecole élémentaire" , "Collège" , "Lycée", "Lycée professionnel")),

[....]


Server : 
shinyServer(function(input, output, session){
  
  #-------------------------------------------------------- Tests reactivity
  
    dataFiltree <- reactive({
      data<-donnees
      data <- filter(data, VilleEtablissement1 == input$VilleEtablissement1)
      data <- filter(data,TypeEtablissement1 == input$TypeEtablissement1)
      data <- filter(data, Type_de_famille == input$Type_de_famille)
      data <- filter(data, Lien_avec_l_enfant_ou_les_enfants == input$Lien_avec_l_enfant_ou_les_enfants)
      data <- filter(data, Nombre_d_enfants <= input$Nombre_d_enfants)
      data <- filter(data, Situation_professionnelle == input$Situation_professionnelle)
      data <- filter(data, Position == input$Position)
      })
    
    dataFiltree2 <- reactive({
      data <-donnees
      data <- data[data$VilleEtablissement1 %in% input$VilleEtablissement1,]
      data <- data[data$TypeEtablissement1 %in% input$TypeEtablissement1,]
      data <- data[data$Type_de_famille %in% input$Type_de_famille,]
      data <- data[data$Lien_avec_l_enfant_ou_les_enfants %in% input$Lien_avec_l_enfant_ou_les_enfants,]
      data <- data[data$Nombre_d_enfants <= input$Nombre_d_enfants,]
      data <- data[data$Situation_professionnelle %in% input$Situation_professionnelle,]
      data <- data[data$Position %in% input$Position,]
})
      
    datafiltree3<-reactive({
      data %>% 
        filter_at(vars(VilleEtablissement1), all_vars())
    })
    
    dataFiltree4  <- reactive({
      data<- subset(data, VilleEtablissement1 == input$VilleEtablissement1
                     & TypeEtablissement1 == input$TypeEtablissement1
                     & Type_de_famille == input$Type_de_famille
                     & Lien_avec_l_enfant_ou_les_enfants == input$Lien_avec_l_enfant_ou_les_enfants
                     & Nombre_d_enfants == input$Nombre_d_enfants
                     & Situation_professionnelle == input$Situation_professionnelle
                     & Position == input$Position)}) 
 
# ------------------------------------------------------------- PLOT / VIZ
                             
    output$Graph1 <- renderPlot({
      eff_c <-table(dataFiltree2$Position)
      barplot(eff_c , xlab ="Avis des répondants", ylab="Effectifs",main = "Êtes-vous pour la reprise de l'école ?", col=brewer.pal( n = 4 , name = "RdYlGn"))
    })

[.......]

      output$Graph7 <- renderPlot({
        x=donnees$TypeEtablissement1
        x=factor(x, levels=c("Ecole maternelle", "Ecole élémentaire", "Collège","Lycée","Lycée professionnel"))
        y=donnees$Position
        eff = table(x,y)
        spineplot(eff, xlab= "Niveau Scolaire", ylab= "Avis", main="Répartition des avis selon le niveau scolaire",col=brewer.pal( n = 4 , name = "RdYlGn"))
      }) 

[.....] 

Thank you for reading, I hope you can help me about this.
Good evening !!

  • : I launched :
    library(shiny)
    library(shinyWidgets)
    library(ggplot2)
    library(plotly)
    library("RColorBrewer")
    library(DT)
    library(readxl)
    library(dplyr)
    library(tidyverse)
    library(lazyeval)
    library(reactlog)

Your reactive data is a function. To call the data in the graph you need to add ().

Try:

    output$Graph1 <- renderPlot({

      eff_c <- dataFiltree2()

      barplot(eff_c$Position , xlab ="Avis des répondants", ylab="Effectifs",main = "Êtes-vous pour la reprise de l'école ?", col=brewer.pal( n = 4 , name = "RdYlGn"))
    })

Does that work? Also have a look at this tutorial:

https://shiny.rstudio.com/tutorial/written-tutorial/lesson6/

Ty for your response !
i tried that but it doesn't work because of my reactive function on the dataframe was deprecated !

So i maked the reactive dataframe like this


#first step is just for slider input

  dataSemiActu <- reactive({
    d <- donnees[donnees$Nombre_d_enfants >= input$Nb_d_enfants[1] & donnees$Nombre_d_enfants <= input$Nb_d_enfants[2],]
  })

#this step makes the reactive dataframe

  dataActu <- reactive ({
    subset(dataSemiActu(),TypeEtablissement1 %in% input$TypeEtablissement1
      & Type_de_famille %in% input$Type_de_famille
      & Lien_avec_l_enfant_ou_les_enfants %in% input$Lien_avec_l_enfant_ou_les_enfants
      & Situation_professionnelle %in% input$Situation_professionnelle
      & VilleEtablissement1 %in% input$VilleEtablissement1
      & Position %in% input$Position)
  })

And your answer work to target some variables ! :smiley:

Have a nice day :slight_smile:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.