-
Notifications
You must be signed in to change notification settings - Fork 1
/
IO.R
351 lines (308 loc) · 8.72 KB
/
IO.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#' get the command line arguments
#'
#' @param x one of 'wd, R_env, script_path, script_dir, env_configs'
#'
#' @return list of all arguments, or single value of select argument
#' @export
#'
#' @examples cmdargs()
#'
cmdargs <- function(x = NULL) {
res <- list()
res$wd <- getwd()
res$R_env <- commandArgs() %>% grep("^RStudio|/R$", ., value = TRUE)
res$script_path <- commandArgs() %>%
grep("^--file=", ., value = TRUE) %>%
stringr::str_replace("^--file=", "")
res$script_dir <- res$script_path %>% stringr::str_replace("/[^/]+$", "")
res$env_configs <- list(commandArgs(), res$R_env, res$script_path) %>%
purrr::reduce(setdiff)
if (is.null(x)) {
return(res)
} else {
return(res[[x]])
}
}
#' detect whether directory is empty recursively
#'
#' @param dir the directory
#'
#' @return logical value
#' @export
#'
#' @examples
#' # create an empty directory
#' dir.create("some/deep/path/in/a/folder", recursive = TRUE)
#' empty_dir("some/deep/path/in/a/folder")
#'
#' # create an empty file
#' file.create("some/deep/path/in/a/folder/there_is_a_file.txt")
#' empty_dir("some/deep/path/in/a/folder")
#' empty_file("some/deep/path/in/a/folder/there_is_a_file.txt", strict = TRUE)
#'
#' # create a file with only character of length 0
#' write("", "some/deep/path/in/a/folder/there_is_a_file.txt")
#' empty_file("some/deep/path/in/a/folder/there_is_a_file.txt", strict = TRUE)
#' empty_file("some/deep/path/in/a/folder/there_is_a_file.txt")
#'
#' # clean
#' unlink("some", recursive = TRUE)
#'
empty_dir <- function(dir) {
purrr::map_lgl(dir, ~ length(dir(.x, recursive = TRUE)) == 0)
}
#' detect whether file is empty recursively
#'
#' @param path the path of file
#' @param strict `FALSE` as default. If `TRUE`, a file with only one
#' character of length 0 will be considered as not empty
#'
#' @return logical value
#' @export
#'
#' @examples
#' # create an empty directory
#' dir.create("some/deep/path/in/a/folder", recursive = TRUE)
#' empty_dir("some/deep/path/in/a/folder")
#'
#' # create an empty file
#' file.create("some/deep/path/in/a/folder/there_is_a_file.txt")
#' empty_dir("some/deep/path/in/a/folder")
#' empty_file("some/deep/path/in/a/folder/there_is_a_file.txt", strict = TRUE)
#'
#' # create a file with only character of length 0
#' write("", "some/deep/path/in/a/folder/there_is_a_file.txt")
#' empty_file("some/deep/path/in/a/folder/there_is_a_file.txt", strict = TRUE)
#' empty_file("some/deep/path/in/a/folder/there_is_a_file.txt")
#'
#' # clean
#' unlink("some", recursive = TRUE)
#'
empty_file <- function(path, strict = FALSE) {
if (strict == TRUE) {
file.info(path)$size == 0
} else {
file.info(path)$size <= 1
}
}
#' split a path into ancestor paths recursively
#'
#' @param path path to split
#'
#' @return character vectors of ancestor paths
#' @export
#'
#' @examples split_path("/home/someone/a/test/path.txt")
split_path <- function(path) {
func <- function(vec) {
r <- purrr::accumulate(
vec,
~ stringr::str_c(c(.x, .y), collapse = "/")
)
r[r != ""]
}
res <- stringr::str_split(path, "/") %>% purrr::map(func)
return(res)
}
#' read excel file
#'
#' @param ... arguments of `readxl::read_excel`
#'
#' @return tibble
#' @export
read_excel <- function(...) {
readxl::read_excel(...)
}
#' write a tibble into an excel file
#'
#' @param df tibble or a list of tibbles
#' @param filename the output filename
#' @param sheetname the names of sheets. If not given, will use 'sheet1', or
#' the names of list
#' @param creator creator
#'
#' @return return status
#' @export
#'
#' @examples # write_excel(mini_diamond, "mini_diamond.xlsx")
write_excel <- function(df, filename, sheetname = NULL, creator = "") {
if (!stringr::str_detect(filename, "\\.xlsx*$")) {
stop("File name should have xlsx/xls suffix!")
}
wb <- openxlsx::createWorkbook(creator = creator)
addsheet <- function(wb, name, table) {
openxlsx::addWorksheet(wb, sheetName = name)
openxlsx::writeData(wb, sheet = name, x = table)
}
if (is.data.frame(df) == TRUE) {
if (is.null(sheetname)) {
sheetname <- "sheet1"
}
if (length(sheetname) != 1) {
stop("Different numbers between dataframe and sheet names!")
}
addsheet(wb, sheetname, df)
} else if (is.list(df) == TRUE && is.data.frame(df) == FALSE) {
if (is.null(sheetname) && is.null(names(df))) {
sheetname <- stringr::str_c("sheet", seq_along(df))
} else if (is.null(sheetname) && any(names(df) != "")) {
names(df)[which(names(df) == "")] <- stringr::str_c(
"sheet", which(names(df) == "")
)
sheetname <- names(df)
}
if (length(df) != length(sheetname)) {
stop("Different numbers between dataframe and sheet names!")
}
purrr::walk2(sheetname, df, ~ addsheet(wb, .x, .y))
}
openxlsx::saveWorkbook(wb, filename, overwrite = TRUE)
}
#' read multi-sheet excel file as a list of tibbles
#'
#' @param x path
#'
#' @return list
#' @export
#'
read_excel_list <- function(x) {
sheets <- readxl::excel_sheets(x)
res <- purrr::map(sheets, ~ readxl::read_excel(path = x, sheet = .x))
names(res) <- sheets
return(res)
}
#' read front matter markdown
#'
#' @param x path
#' @param rm_blank_line remove leading and trailing blank lines
#'
#' @return list
#' @export
#'
read_fmmd <- function(x, rm_blank_line = TRUE) {
res <- rmarkdown::yaml_front_matter(x)
content_md <- readr::read_file(x) %>%
str_remove(stringr::regex("^---.+---", dotall = TRUE))
if (rm_blank_line == TRUE) {
content_md <- str_remove(content_md, "^\\n+") %>%
str_replace("\\n*$", "\n")
}
res[["content_md"]] <- content_md
return(res)
}
#' connection parameters to remote server via sftp
#'
#' @param server remote server
#' @param port SSH port, 22 as default
#' @param user username
#' @param password password
#' @param wd workdir
#'
#' @return sftp_connection object
#' @export
#'
#' @examples
#' # sftp_con <- sftp_connect(server='remote_host', port=22,
#' # user='username', password = "password", wd='~')
sftp_connect <- function(server = "localhost", port = 22,
user = NULL, password = NULL, wd = "~") {
structure(
list(
server = server,
port = port,
userpwd = stringr::str_glue("{user}:{password}"),
# if ends with / and not root directory (/), remove the last /
workdir = stringr::str_replace(wd, "([^/]+)/$", "\\1")
),
class = "sftp_connection"
)
}
#' download file from remote server via sftp
#'
#' @param sftp_con sftp_connection created by sftp_connect()
#' @param path remote file path
#' @param to local target path
#'
#' @return NULL
#' @export
#'
#' @examples
#' # sftp_download(sftp_con,
#' # path=c('t1.txt', 't2.txt'),
#' # to=c('path1.txt', 'path2.txt')
sftp_download <- function(sftp_con, path = NULL, to = basename(path)) {
if (!inherits(sftp_con, "sftp_connection")) {
stop("sftp_con must be a sftp_connection object")
}
# absolute path of remote file
absolute_path <- ifelse( # nolint
stringr::str_starts(path, "/|~"),
path,
stringr::str_c(sftp_con$workdir, path, sep = "/")
)
# path of target file
to <- ifelse(
stringr::str_ends(to, "/"),
stringr::str_c(to, basename(path)),
to
)
# download
url <- stringr::str_glue(
"sftp://{sftp_con$server}:{sftp_con$port}/{absolute_path}"
)
handle <- curl::new_handle(userpwd = sftp_con$userpwd)
purrr::walk2(url, to,
function(x, y) {
curl::curl_download(x, y, handle = handle)
cat(stringr::str_glue("finished: {y} <- {x}"), "\n")
},
.progress = TRUE
)
}
#' list files from remote server via sftp
#'
#' @param sftp_con sftp_connection created by sftp_connect()
#' @param path remote directory path
#' @param all list hidden files or not
#'
#' @return files in the dir
#' @export
#'
#' @examples
#'
#' # sftp_ls(sftp_con, 'your/dir')
#'
sftp_ls <- function(sftp_con, path = NULL, all = FALSE) {
if (!inherits(sftp_con, "sftp_connection")) {
stop("sftp_con must be a sftp_connection object")
}
# absolute path of remote dir
absolute_path <- ifelse( # nolint
stringr::str_starts(path, "/|~"),
path,
stringr::str_c(sftp_con$workdir, path, sep = "/")
)
url <- stringr::str_glue(
"sftp://{sftp_con$server}:{sftp_con$port}/{absolute_path}"
)
handle <- curl::new_handle(userpwd = sftp_con$userpwd)
# add / if not at the end
url <- ifelse(
str_detect(url, "/$"), url,
str_c(url, "/")
)
# curl
content <- curl::curl(url, handle = handle)
res <- readLines(content) %>%
str_split(" +") %>%
map_chr(~ .x[9]) %>%
setdiff(c(".", ".."))
is_dir <- readLines(content) %>%
str_split(" +") %>%
map_chr(~ str_sub(.x[1], 1, 1)) == "d"
res <- ifelse(is_dir, str_c(res, "/"), res)
if (all != TRUE) {
res <- str_subset(res, "^[^\\.]")
}
return(res)
}