Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add write_chronicle_df #21

Open
b-rodrigues opened this issue Feb 14, 2024 · 0 comments
Open

Add write_chronicle_df #21

b-rodrigues opened this issue Feb 14, 2024 · 0 comments

Comments

@b-rodrigues
Copy link
Owner

 write_chronicle <- function(.c, path, row.names = FALSE, sep = ",", ...){
                                                                          
  stopifnot("Only provide one path" = {length(path) == 1})                
                                                                          
  value <- chronicler::pick(.c, "value")                                  
                                                                          
  stopifnot("Value must be of class data.frame!" = is.data.frame(value))  
                                                                          
  ext <- stringr::str_extract(path,                                       
                              "\\.([0-9a-z]+)(?=[?#])|(\\.)(?:[\\w]+)$")  
                                                                          
  stopifnot("write_chronicle() can only save data as either .csv or .xlsx. Change the extension of the output." = (any(c(".csv", ".xlsx") %in% ext)))
                                 
  log <- chronicler::read_log(.c)
                                 
                                 
  if(ext == ".csv"){             
                                 
    logcsv <- c(paste0("This first ", length(log) + 2, " lines of this .csv file constitute a log."),
             paste0("Skip the first ", length(log) + 2, " lines to read in the data."),    
             log)                                                                          
                                                                                           
    write(logcsv, file = path)                                                             
    suppressWarnings(                                                                      
      write.table(value, file = path, sep = sep, append = TRUE, row.names = row.names, ...)
    )                                                                                      
                                                                                           
  } else {                                                                                 
                                                                                           
    logxlsx <- c("This sheet contains a log of the operations used to create the dataset in the 'value' sheet.",
             log)                                                                              
                                                                                               
    xlsx_output <- list("value" = value,                                                       
                        "log" = logxlsx)                                                       
                                                                                               
    openxlsx::write.xlsx(xlsx_output, file = path)                                             
                                                                                               
  }                                                                                            
                                                                                               
}   
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant