R/download_excel.R

Defines functions download_excel create_template replace_with_na

Documented in create_template download_excel replace_with_na

#' Replace with NA
#' @param x any input
#' @description This is a helper function for `create_template`. It is used to replace cell values with NA
#' @return NA
replace_with_na <- function(x) {
  return(NA)
}

#' Create template
#' @param data data with Deltager, Uge and questions
#' 
create_template <- function(data) {
  
  var_overwrite <- paste0("Spørgsmål_", 1:17)
  
  data %>% 
    dplyr::mutate(dplyr::across(.cols = var_overwrite, .fns = replace_with_na))
  
}

#' Download excel
#' @param data data to download
#' @param file_name file name including extension. Should be .xlsx
#' @return Downloaded file
#' @export
#' 
download_excel <- function(data, file_name) {

  shiny::downloadHandler(
    filename = file_name,
    content = function(file) {
      xlsx::write.xlsx(
        x = as.data.frame(data), ## It's important to convert x to data.frame to prevent bug
        file = file, 
        sheetName = "data", 
        row.names = FALSE, 
        showNA = FALSE
      )
    }
  )
}
kristian-bak/rumination documentation built on Oct. 31, 2022, 6:44 p.m.