2024-01-11 13:42:03 +01:00
|
|
|
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"))
|
2024-01-11 13:42:03 +01:00
|
|
|
|
|
|
|
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 13:42:03 +01:00
|
|
|
|
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)
|
2024-01-11 13:42:03 +01:00
|
|
|
|
|
|
|
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)
|
|
|
|
})
|
2024-01-11 13:42:03 +01:00
|
|
|
|
|
|
|
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 13:42:03 +01:00
|
|
|
|
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 13:42:03 +01:00
|
|
|
|
2024-01-11 09:43:23 +01:00
|
|
|
} else {
|
|
|
|
stop("Input file format has to be either '.csv', '.xls' or '.xlsx'")
|
|
|
|
}
|
2024-01-11 13:42:03 +01:00
|
|
|
|
2024-01-11 09:43:23 +01:00
|
|
|
return(df)
|
|
|
|
} else {
|
|
|
|
return(NULL)
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
2024-01-11 13:42:03 +01:00
|
|
|
|
2024-01-11 09:43:23 +01:00
|
|
|
assign <-
|
2024-01-11 13:42:03 +01:00
|
|
|
reactive({
|
2024-01-11 09:43:23 +01:00
|
|
|
assigned <- group_assignment(
|
|
|
|
ds = dat(),
|
|
|
|
excess_space = input$ecxess,
|
|
|
|
pre_assign = dat_pre()
|
|
|
|
)
|
|
|
|
return(assigned)
|
|
|
|
})
|
2024-01-11 13:42:03 +01:00
|
|
|
|
|
|
|
|
|
|
|
output$raw.data.tbl <- renderTable({
|
2024-01-11 09:43:23 +01:00
|
|
|
assign()$export
|
|
|
|
})
|
2024-01-11 13:42:03 +01:00
|
|
|
|
|
|
|
output$pre.assign <- renderTable({
|
2024-01-11 09:43:23 +01:00
|
|
|
dat_pre()
|
|
|
|
})
|
2024-01-11 13:42:03 +01:00
|
|
|
|
|
|
|
output$input <- renderTable({
|
2024-01-11 09:43:23 +01:00
|
|
|
dat()
|
|
|
|
})
|
2024-01-11 13:42:03 +01:00
|
|
|
|
|
|
|
output$assign.plt <- renderPlot({
|
2024-01-11 09:43:23 +01:00
|
|
|
assignment_plot(assign())
|
|
|
|
})
|
2024-01-11 13:42:03 +01:00
|
|
|
|
2024-01-11 09:43:23 +01:00
|
|
|
# Downloadable csv of selected dataset ----
|
2024-01-11 13:42:03 +01:00
|
|
|
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 13:42:03 +01:00
|
|
|
|
2024-01-11 09:43:23 +01:00
|
|
|
}
|