Nothing
#' Save a `BGF` to an external file
#'
#' Saves a BGF to either a '.csv'- or a '.RDS'-file.
#' It exists one end user function, which internally calls three sub functions, if a '.csv'-file is produced.
#' Otherwise, it acts as a wrapper for [saveRDS].
#'
#' The main function used to save a `BGF` as an external data file is `save_BGF`.
#' The user can choose if the `BGF` should be saved as an '.RDS'-file or in a human readable '.csv'-file.
#' A human readable '.csv'-file is generated by a consecutive call to the three sub-functions `save_ExpParam`, `save_metaData` and `save_BioGasData`.
#' Each of these functions is designed to write a single layer of the input `BGF` to an external '.csv'-file.
#'
#' @param x a `BGF` that should be exported
#' @param opath a `path` pointing to a location on the system, where the `BGF` should be saved
#' @param mkdir `logic`, if `TRUE` a new directory is created as specified in `opath`
#' @param feedback `logic`, if `TRUE` the function will print a feedback to the R console
#' @param append `logic`, if `TRUE` the function assumes that the `path` specified in `opath` already includes a file name with an appropriate ending as specified in `format`. Otherwise a file name will be generated
#' @param format specifies the output generated by the function. Can be either 'csv' or 'RDS'
#' @param ... further arguments that can be passed to [dir.create].
#'
#'@returns Either a '.csv' or '.RDS' file created of a `BGF`
#'
#'@examples
#'# create a BGF
#'myBGF <- BGF(LETTERS[1:5],LETTERS[1],"myBGF",52,2,"manual")
#'
#'
#'# save it as '.csv' file
#'save_BGF(myBGF)
#'
#'# save it as '.RDS' file
#'save_BGF(myBGF,format="RDS")
#'
#'# remove files
#'file.remove("myBGF_BGF_object.csv")
#'file.remove("myBGF_BGF_object.RDS")
#'
#'@export
# save_BGF() ####
save_BGF=function(x,opath=NULL,mkdir=FALSE,feedback=FALSE,append=FALSE,format="csv",...){
if(isFALSE(class(x)=="BGF")){ # check if 'x' is class BGF
stop("'x' must be class 'BGF'!",
call. = FALSE)
}
if(isTRUE(is.null(opath))) opath <- "" # check if 'opath' was specified
if(isTRUE(mkdir)){
if(isFALSE(endsWith(opath,"/"))) opath <- paste0(opath,"/") # check if dir name was provided correctly and adjust if needed
if(isFALSE(dir.exists(opath))) dir.create(opath,...) # check if an output dir should be created; you can't override existing dirs
}
if(format=="csv"){
if(isFALSE(append)) opath <- paste0(opath,x$ExpParam$name,"_BGF_object.csv") # generate final file name
save_ExpParam(x,opath,FALSE,feedback,append = TRUE) # writes 'ExpParam' slot to 'opath'
write(" ",append = TRUE,file = opath,sep="") # add empty line as separator
save_metaData(x,opath,FALSE,feedback,append = TRUE) # writes 'metaData' slot to 'opath'
write(" ",append = TRUE,file = opath,sep="") # add empty line as separator
save_BioGasData(x,opath,FALSE,feedback,append = TRUE) # writes 'BioGasData' slot to 'opath'
# give feedback
if(isTRUE(feedback)){
m1 <- paste0(x$ExpParam$name," was written to file '",opath,"' successfully!")
message(m1)
}
}else if(format=="RDS"){
if(isFALSE(append)) opath <- paste0(opath,x$ExpParam$name,"_BGF_object.RDS") # generate final file name
saveRDS(x,opath)
# give feedback
if(isTRUE(feedback)){
m1<-paste0(x$ExpParam$name," was written to file '",opath,"' successfully!")
message(m1)
}
}else{
m1 <- paste0("Nothing was saved...","\n")
m1 <- paste0(m1,"Supported format's are 'csv' or 'RDS'!")
message(m1)
}
}
#'@rdname save_BGF
#'
#'@examples
#'# save only the 'ExpParam' layer
#'save_ExpParam(myBGF)
#'
#'# remove file
#'file.remove("myBGF_ExpParam.csv")
#'
#' @export
#'
# save_ExpParam() ####
save_ExpParam=function(x,opath=NULL,mkdir=FALSE,feedback=FALSE,append=FALSE,...){
if(isFALSE(class(x)=="BGF")){ # check if 'x' is class BGF
stop("'x' must be class 'BGF'!",
call. = FALSE)
}
if(isTRUE(is.null(opath))) opath <- "" # check if 'opath' was specified
if(isTRUE(mkdir)){
if(isFALSE(endsWith(opath,"/"))) opath <- paste0(opath,"/") # check if dir name was provided correctly and adjust if needed
if(isFALSE(dir.exists(opath))) dir.create(opath,...) # check if an output dir should be created; you can't override existing dirs
}
if(isFALSE(append)) opath <- paste0(opath,x$ExpParam$name,"_ExpParam.csv") # generate final file name
write(paste0("BioGasFermentation:ExpParam,",length(x$ExpParam)),append = append,file = opath,sep="\n") # write format specifier
write(" ",append = TRUE,file = opath,sep="") # add empty line as separator
for(i in c(1:length(x$ExpParam))) write(paste0(names(x$ExpParam)[i],",",x$ExpParam[i]),file = opath,append = TRUE,sep="\n") # export each slot of 'ExpParam' as name,value pair per row
# give feedback
if(isTRUE(feedback)){
m1 <- paste0(x$ExpParam$name,"'s '$ExpParam' was written to file '",opath,"'...")
message(m1)
}
}
#'@rdname save_BGF
#'
#'@examples
#'# save only the 'metaData' layer
#'save_metaData(myBGF)
#'
#'# remove file
#'file.remove("myBGF_metaData.csv")
#'
#' @export
#'
# save_metaData() ####
save_metaData=function(x,opath=NULL,mkdir=FALSE,feedback=FALSE,append=FALSE,...){
if(isFALSE(class(x)=="BGF")){ # check if 'x' is class BGF
stop("'x' must be class 'BGF'!",
call. = FALSE)
}
if(isTRUE(is.null(opath))) opath <- "" # check if 'opath' was specified
if(isTRUE(mkdir)){
if(isFALSE(endsWith(opath,"/"))) opath <- paste0(opath,"/") # check if dir name was provided correctly and adjust if needed
if(isFALSE(dir.exists(opath))) dir.create(opath,...) # check if an output dir should be created; you can't override existing dirs
}
if(isFALSE(append)) opath <- paste0(opath,x$ExpParam$name,"_metaData.csv") # generate final file name
write(paste0("BioGasFermentation:metaData,",nrow(x$metaData),",",length(x$metaData)),append = append,file = opath,sep="\n") # write format specifier
write(" ",append = T,file = opath,sep="") # add empty line as separator
write(paste0("Row,",paste(colnames(x$metaData),collapse = ",")),append = T,file = opath,sep="") # write column names
x$metaData$Layout<-as.character(x$metaData$Layout)
for(i in c(1:nrow(x$metaData))) write(paste0(rownames(x$metaData)[i],",",paste(x$metaData[i,],collapse = ",")),file = opath,append = TRUE,sep="\n") # export each slot of 'ExpParam' as name,value pair per row
# give feedback
if(isTRUE(feedback)){
m1 <-paste0(x$ExpParam$name,"'s '$metaData' was written to file '",opath,"'...")
message(m1)
}
}
#'@rdname save_BGF
#'
#' @examples
#'# save only the 'BioGasData' layer
#'save_BioGasData(myBGF)
#'
#'# remove file
#'file.remove("myBGF_BioGasData.csv")
#'
#' @export
#'
# save_BioGasData() ####
save_BioGasData=function(x,opath=NULL,mkdir=FALSE,feedback=FALSE,append=FALSE,...){
if(isFALSE(class(x)=="BGF")){ # check if 'x' is class BGF
stop("'x' must be class 'BGF'!",
call. = FALSE)
}
if(isTRUE(is.null(opath))) opath <- "" # check if 'opath' was specified
if(isTRUE(mkdir)){
if(isFALSE(endsWith(opath,"/"))) opath <- paste0(opath,"/") # check if dir name was provided correctly and adjust if needed
if(isFALSE(dir.exists(opath))) dir.create(opath,...) # check if an output dir should be created; you can't override existing dirs
}
if(isFALSE(append)) opath <- paste0(opath,x$ExpParam$name,"_BioGasData.csv") # generate final file name
write(paste0("BioGasFermentation:BioGasData,",nrow(x$BioGasData),",",length(x$BioGasData)),append = append,file = opath,sep="\n") # write format specifier
write(" ",append = T,file = opath,sep="") # add empty line as separator
write(paste0("Row,",paste(colnames(x$BioGasData),collapse = ",")),append = TRUE,file = opath,sep="") # write column names
for(i in c(1:nrow(x$BioGasData))) write(paste0(rownames(x$BioGasData)[i],",",paste(x$BioGasData[i,],collapse = ",")),file = opath,append = TRUE,sep="\n") # export each slot of 'ExpParam' as name,value pair per row
# give feedback
if(isTRUE(feedback)){
m1 <- paste0(x$ExpParam$name,"'s '$BioGasData' was written to file '",opath,"'...")
message(m1)
}
}
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.