Felix G. Lopez
We start by loading the necessary libraries: shiny for
building the web application, readxl for reading Excel
files, dplyr for data manipulation, and
ggplot2 for plotting.
## Warning: package 'shiny' was built under R version 4.3.3
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(ggplot2)
# Read the Excel file from a GitHub URL
fileURL <- "https://github.com/felixglopez/cbo_1/raw/main/brasil_v11.csv"
# Specify the destination file name
destfile <- "brasil_v11.csv"
# Download the file from the URL
download.file(fileURL, destfile)
# Read the downloaded CSV file
data <- read.csv(destfile, sep = ";")
# Select relevant columns
remdata <- data %>%
select(ano, starts_with("rem_media"))
head(data [,1:5])## ano federal_total rem_media_federal_total federal_direto
## 1 2021 857923 12024.99 469862
## 2 2020 1077104 11303.05 700929
## 3 2019 938713 12741.43 541991
## 4 2018 1101196 11373.43 661035
## 5 2017 1181893 11625.90 733350
## 6 2016 1171346 11162.09 765426
## rem_media_federal_direto
## 1 12654.51
## 2 10937.31
## 3 13079.67
## 4 11069.17
## 5 11424.02
## 6 10915.07
## [1] 37 41
ui <- fluidPage(
titlePanel("Comparing salaries in different bureaucracy levels"),
sidebarLayout(
sidebarPanel(
selectInput("variable", "Select Variable", choices = NULL),
radioButtons("view", "Select View", choices = c("Table", "Summary"), selected = "Summary")
),
mainPanel(
tableOutput("tableOutput"),
verbatimTextOutput("summaryOutput"),
plotOutput("comparisonPlot")
)
)
)server <- function(input, output, session) {
observe({
updateSelectInput(session, "variable", choices = names(remdata))
})
filtered_data <- reactive({
req(input$variable)
remdata[[input$variable]]
})
output$tableOutput <- renderTable({
req(input$view == "Table")
head(remdata[, c(input$variable, "rem_media_federal_total")], 10)
})
output$summaryOutput <- renderPrint({
req(input$view == "Summary")
selected_var_summary <- summary(filtered_data())
federal_exec_summary <- summary(remdata$rem_media_federal_total)
list(
"Selected Variable Summary" = selected_var_summary,
"Remuneração media executivo federal" = federal_exec_summary,
"Difference" = selected_var_summary - federal_exec_summary
)
})
output$comparisonPlot <- renderPlot({
req(input$variable)
selected_var_data <- remdata[[input$variable]]
federal_exec_data <- remdata$rem_media_federal_total
if (is.numeric(selected_var_data) & is.numeric(federal_exec_data)) {
plot_data <- data.frame(
Category = c(rep("Selected Variable", length(selected_var_data)), rep("Federal Exec", length(federal_exec_data))),
Value = c(selected_var_data, federal_exec_data)
)
ggplot(plot_data, aes(x = Category, y = Value, fill = Category)) +
geom_boxplot() +
labs(title = paste("Comparison of", input$variable, "with 'rem_media_federal_total'"), y = "Values") +
theme_minimal()
} else {
plot(1, type = "n", ann = FALSE)
text(1, 1, "Selected variable and 'federal_exec' must be numeric for plotting.", cex = 1.2)
}
})
}| ### Uncomment the lines below to install rsconnect and deploy the application |
| #install.packages(‘rsconnect’) library(rsconnect) |
| #Set account information for deployment #rsconnect::setAccountInfo(name=‘fglopez’, # token=‘9AE9C3B3FC046BF4197D564806098C9F’, # secret=‘SECRET’) |
| # Deploy the application # deployApp(“~/Documents/GitHub/cbo_1”) |