From e8ea68266fe4cb51eefdd2a17eeff771714500e8 Mon Sep 17 00:00:00 2001 From: Dongdong Kong Date: Mon, 10 Apr 2023 11:13:49 +0800 Subject: [PATCH] add rio support csv, fst, rda, rds --- DESCRIPTION | 5 ++--- NAMESPACE | 7 +++++++ NEWS.md | 2 ++ R/cmd_func.R | 4 +++- R/reexports.R | 34 +++++++++++++++++++--------------- R/rio.R | 38 ++++++++++++++++++++++++++++++++++++++ man/reexports.Rd | 5 +++-- vignettes/parallel.Rmd | 36 ++++++++++++++++++++++++++++++++++++ 8 files changed, 110 insertions(+), 21 deletions(-) create mode 100644 R/rio.R create mode 100644 vignettes/parallel.Rmd diff --git a/DESCRIPTION b/DESCRIPTION index 68e91d3..db88223 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -16,7 +16,7 @@ Imports: magrittr, purrr, progress, lubridate, - data.table, + data.table, fst, openxlsx, readxl, graphics, @@ -24,8 +24,7 @@ Imports: parallel, doParallel, foreach, iterators, - plyr, - dplyr, + plyr, dplyr, clipr, glue, matrixStats, rstudioapi, diff --git a/NAMESPACE b/NAMESPACE index cbbb1c4..1dc72dc 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -9,6 +9,7 @@ export("%<>%") export("%>%") export("%do%") export("%dopar%") +export("%||%") export(.) export(InitCluster) export(OS_type) @@ -63,6 +64,7 @@ export(edit_r_environ) export(edit_r_profile) export(edit_r_profile_sys) export(evince) +export(export) export(file_ext) export(file_mv) export(file_name) @@ -88,6 +90,7 @@ export(group_apply) export(guess_names) export(icount) export(ifelse2) +export(import) export(install_git) export(install_gitee) export(install_github) @@ -220,6 +223,8 @@ importFrom(dplyr,tribble) importFrom(foreach,"%do%") importFrom(foreach,"%dopar%") importFrom(foreach,foreach) +importFrom(fst,read_fst) +importFrom(fst,write_fst) importFrom(grDevices,cairo_pdf) importFrom(grDevices,col2rgb) importFrom(grDevices,colorRampPalette) @@ -270,8 +275,10 @@ importFrom(plyr,progress_none) importFrom(plyr,quickdf) importFrom(plyr,rbind.fill.matrix) importFrom(plyr,revalue) +importFrom(purrr,`%||%`) importFrom(purrr,as_mapper) importFrom(purrr,map) +importFrom(purrr,map_depth) importFrom(purrr,transpose) importFrom(remotes,install_git) importFrom(remotes,install_github) diff --git a/NEWS.md b/NEWS.md index 2e40367..d29ee9c 100644 --- a/NEWS.md +++ b/NEWS.md @@ -3,6 +3,8 @@ - fix `cdo_grid` corner coordinate error. - set `kill=FALSE` as default in `InitCluster` +- Please manually add `C:/Program Files/RStudio/resources/app/bin/sumatra/SumatraPDF.exe` to your system path +- add rio: support csv, fst, rda, rds # Ipaper 0.1.7 diff --git a/R/cmd_func.R b/R/cmd_func.R index e85dca6..cfa5949 100644 --- a/R/cmd_func.R +++ b/R/cmd_func.R @@ -93,7 +93,9 @@ pdf_view <- function(file, ...) { #' @rdname pdf_view #' @export -SumatraPDF <- cmd_func("C:/Program Files/RStudio/resources/app/bin/sumatra/SumatraPDF.exe") +SumatraPDF <- cmd_func("SumatraPDF.exe") + +# C:/Program Files/RStudio/resources/app/bin/sumatra/SumatraPDF.exe #' @rdname pdf_view #' @export diff --git a/R/reexports.R b/R/reexports.R index 058f4d6..2d46c50 100644 --- a/R/reexports.R +++ b/R/reexports.R @@ -1,6 +1,25 @@ #' @export transpose <- purrr::transpose +#' @importFrom purrr map map_depth `%||%` +#' @export +purrr::`%||%` + +#' @export +purrr::map +# #' @importFrom progress progress_bar +# map <- function(.x, .f, ...) { +# n = length(.x) +# pb <- progress_bar$new(total = n) +# .f <- as_mapper(.f, ...) +# .f2 = function(..., .x = ..1, .y = ..2, . = ..1) { +# pb$tick() +# .f(..., .x, .y, .) +# } +# # browser() +# .Call(purrr:::map_impl, environment(), ".x", ".f2", "list") +# } + #' @importFrom plyr mapvalues revalue #' @export plyr::mapvalues @@ -14,7 +33,6 @@ plyr::revalue # #' @export # dplyr::last - first <- function(x) { x[1] } @@ -57,20 +75,6 @@ magrittr::`%<>%` #' @export magrittr::set_names -#' @export -purrr::map -# #' @importFrom progress progress_bar -# map <- function(.x, .f, ...) { -# n = length(.x) -# pb <- progress_bar$new(total = n) -# .f <- as_mapper(.f, ...) -# .f2 = function(..., .x = ..1, .y = ..2, . = ..1) { -# pb$tick() -# .f(..., .x, .y, .) -# } -# # browser() -# .Call(purrr:::map_impl, environment(), ".x", ".f2", "list") -# } #' @importFrom data.table data.table is.data.table as.data.table fread fwrite #' @export diff --git a/R/rio.R b/R/rio.R new file mode 100644 index 0000000..35c51e5 --- /dev/null +++ b/R/rio.R @@ -0,0 +1,38 @@ +#' @importFrom fst write_fst read_fst +export_fst <- function(x, path, compress = 100, uniform_encoding = TRUE) { + write_fst(x, path, compress, uniform_encoding) +} + +import_fst <- function( + path, columns = NULL, from = 1, to = NULL, + as.data.table = TRUE, old_format = FALSE) { + read_fst(path, columns, from, to, as.data.table, old_format) +} + +#' @export +export <- function(x, path) { + ext <- tools::file_ext(path) %>% tolower() + if (ext == "rda") { + save(x, file = path, ...) + } else if (ext == "rds") { + saveRDS(x, path, ...) + } else if (ext == "fst") { + write_fst(x, path, ...) + } else if (ext == "csv") { + fwrite(x, path, ...) + } +} + +#' @export +import <- function(path, ...) { + ext <- tools::file_ext(path) %>% tolower() + if (ext == "rda") { + load(path) + } else if (ext == "rds") { + readRDS(path) + } else if (ext == "fst") { + import_fst(path, ...) + } else if (ext == "csv") { + fread(x, path, ...) + } +} diff --git a/man/reexports.Rd b/man/reexports.Rd index b6e15f1..01e5a63 100644 --- a/man/reexports.Rd +++ b/man/reexports.Rd @@ -9,6 +9,8 @@ \alias{green} \alias{underline} \alias{.} +\alias{\%||\%} +\alias{map} \alias{mapvalues} \alias{revalue} \alias{select} @@ -22,7 +24,6 @@ \alias{\%>\%} \alias{\%<>\%} \alias{set_names} -\alias{map} \alias{data.table} \alias{is.data.table} \alias{fread} @@ -76,7 +77,7 @@ below to see their documentation. \item{plyr}{\code{\link[plyr:quoted]{.}}, \code{\link[plyr]{mapvalues}}, \code{\link[plyr]{revalue}}} - \item{purrr}{\code{\link[purrr]{map}}} + \item{purrr}{\code{\link[purrr:reexports]{\%||\%}}, \code{\link[purrr]{map}}} \item{remotes}{\code{\link[remotes]{install_git}}, \code{\link[remotes]{install_github}}, \code{\link[remotes]{install_gitlab}}} diff --git a/vignettes/parallel.Rmd b/vignettes/parallel.Rmd new file mode 100644 index 0000000..ef5160f --- /dev/null +++ b/vignettes/parallel.Rmd @@ -0,0 +1,36 @@ +--- +title: "parallel" +output: rmarkdown::html_vignette +vignette: > + %\VignetteIndexEntry{parallel} + %\VignetteEngine{knitr::rmarkdown} + %\VignetteEncoding{UTF-8} +--- + +```{r, include = FALSE} +knitr::opts_chunk$set( + collapse = TRUE, + comment = "#>" +) +``` + +```{r setup, eval=FALSE} +library(Ipaper) + +InitCluster(4) +cl = getOption("cl") +``` + +```{r, eval=FALSE} +library(parallel) + +# 加载共享函数 +clusterEvalQ(cl, { + library(Ipaper) +}) +# parallel::clusterExport(cl, ) + +foreach(i = icount(10)) %dopar% { + runningId(i) +} +```