-
Notifications
You must be signed in to change notification settings - Fork 0
/
import_txt.R
61 lines (54 loc) · 1.47 KB
/
import_txt.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
# import_txt ----------------------------------------------
#' Import coordinates from .txt files
#'
#' Wraps `read.table`. Typically useful for raw text file, such as those generated
#' by the popular "ImageJ's > Export as xy coordinates".
#'
#' @param x paths to `.txt` files
#' @param from_col,to_col column names where to get paths and where to store results
#' @param ... additional arguments to feed [utils::read.table]
#'
#' @return [Momocs2::coo_single]
#' @family import
#' @examples
#' # list of files case
#' lf <- Momit_data("txt") %>% list_files()
#' lf
#' # single case
#' lf[1] %>% import_txt()
#' # multi case
#' lf %>% import_txt()
#'
#' # mom_tbl case
#' z <- Momit_data("txt") %>% sniff()
#' z %>% import_txt()
#'
#' # case with different from/to_col names
#' z %>%
#' dplyr::rename(plop=path) %>%
#' import_txt(from_col=plop, to_col=plip)
#' @export
import_txt <- function(x, from_col, to_col, ...){
UseMethod("import_txt")
}
#' @export
import_txt.default <- function(x, ...){
not_defined("import_txt")
}
#' @export
import_txt.character <- function(x, ...){
# paths case
if (length(x)>1)
return(purrr::map(x, import_txt, ...))
# single path
x %>%
utils::read.table(...) %>%
Momocs2::coo_single()
}
#' @export
import_txt.mom_tbl <- function(x, from_col=path, to_col=coo, ...){
from_col <- enquo(from_col)
to_col <- enquo(to_col)
res <- x %>% dplyr::pull(!!from_col) %>% import_txt(...) %>% Momocs2::coo_list()
dplyr::mutate(x, !!to_col := res)
}