R/backups.R

Defines functions Write_Surveys_to_Files_XLSX Write_Surveys_to_Backup_Files_XLSX Write_Surveys_to_Backup_Files

Documented in Write_Surveys_to_Backup_Files Write_Surveys_to_Backup_Files_XLSX Write_Surveys_to_Files_XLSX

#' Saves each survey part individually as a csv file.
#'
#' @param Survey_List this is a list containing the downloaded (or filtered) data from the survey for a multiple-part survey.
#' @param Backup_Folder string specifying where it should be saved. If the folder doesn't exist yet, it will be created. The default is a "Backups" folder
#' @param Survey_IDs string vector with the survey IDs
#' @param Provider string with the names of the panel provider
#' @return
#' @keywords Saving csv
#' @export
#' @examples
#' Write_Surveys_to_Backup_Files(Cint_Surveys_0, "Backups", c(12031:12039), "Cint")
Write_Surveys_to_Backup_Files <- function(Survey_List, Backup_Folder = "Backups", Survey_IDs, Provider){
  # Function to save the survey components as individual csvs:
  # They're saved in dated folders
  # These are the "backups"
  Survey_IDs <- Survey_IDs

  if(!file.exists(Backup_Folder)){dir.create(Backup_Folder)}

  Folder <- paste0(Backup_Folder, "/", format(Sys.time(), "%Y%m%d_%H%M%S"), "_", Provider)

  dir.create(Folder)

  for(i in c(1:length(Survey_List))){
    File <- paste0(Folder,"/",i,"_",Survey_IDs[i],".csv")
    fwrite(Survey_List[[i]], File, append = FALSE, sep = ";")
  } #end for loop
} #end Write_Surveys_to_Backup_Files function




#' XLSX Backup of Limesurvey Surveys
#'
#' Writes a list of surveys to xlsx backup files, using the limesurvey IDs and panel providers as names
#' @param Survey_List this is a list containing only the downloaded data from the survey for a multiple-part survey
#' @param Backup_Folder string specifying where it should be saved. If the folder doesn't exist yet, it will be created. The default is a "Backups" folder
#' @param Survey_IDs this is a vector with the Limesurvey IDs of the survey parts in the same order as they are given in the list
#' @param Provider Just a string with the name of the panel provider, i.e. "Cint"
#' @keywords Backup
#' @export
#' @examples
#' Write_Surveys_to_Backup_Files_XLSX(Cint_Surveys_0, c(112030:112039), "Cint")

Write_Surveys_to_Backup_Files_XLSX <- function(Survey_List, Backup_Folder = "Backups", Survey_IDs, Provider){
  # Function to save the survey components as individual csvs:
  # They're saved in dated folders
  # These are the "backups"
  Survey_IDs <- Survey_IDs

  if(!file.exists(Backup_Folder)){dir.create(Backup_Folder)}

  Folder <- paste0(Backup_Folder, "/", format(Sys.time(), "%Y%m%d_%H%M%S"), "_", Provider)

  dir.create(Folder)

  for(i in c(1:length(Survey_List))){
    File <- paste0(Folder,"/",i,"_",Survey_IDs[i],"_", Provider,".xlsx")
    openxlsx::write.xlsx(Survey_List[[i]], File)
  } #end for loop
} #end Write_Surveys_to_Backup_Files_XLSX function





#' Write Limesurvey Surveys to XLSX Files <Automated Workflow>
#'
#' Writes a list of surveys to xlsx backup files, using the limesurvey IDs and panel providers as names
#' @param Survey_List this is a list containing only the downloaded data from the survey for a multiple-part survey
#' @param Top_Folder string specifying where it should be saved. If the folder doesn't exist yet, it will be created. The default is a "Backups" folder
#' @param Sub_Folder Default value is NA- which creates no subfolders. Just a string with the name of the subfolders, where the individual files will be saved. This could allow saving organized by panel provider, i.e. "Cint"
#' @param Use_Date_Sub_Folder Default value is TRUE, which means that the Sub_Folder is created with a date and time prefix. FALSE inhibits this behavior, and will create a folder where the contents can be overwritten. TRUE is useful for backup purposes, FALSE when one folder should be updated with the most current information on a regular basis because it is referenced by another application
#' @param Use_Date_File Default value is FALSE, which means that the files do not have the date and time as a prefix to their names
#' @keywords Backup
#' @export
#' @examples
#' Write_Surveys_to_Files_XLSX(Cint_Surveys_0, c(112030:112039), "Cint")

Write_Surveys_to_Files_XLSX <- function(Survey_List, Top_Folder = "Backups", Sub_Folder = NA, Use_Date_Sub_Folder = TRUE, Use_Date_File = FALSE){
  # Function to save the survey components as individual xlsx:
  # They're saved in dated folders
  # These are the "backups"
  Date_Time <- paste0(format(Sys.time(), "%Y%m%d_%H%M%S"), "_")


  # If the user wants subfolder date time stamps
  if(Use_Date_Sub_Folder == TRUE){
    Sub_Folder_Date_Time <- Date_Time
  }
  else{
    Sub_Folder_Date_Time <- ""
  }

  # If the user wants file date time stamps
  if(Use_Date_File == TRUE){
    File_Date_Time <- Date_Time
  }
  else{
    File_Date_Time <- ""
  }


  if(!file.exists(Top_Folder)){dir.create(Top_Folder)}


  # If the user wants a subfolder structure:
if(!is.na(Sub_Folder)){
    Folder <- paste0(Top_Folder, "/", Sub_Folder_Date_Time, Sub_Folder)
    # Create the folder if it doesn't exist
    if(!file.exists(Folder)){dir.create(Folder)}


  for(i in c(1:length(Survey_List))){

    Name_of_Survey <- Survey_List[[i]][1, c("surveyName")]
    ID_of_Survey <- Survey_List[[i]][1, c("surveyId")]

    File <- paste0(Folder, "/", File_Date_Time, i, "_", paste0(ID_of_Survey), "_", paste0(Name_of_Survey), ".xlsx")

    openxlsx::write.xlsx(Survey_List[[i]], File, overwrite = TRUE)
  } #end for loop

} # end if - covers the subfolder case
else{
  for(i in c(1:length(Survey_List))){

    Name_of_Survey <- Survey_List[[i]][1, c("surveyName")]
    ID_of_Survey <- Survey_List[[i]][1, c("surveyId")]

    File <- paste0(Top_Folder, "/", File_Date_Time, i, "_", paste0(ID_of_Survey), "_", paste0(Name_of_Survey), ".xlsx")

    openxlsx::write.xlsx(Survey_List[[i]], File, overwrite = TRUE)
  } #end for loop
} # end else - covers the no subfolder case

} #end Write_Surveys_to_Files_XLSX function
bpresentati/surveyR documentation built on March 19, 2022, 3:40 a.m.