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 panels shows up where the user can choose the independent variable:
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
output$plot <- renderPlot(function(){
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() -> plot.data
})
plot.data.g <- likert(items = plot.data[,1, drop=FALSE], grouping = plot.data[,2,drop=FALSE])
if(input$group==FALSE)
{plot(plot.data.g) +
ggtitle(q_text()) +
xlab("Frage")}
if(input$group==TRUE)
{plot(plot.data.g) +
ggtitle(q_text()) +
xlab("Frage") +
labs(x=av.select()) +
facet_grid(input$UV)}
})
#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
print(for.table)
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)
})
}
shinyApp(ui, server)
My question/problem is now: How can I generate to group the dependent and independent variable server-side? My code returns an empty plot.
There are several problems in your definition of output$plot
. If you run into this sort of problem in the future, I suggest you break the problem down into smaller steps: start at the beginning - do you have the correct input data? Then go to the next step: does your summary function work? Etc, etc.
The issues here seem to be:
reactive
definition inside a renderXXX
call.reactive
inside the renderPlot
to an object.renderPlot
defines a function that is never called.I don't have the lickert
package and don't want to install it just to answer a StackOverflow question, so I've modified your renderPlot
to produce a simple bar chart of the input data, which I suspect is close to what you want. You'll have to make obvious modifications to restore your original plot.
Replace your current definition of output$plot
with:
#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()
data
})
output$plot <- renderPlot({
# plot.data.g <- likert(items = plot.data[,1, drop=FALSE], grouping = plot.data[,2,drop=FALSE])
# p <- plot(plot.data.g) + ggtitle(q_text()) + xlab("Frage")
p <- plot.data() %>% ggplot() + geom_bar(aes(x=netusoft))
if(input$group) {
p <- p + facet_grid(input$UV)
}
p
})
Here's an example of the plot produced with grouping requested.