prioritized.grouping/R/server.R

95 lines
2.3 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 <- 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 {
stop("Input file format has to be either '.csv', '.xls' or '.xlsx'")
}
},
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 <- read.csv(input$file2$datapath,na.strings = c("NA", '""',""))
} else if (ext %in% c("xls", "xlsx")) {
df <- openxlsx::read.xlsx(input$file2$datapath,na.strings = c("NA", '""',""))
2024-01-11 09:43:23 +01:00
} else {
stop("Input file format has to be either '.csv', '.xls' or '.xlsx'")
}
2024-01-11 09:43:23 +01:00
return(df)
} else {
return(NULL)
}
})
2024-01-11 09:43:23 +01:00
assign <-
reactive({
2024-01-11 09:43:23 +01:00
assigned <- group_assignment(
ds = dat(),
excess_space = input$ecxess,
pre_assign = dat_pre()
)
return(assigned)
})
output$raw.data.tbl <- renderTable({
2024-01-11 09:43:23 +01:00
assign()$export
})
output$pre.assign <- renderTable({
2024-01-11 09:43:23 +01:00
dat_pre()
})
output$input <- renderTable({
2024-01-11 09:43:23 +01:00
dat()
})
output$assign.plt <- renderPlot({
2024-01-11 09:43:23 +01:00
assignment_plot(assign())
})
2024-01-11 09:43:23 +01:00
# Downloadable csv of selected dataset ----
output$downloadData <- downloadHandler(
2024-01-11 09:43:23 +01:00
filename = "group_assignment.csv",
content = function(file) {
write.csv(assign()$export, file, row.names = FALSE)
}
)
2024-01-11 09:43:23 +01:00
}