#' @title Write dataframe as standart DTwin csv file or rds, fst, xlsx
#' @description write_stn_csv expand typical write.table function by standart attributes - quote, row.names, col.names, sep, dec, fileEncoding
#' @usage write_stn_csv (data, file, dec = ".", type = "csv")
#' @param data the object to be written, preferably a matrix or data frame.
#' @param file Path in string for saving csv
#' @param dec decimal separator, "." by default
#' @param type "csv" (by default), "rds", "xls" or "fst" format saving
#' @return file in file system
#' @importFrom utils write.table
#' @importFrom fst write.fst
#' @importFrom openxlsx write.xlsx
#' @export
#' @examples
#' write_stn_csv(swiss, file = paste0(getwd(), "/swiss.csv"))
#' write_stn_csv(swiss, file = paste0(getwd(), "/swiss.csv"), dec = ",")
write_stn_csv <- function (data, file, dec = ".", type = "csv"){
if(type == "csv") write.table(data, file, quote = FALSE, row.names = FALSE, col.names = TRUE, sep = ";", dec = dec, fileEncoding = "UTF-8")
if(type == "rds") saveRDS(data, file)
if(type == "fst") fst::write.fst(data, file)
if(type == "xls") openxlsx::write.xlsx (data, file)
print(paste0("Сохранено. ", file))
}
#' @title Read the last version csv, excel, fst or rds dataset in the folder
#' @description read_stn_csv help to pick up the last version of the file in the directory by pattern in the file name
#' @usage read_stn_csv (path = "1. data/", pattern = "DT.0575.SystemDyn.", sep = ";", type = "csv", header = T)
#' @param path Filder in string for reading dataset
#' @param pattern the pattern string in filename
#' @param sep separator in dataset
#' @param type file extension either xls (xlsx), csv, fst or rsd
#' @param header boolean. is header needed when csv. TRUE by default
#' @param sheet name of excel sheet if needed. NULL by default
#' @param range cells range for excel sheet if needed. NULL by default
#' @return tibble
#' @importFrom readr read_delim
#' @importFrom readxl read_excel
#' @importFrom utils tail
#' @importFrom dplyr %>%
#' @importFrom stringr str_subset
#' @importFrom fst read.fst
#' @export
#' @examples
#' read_stn_csv (path = "1. data/", pattern = "DT.0722.BB.City", type = "rds")
#' read_stn_csv (path = "1. data/", pattern = "DT.0722.BB.City", type = "xls", sheet = "1", range = "B2:G14")
#' read_stn_csv (path = "1. data/", pattern = "DT.0575.SystemDyn.")
read_stn_csv <- function ( # Чтение последней версии сохраненного набора данных с паттерном в названии файла
path = "1. data/", # Внутренний путь до папким
pattern = "DT.0575.SystemDyn.*(in)*", # Код DT или любой текстовый паттерн, содержащийся в имени файла который надо прочитать
sep = ";",
type = "csv", #Расширение файла которые надо читать, возможно rds или csv
header = TRUE ,
sheet = NULL , # Название вкладки в экселе
range = NULL # Ячейки в экселе
)
{
if (type == "xls") ds <- readxl::read_excel(paste0(path, str_subset(list.files(path), pattern = pattern) %>% tail(1)), sheet = sheet, range = range) %>% as_tibble()
if (type == "csv") ds <- readr::read_delim(paste0(path, str_subset(list.files(path), pattern = pattern) %>% tail(1)), delim = sep) %>% as_tibble()
if (type == "rds") ds <- readRDS (paste0(path, str_subset(list.files(path), pattern = pattern) %>% tail(1)) ) %>% as_tibble()
if (type == "fst") ds <- fst::read.fst(paste0(path, str_subset(list.files(path), pattern = pattern) %>% tail(1)) ) %>% as_tibble()
return(ds)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.