This is a shiny
app in which first I need to select 3 or more columns from the selectinput()
named Variable
and then 1 from the Group selectInput()
.The first works as with this I set which variables am I going to use in my radarchart. The second I want to be used in order every time to create a radarchart with a legend based on the values of the selected Group with different color for each group
# Load necessary libraries
library(shiny)
library(ggplot2)
library(dplyr)
library(fmsb)
# Sample data (replace this with your actual data)
Test_RShiny_Data <- structure(list(abs_edu = c(1, 0, 1, 0, 1, 0, 1, 1, 0, 1), abs_employ = c(1,
1, 0, 0, 1, 0, 1, 1, 1, 1), abs_wash = c(1, 0, 0, 1, 1, 0, 1,
0, 1, 1), abs_health = c(1, 0, 0, 1, 1, 1, 1, 0, 1, 1), abs_protection = c(1,
0, 0, 0, 1, 0, 1, 0, 1, 1), abs_document = c(1, 1, 0, 1, 1, 1,
1, 1, 1, 1), ast_hhasset = c(0, 1, 1, 1, 0, 1, 1, 0, 1, 1), ast_itc = c(1,
0, 1, 1, 1, 0, 1, 1, 1, 1), ast_transport = c(0, 0, 1, 0, 1,
0, 0, 1, 0, 0), ast_nonessential = c(1, 0, 1, 0, 0, 0, 0, 0,
0, 0), ast_employ = c(0, 1, 1, 0, 0, 0, 0, 1, 0, 1), ast_save = c(0,
0, 1, 0, 0, 0, 0, 0, 0, 1), sc_safecom = c(1, 0, 1, 0, 0, 0,
0, 0, 1, 1), sc_group = c(0, 1, 1, 1, 1, 1, 1, 0, 0, 0), sc_safeharass = c(1,
1, 1, 1, 1, 0, 0, 1, 1, 1), sc_safeterror = c(1, 1, 1, 1, 1,
0, 1, 1, 1, 1), sc_safehh = c(1, 1, 1, 1, 1, 0, 1, 1, 1, 1),
ac_edu = c(3, 3, 3, 2, 3, 2, 3, 3, 3, 3), ac_numemploy = c(0,
1, 1, 1, 1, 1, 0, 1, 0, 0), ac_supportwork = c(0, 0, 1, 1,
1, 0, 1, 0, 1, 1), ac_tension = c(1, 0, 1, 1, 0, 0, 1, 0,
1, 1), cs = c(1, 1, 1, 0.441957086324692, 1, 0, 0.441957086324692,
0.441957086324692, 1, 1), ge = c(0.526506841182709, -0.51858788728714,
0.526506841182709, -0.51858788728714, -0.51858788728714,
0.243249282240868, 0.243249282240868, -0.51858788728714,
-0.51858788728714, 0.526506841182709), gbv = c(0.770168602466583,
0.770168602466583, 0.770168602466583, 0.770168602466583,
0.770168602466583, 0.770168602466583, -0.0019789848010987,
-0.0019789848010987, 0.770168602466583, 0.770168602466583
), country = c("Ukrainian", "Ukrainian", "Moldovan", NA,
"Ukrainian", "Ukrainian", "Ukrainian", "Ukrainian", "Ukrainian",
"Ukrainian"), marital = c("Seperated/divorced/widowed", "Seperated/divorced/widowed",
"Married", "Married", "Single/Engaged", "Married", "Seperated/divorced/widowed",
"Seperated/divorced/widowed", "Seperated/divorced/widowed",
"Seperated/divorced/widowed"), child = c("1-3 children",
"1-3 children", "More than 3 children", "1-3 children", "1-3 children",
"1-3 children", "1-3 children", "1-3 children", "1-3 children",
"1-3 children"), ageg = c(2, 3, 3, 2, 3, 2, 2, 1, 3, 2)), row.names = c(NA,
-10L), class = c("tbl_df", "tbl", "data.frame"))
# Define UI
ui <- fluidPage(
titlePanel("Dynamic Bar Plot"),
sidebarLayout(
sidebarPanel(
selectInput("variable", "Select 3 or more variables", choices = colnames(Test_RShiny_Data),multiple = T),
selectInput("Group", "Group by", choices = colnames(Test_RShiny_Data),multiple = F),
actionButton("goButton", "Go")
),
mainPanel(
plotOutput("radarPlot")
)
)
)
# Define server logic
server <- function(input, output) {
# Reactive expression to process the data
processed_data <- reactive({
req(input$goButton) # Ensure the button is clicked
isolate({
myvars <- input$variable
newdata <- Test_RShiny_Data[myvars]
})
})
output$radarPlot <- renderPlot({
radarchart(processed_data(), axistype = 1,
pfcol = colors_fill,
title = "Correlations of the sub-variables with the AC Pillar",
pcol = 1:ncol(processed_data()), plwd = 2, plty = 1,
cglty = 1, cglwd = 0.5, cglcol = "black",
vlcex = 0.8, caxislabels = seq(0, 1, by = 0.2),
maxmin = TRUE)
legend(x=0.7, y=1, legend = colnames(processed_data()[input$Group,]), bty = "n", pch=20 , text.col = "grey", cex=1.2, pt.cex=3)
})
}
# Run the application
shinyApp(ui = ui, server = server)