This app starts with a input field to write the name of a user. Second, it asks the user to provide 5 categories in text form. Then, the user needs to evaluate these categories by scoring them from 1-10. Finally, the vizualization shows the categories, the scores of the different user to compare the scores.
Drop files here
or click to upload
library(shiny)
library(ggplot2)
library(dplyr)
source("data_management.R")
source("ui_components.R")
ui <- fluidPage(
titlePanel("Category Scoring Comparison"),
sidebarLayout(
sidebarPanel(
# User name input
textInput("user_name", "Enter your name:", ""),
# Conditional panel for category inputs
conditionalPanel(
condition = "input.user_name != ''",
uiOutput("category_inputs")
),
# Conditional panel for scoring
conditionalPanel(
condition = "input.categories_ready",
uiOutput("score_inputs")
),
# Submit button
conditionalPanel(
condition = "input.categories_ready",
actionButton("submit", "Submit Scores")
)
),
mainPanel(
plotOutput("comparison_plot")
)
)
)
server <- function(input, output, session) {
# Initialize reactive values
rv <- reactiveValues(
categories = NULL,
scores = NULL,
all_data = load_data()
)
# Generate category input fields
output$category_inputs <- renderUI({
if (input$user_name == "") return(NULL)
tagList(
lapply(1:5, function(i) {
textInput(paste0("category_", i),
label = paste("Category", i),
value = "")
}),
actionButton("categories_ready", "Set Categories")
)
})
# Generate score input fields
output$score_inputs <- renderUI({
req(input$categories_ready)
categories <- sapply(1:5, function(i) input[[paste0("category_", i)]])
rv$categories <- categories
tagList(
lapply(1:5, function(i) {
sliderInput(paste0("score_", i),
label = categories[i],
min = 1,
max = 10,
value = 5)
})
)
})
# Handle submission
observeEvent(input$submit, {
scores <- sapply(1:5, function(i) input[[paste0("score_", i)]])
new_data <- data.frame(
user = input$user_name,
category = rv$categories,
score = scores
)
rv$all_data <- save_data(new_data)
})
# Generate comparison plot
output$comparison_plot <- renderPlot({
req(rv$all_data)
ggplot(rv$all_data, aes(x = category, y = score, fill = user)) +
geom_bar(stat = "identity", position = "dodge") +
theme_minimal() +
labs(title = "Category Scores Comparison",
x = "Category",
y = "Score") +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
})
}
shinyApp(ui = ui, server = server)
Hi! I can help you with any questions about Shiny and R. What would you like to know?