R Shiny: Crosstable and Plot grouping with reactive values - empty outputs

I am building a Shiny app to display variables of the "European Social Survey" (table and graph). Therefore I created conditional panels with "selectInput" where the user can select which variable should be displayed. In a second step I want to group the displayed variable by e.g. gender. For doing so I included a checkbox. If this checkbox is TRUE a further conditional panel shows up where the user can choose the independent variable.
I tried to group the plot by using the facet_grid command - without success. Further I tried the generate a very simple crosstable (trying both a datatable command with a dataframe and a table command; latter in the expample below) - also without success. Dependent and independent variables are shown and reactive but empty.

Any advice?

library(shiny)
library(shinydashboard)
library(shinydashboardPlus)
library(tidyverse)
library(haven)
library(likert)
library(DT)
library(plotly)

levels.netusoft <- c('Sehr wenig', 'Etwas', 'Stark', 'Sehr stark', 'Verweigert', 'Weiß nicht', 'Keine Antwort')
levels.ppltrst <- c('1', '2', '3', '4', '5', '6', 'Verweigert', 'Weiß nicht', 'Keine Antwort')
levels.polintr <- c('Überhaupt nicht', 'Sehr wenig', 'Etwas', 'Stark', 'Sehr stark', 'Verweigert', 'Weiß nicht', 'Keine Antwort')
levels.psppsgva <- c('Überhaupt nicht fähig', 'Wenig fähig', 'Ziemlich fähig', 'Sehr fähig', 'Vollkommen fähig', 'Verweigert', 'Weiß nicht', 'Keine Antwort')
levels.actrolga <- c('Wenig fähig', 'Ziemlich fähig', 'Sehr fähig', 'Vollkommen fähig', 'Verweigert', 'Weiß nicht', 'Keine Antwort')
levels.gndr <- c('männlich', 'weiblich')
dataset <- data.frame('netusoft'=factor(sample(levels.netusoft[1:7], 100, replace=TRUE)),
                      'ppltrst'=factor(sample(levels.ppltrst[1:8], 100, replace=TRUE)),
                      'polintr'=factor(sample(levels.polintr[1:8], 100, replace=TRUE)),
                      'psppsgva'=factor(sample(levels.psppsgva[1:8], 100, replace=TRUE)),
                      'actrolga'=factor(sample(levels.actrolga[1:7], 100, replace=TRUE)),
                      'gndr'=factor(sample(levels.gndr[1:2], 100, replace=TRUE)),
                      check.names=FALSE)


# ----- UI
ui <- fluidPage(
    dashboardPage(
        dashboardHeader(title = "European Social Survey Österreich Dashboard", titleWidth = 300),
        dashboardSidebar(width = 300,
                         selectInput(inputId='round', label="Wählen Sie eine ESS Runde aus",  
                                     c("ESS 1" = "1",
                                       "ESS 2" = "2",
                                       "ESS 3" = "3",
                                       "ESS 4" = "4",
                                       "ESS 5" = "5",
                                       "ESS 7" = "7",
                                       "ESS 8" = "8",
                                       "ESS 9" = "9"),
                                     selected = "9", selectize = FALSE), #end selectinput
                         conditionalPanel(
                             condition = "input.round == '9'",
                             selectInput(inputId='battery', label="Wählen Sie Themenfeld aus",  
                                         c("A: Medien-, Internetnutzung, Soziales Vertrauen" = "A",
                                           "B: Politische Variablen, Immigration" = "B"), selectize = FALSE), #end selectinput
                         ), #end conditionalPanel
                         conditionalPanel(
                             condition = "input.round == '9' && input.battery == 'A'",
                             selectInput(inputId = "avA_9", label = "Wählen Sie eine Frage aus", 
                                         c("A2|Häufigkeit Internetnutzung" = "netusoft", 
                                           "A4|Vertrauen in Mitmenschen" = "ppltrst"), selectize = FALSE), #end selectInput
                         ), #end conditionalPanel 
                         conditionalPanel(
                             condition = "input.round == '9' && input.battery == 'B'",
                             selectInput(inputId = "avB_9", label = "Wählen Sie eine Frage aus", 
                                         c("B1|Interesse an Politik" = "polintr", 
                                           "B2|Politische Mitsprachem?glichkeit" = "psppsgva", 
                                           "B3|Fähigkeit politischen Engagements " = "actrolga"), selectize = FALSE) #end selectInput
                         ), #end conditionalPanel
                         checkboxInput(
                             inputId = "group",
                             label = "Daten gruppieren",
                             value = FALSE), #end checkbox
                         
                         conditionalPanel(
                             condition = "input.group==true",
                             selectInput(
                                 inputId = "UV",
                                 label = "Daten gruppieren nach:",
                                 c("Geschlecht" = "gndr")
                             ) # end conditionalPanel
                         )
                         
        ), # end dashboardSidebar
        
        dashboardBody(
            
            fluidRow(
                box(width = 7, status = "info", solidHeader = TRUE,
                    title = "Table:",
                    dataTableOutput("tabelle", width = "100%")
                ),
                box(width = 8, status = "info", solidHeader = TRUE,
                    title = "Graph:",
                    plotOutput("plot", width = "auto", height = 500)
                )
            ) # end fluidRow
            
        ) #end dashboardBody
    )
)

server <- function(input, output) {
    
    av.select <- reactive({
        if (input$battery == "A" && input$round == "9") {
            av.select <- input$avA_9
        }
        else if (input$battery == "B" && input$round == "9") {
            av.select <- input$avB_9
        }
        return(av.select)
    })
    
    
    #Plotting the data  
    plot.data <- reactive({
        data <- subset(dataset, select=c(av.select(), input$UV))
        data <- data[complete.cases(data)==1,] %>%
            mutate_all(as_factor) %>%
            droplevels(exclude = c("Weiß nicht", "Verweigert", "Keine Antwort")) %>%
            as.data.frame()
    }) 
    
    
    output$plot <- renderPlot({
        plot.data.g <- likert(plot.data()[,1, drop=FALSE])
        p <- plot(plot.data.g) 
        
        if(input$group==TRUE) {
            p <- plot(plot.data.g) + facet_grid(.~input$UV)
        }
        p
    })
    
    #Creating the table
    output$tabelle <- renderDataTable({
        x <- av.select()
        dataset %>%
            count(!!as.symbol(x)) %>%
            mutate(Antwortkategorie=as_factor(!!as.symbol(x))) %>%
            mutate(n=n) %>%
            mutate(Prozent = prop.table(n)) %>%
            mutate('Kum. Prozent' = cumsum(Prozent)) %>%
            as.data.frame() -> for.table
        
        y <- input$UV
        
        test_tab <- table(x, y) %>% as.data.frame()
        
        if(input$group==FALSE){  
            datatable(for.table[,c(3,2,4,5)], extensions = 'Buttons', options = list(dom = 'Brtip', buttons = c('copy', 'csv', 'excel', 'pdf', 'print'))) %>%
                formatPercentage(c('Prozent','Kum. Prozent'), 1) 
        }
        else if(input$group==TRUE){
            table(x, y)
            
        }
    })
    
    
}    

shinyApp(ui, server)

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.