R/susor_export_file.R

Defines functions susor_export_file

Documented in susor_export_file

#' Starts the export process and downloads files from Survey Solution's server.
#'
#' As for any other function of \code{susor}, \code{susor_export_file()} requires
#' that you first define your server credentials with \code{susor_login()}.For
#' \code{susor_export_file()} it is required that you define \code{susor_dir_downloads}
#' in \code{susor_login()}. \code{susor_dir_download} is the directory
#' where the file will be unzipped and saved. Pass the questionnaire variable
#' \code{susor_qn_variable}, and the version (\code{susor_qn_version}) to download
#' \code{susor_export_file()} will generate and download
#' that version of the questionnaire.
#' @return Unzipped file saved in \code{susor_dir_downloads}.
#' @param  susor_qn_variable A string. The variable name of questionnaire (you can
#' find it in the tibble \code{susor_questionnaires} that is created by
#' \code{susor_login()}).
#' @param susor_qn_version A number. Version number of the questionnaire
#' to download (it can be found in the tibble \code{susor_questionnaires}
#' that is created by  \code{susor_login()}).
#' @param susor_format A string to define the format of the file c("STATA", "Tabular").
#' Default = "STATA"
#' @param susor_interview_status A string. To define the status of interviews to download
#' c( "All", "SupervisorAssigned", "InterviewerAssigned", "Completed", "RejectedBySupervisor",
#'  "ApprovedBySupervisor", "RejectedByHeadquarters", "ApprovedByHeadquarters").
#'  Default = "ALL"
#' @param  temporary_dir A temporary directory where the .zip format from the
#' API export response is temporally stored and unzipped from. Default = tempdir().
#' @examples
#' susor_exportfile(susor_qn_variable = "pupils_SLK"
#' susor_qn_version = 5)



susor_export_file = function(susor_qn_variable,
                             susor_qn_version,
                             susor_format = "STATA",
                             susor_interview_status = "All",
                             temporary_dir = tempdir(),
                             #dir_ss_downloads = tempdir(),
                             ...){

  #check that susor_login() has been ran
  check_credentials()



  # get questionnaire ID (susor_questionnaires is generated by susor_login)
  quid <- susor_questionnaires %>% dplyr::filter(Variable == susor_qn_variable & Version == susor_qn_version) %>%
    pull(QuestionnaireIdentity)


  #generate file in the server ---------------------------------------------------
  post_export <- susor_generate_file(
    susor_quid = quid,
    susor_format = susor_format
  )

  #get job ID and the query from post to generate file
  JobID <- post_export$jobID
  apiGenerate <- post_export$apiGenerate

  message(paste("JobID:",JobID))


  #status of the export process
  file_status <- susor_get_status_creation(apiGenerate, JobID)

  #wait until the export process  is Completed so we can start exporting
  while (file_status != "Completed") {
    Sys.sleep(1)
    message(paste("Creating", susor_qn_variable, "version:", susor_qn_version, "in Server"))
    file_status <- susor_get_status_creation(apiGenerate, JobID)

  }

  message("File creation has been completed in the server! Starting download!")

  #start exporting the file -------------------------------------------------

  #Define query to export the file
  apiExport <- sprintf("%s/api/v2/export/%s/file", susor_server, JobID)


  #export file
  response_export <- GET(apiExport, authenticate(susor_user, susor_password))

  #check response from the server
  status <- response_export$status_code
  check_response(status)


  #Unzip and save in download directory (this directory must be defined in susor_login) ------------------------

  # (this is the name of the file)
  unzip_name <- paste0(susor_qn_variable,"_", susor_qn_version)
  zip_name <- paste0(unzip_name,".zip")

  zipfile <- file.path(temporary_dir,zip_name)

  # create directory where .zip is going to be extracted
  outputdir <- file.path(susor_dir_downloads, unzip_name )

  if (!file.exists(outputdir)){

    dir.create(outputdir)

  }

  #open connection to write data in temporary directory
  filecon <- file(zipfile, "wb")
  #write data contents to download file
  writeBin(response_export$content, filecon)
  #close the connection
  close(filecon)


  ## unzip data into the export directory
  unzip(zipfile=zipfile, overwrite = TRUE,
        exdir = outputdir,
        unzip = "internal"
  )


  #message to the user :)
  message(paste(susor_qn_variable, susor_qn_version, "has been exported to:", outputdir))



}
araupontones/susor documentation built on May 12, 2022, 1:31 p.m.