In the shiny app above I try to group by selected input but I get:
Caused by error in `all_of()`:
! Can't subset elements that don't exist.
✖ Element `abs_edu` doesn't exist.
library(shiny)
library(tibble)
library(dplyr)
library(ggplot2)
library(tidyr)
# Sample data (assuming you have more columns)
Test_RShiny_Data<-structure(list(abs_edu = c(1, 0, 1, 0, 1), abs_employ = c(1,
1, 0, 0, 1), abs_wash = c(1, 0, 0, 1, 1), abs_health = c(1, 0,
0, 1, 1), abs_protection = c(1, 0, 0, 0, 1), abs_document = c(1,
1, 0, 1, 1), ast_hhasset = c(0, 1, 1, 1, 0), ast_itc = c(1, 0,
1, 1, 1), ast_transport = c(0, 0, 1, 0, 1), ast_nonessential = c(1,
0, 1, 0, 0), ast_employ = c(0, 1, 1, 0, 0), ast_save = c(0, 0,
1, 0, 0), sc_safecom = c(1, 0, 1, 0, 0), sc_group = c(0, 1, 1,
1, 1), sc_safeharass = c(1, 1, 1, 1, 1), sc_safeterror = c(1,
1, 1, 1, 1), sc_safehh = c(1, 1, 1, 1, 1), ac_edu = c(3, 3, 3,
2, 3), ac_numemploy = c(0, 1, 1, 1, 1), ac_supportwork = c(0,
0, 1, 1, 1), ac_tension = c(1, 0, 1, 1, 0), cs = c(1, 1, 1, 0.441957086324692,
1), ge = c(0.526506841182709, -0.51858788728714, 0.526506841182709,
-0.51858788728714, -0.51858788728714), gbv = c(0.770168602466583,
0.770168602466583, 0.770168602466583, 0.770168602466583, 0.770168602466583
), country = c("Ukrainian", "Ukrainian", "Moldovan", NA, "Ukrainian"
), marital = c("Seperated/divorced/widowed", "Seperated/divorced/widowed",
"Married", "Married", "Single/Engaged"), child = c("1-3 children",
"1-3 children", "More than 3 children", "1-3 children", "1-3 children"
), ageg = c(2, 3, 3, 2, 3), res = structure(c(0.0514511360129742,
-0.215042617390641, -0.0574834900086346, -0.0544855258954085,
0.0261650395060347), dim = c(5L, 1L), dimnames = list(NULL, "Res"), class = c("lavaan.matrix",
"matrix")), rci = structure(c(70.7314751579487, 33.9672828176585,
55.7033797807544, 56.1169644441105, 67.2431272937182), dim = c(5L,
1L), dimnames = list(NULL, "Res"), class = c("lavaan.matrix",
"matrix"))), row.names = c(NA, -5L), class = c("tbl_df", "tbl",
"data.frame"))
# Shiny UI
ui <- fluidPage(
titlePanel("Select Column Names"),
sidebarLayout(
sidebarPanel(
selectInput("vars_rci", "Choose a column:",
choices = colnames(Test_RShiny_Data),
multiple = FALSE)
),
mainPanel(
plotOutput("stacked")
)
)
)
# Shiny Server
server <- function(input, output) {
rsm_data <- reactive({
req(input$vars_rci)
d <- Test_RShiny_Data %>%
group_by(across(all_of(input$vars_rci))) %>%
summarise(
abs = mean(abs_edu, na.rm = TRUE) + mean(abs_employ, na.rm = TRUE) + mean(abs_wash, na.rm = TRUE) + mean(abs_health, na.rm = TRUE) + mean(abs_protection, na.rm = TRUE) + mean(abs_document, na.rm = TRUE),
ast = mean(ast_hhasset, na.rm = TRUE) + mean(ast_itc, na.rm = TRUE) + mean(ast_transport, na.rm = TRUE) + mean(ast_nonessential, na.rm = TRUE) + mean(ast_employ, na.rm = TRUE) + mean(ast_save, na.rm = TRUE),
sc = mean(sc_safecom, na.rm = TRUE) + mean(sc_group, na.rm = TRUE) + mean(sc_safeharass, na.rm = TRUE) + mean(sc_safeterror, na.rm = TRUE) + mean(sc_safehh, na.rm = TRUE),
ac = mean(ac_edu, na.rm = TRUE) + mean(ac_numemploy, na.rm = TRUE) + mean(ac_supportwork, na.rm = TRUE) + mean(ac_tension, na.rm = TRUE)
) %>%
pivot_longer(cols = c(abs, ast, sc, ac), names_to = "Pillar", values_to = "Value") %>%
group_by(across(all_of(input$vars_rci))) %>%
mutate(Percentage = Value / sum(Value))
return(d)
})
output$stacked <- renderPlot({
req(rsm_data())
ggplot(rsm_data(), aes_string(x = input$vars_rci, y = "Value", fill = "Pillar")) +
geom_bar(stat = "identity") +
geom_text(aes(label = sprintf("%.2f", Percentage), y = Value + 0.05),
position = position_stack(vjust = 0.5)) +
labs(title = paste("RSM by", input$vars_rci), x = input$vars_rci, y = "Value") +
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
scale_fill_manual(values = c("abs" = "skyblue", "ast" = "orange", "sc" = "green", "ac" = "red"))
})
}
# Run the Shiny App
shinyApp(ui = ui, server = server)