prioritized.grouping/app/server_raw.R

106 lines
2.7 KiB
R
Raw Normal View History

library(shiny)
2024-01-11 09:43:23 +01:00
server <- function(input, output, session) {
# source("https://git.nikohuru.dk/au-phd/PhysicalActivityandStrokeOutcome/raw/branch/main/side%20projects/assignment.R")
# source(here::here("R/group_assign.R"))
dat <- reactive({
2024-01-11 09:43:23 +01:00
# input$file1 will be NULL initially. After the user selects
# and uploads a file, head of that data file by default,
# or all rows if selected, will be shown.
2024-01-11 09:43:23 +01:00
req(input$file1)
# Make laoding dependent of file name extension (file_ext())
ext <- file_extension(input$file1$datapath)
tryCatch(
{
if (ext == "csv") {
df <- utils::read.csv(input$file1$datapath,na.strings = c("NA", '""',""))
} else if (ext %in% c("xls", "xlsx")) {
df <- openxlsx::read.xlsx(input$file1$datapath,na.strings = c("NA", '""',""))
} else if (ext == "ods") {
df <- readODS::read_ods(file = file)
} else {
stop("Input file format has to be on of:
'.csv', '.xls', '.xlsx' or '.ods'")
}
},
error = function(e) {
# return a safeError if a parsing error occurs
stop(safeError(e))
}
)
2024-01-11 09:43:23 +01:00
return(df)
})
dat_pre <- reactive({
2024-01-11 09:43:23 +01:00
# req(input$file2)
# Make laoding dependent of file name extension (file_ext())
if (!is.null(input$file2$datapath)){
ext <- file_extension(input$file2$datapath)
2024-01-11 09:43:23 +01:00
if (ext == "csv") {
df <- utils::read.csv(input$file2$datapath,na.strings = c("NA", '""',""))
2024-01-11 09:43:23 +01:00
} else if (ext %in% c("xls", "xlsx")) {
df <- openxlsx::read.xlsx(input$file2$datapath,na.strings = c("NA", '""',""))
} else if (ext == "ods") {
df <- readODS::read_ods(file = file)
2024-01-11 09:43:23 +01:00
} else {
stop("Input file format has to be on of:
'.csv', '.xls', '.xlsx' or '.ods'")
2024-01-11 09:43:23 +01:00
}
2024-01-11 09:43:23 +01:00
return(df)
} else {
return(NULL)
}
})
groups <-
reactive({
grouped <- prioritized_grouping(
data = dat(),
excess_space = input$excess,
pre_grouped = dat_pre()
2024-01-11 09:43:23 +01:00
)
return(grouped)
2024-01-11 09:43:23 +01:00
})
plot.overall <- reactive({
dplyr::case_match(input$overall.plot,
"yes"~TRUE,
"no"~FALSE,
.default=NULL)
})
output$raw.data.tbl <- renderTable({
groups()$export
2024-01-11 09:43:23 +01:00
})
output$pre.groups <- renderTable({
2024-01-11 09:43:23 +01:00
dat_pre()
})
output$input <- renderTable({
2024-01-11 09:43:23 +01:00
dat()
})
output$groups.plt <- renderPlot({
grouping_plot(groups(),overall = plot.overall())
2024-01-11 09:43:23 +01:00
})
2024-01-11 09:43:23 +01:00
# Downloadable csv of selected dataset ----
output$downloadData <- downloadHandler(
filename = "prioritized_grouping.csv",
2024-01-11 09:43:23 +01:00
content = function(file) {
write.csv(groups()$export, file, row.names = FALSE)
2024-01-11 09:43:23 +01:00
}
)
2024-01-11 09:43:23 +01:00
}