Skip to content

Commit 0b0743a

Browse files
committed
Add write(), data_types() and comment to "read_write" options
1 parent 7ce085c commit 0b0743a

14 files changed

Lines changed: 387 additions & 70 deletions

DESCRIPTION

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
Package: data.io
22
Type: Package
3-
Version: 1.0.1
3+
Version: 1.1.0
44
Title: Data Input/Output, Read or Write Data from Files or Datasets in R Packages in Different Formats
5-
Description: Read or write data from many different formats (tabular datasets, images,
6-
...) into R objects. Add labels and units in different languages.
5+
Description: Read or write data from many different formats (tabular datasets,
6+
from statistic software, ...) into R objects. Add labels and units in
7+
different languages.
78
Authors@R: c(person("Philippe", "Grosjean", role = c("aut", "cre"),
89
email = "phgrosjean@sciviews.org"))
910
Maintainer: Philippe Grosjean <phgrosjean@sciviews.org>
1011
Depends: R (>= 3.3.0)
1112
Imports: tibble, tsibble, Hmisc, utils, readr, rlang, datasets, ggplot2, nycflights13
12-
Suggests: SciViews, readxl, haven, WriteXLS, openxlsx, covr, knitr, testthat
13+
Suggests: SciViews, readxl, haven, WriteXLS, writexl, covr, knitr, testthat
1314
License: GPL-2
1415
Encoding: UTF-8
1516
LazyData: true

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export(as.dataframe)
1616
export(as_dataframe)
1717
export(cl)
1818
export(data_example)
19+
export(data_types)
1920
export(hread_text)
2021
export(hread_xls)
2122
export(hread_xlsx)
@@ -30,6 +31,7 @@ export(relative_path)
3031
export(type_from_extension)
3132
export(unlabelise)
3233
export(unlabelize)
34+
export(write)
3335
importFrom(Hmisc,"label<-")
3436
importFrom(Hmisc,label)
3537
importFrom(readr,default_locale)

NEWS.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
11
# data.io News
22

3+
## Changes in data.io 1.1.0
4+
5+
- A basic version of write() is now available.
6+
7+
- data_types() function added to easily get information about data types that
8+
can be read() or write()
9+
10+
- Description added into "read_write" options.
11+
12+
313
## Changes in data.io 1.0.1
414

5-
- bug corrected: forgot to change 'data' -> 'data.io' in 'read_write' options.
15+
- Bug corrected: forgot to change 'data' -> 'data.io' in 'read_write' options.
616

717

818
## Changes in data.io 1.0.0

R/data_types.R

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#' List recognized file formats (types) for read() and write()
2+
#'
3+
#' @param types_only If `TRUE`, only a vector of types is returned, otherwise,
4+
#' a `tibble` with fll specifications is provided.
5+
#' @param view If `TRUE`, the result is "viewed" (displayed in a table in a
6+
#' separate window, if the user interface allows it, e.g., in RStudio) and
7+
#' returned invisibly. Otherwise, the results are returned normally.
8+
#'
9+
#' @description Dispaly information about data types that can read() and write()
10+
#' can use, as well as, the original functions that are delegated (see they
11+
#' respective help pages for more info and to know which additional parameters
12+
#' can be used in read() and write()).
13+
#'
14+
#' @return An `tibble` with `types_only = FALSE`, or a character vector.
15+
#' @details The function is mainly designed to be used interactively and to
16+
#' provide information about file types that can be read() or write(). This
17+
#' cannot be done through a man page because this list is dynamic and other
18+
#' packages could add or change entries there. With `view = FALSE`, the function
19+
#' can, nevertheless, be also used in a script or a R Markdown/Notebook
20+
#' document.
21+
#' @author Philippe Grosjean <phgrosjean@sciviews.org>
22+
#' @export
23+
#' @seealso [read()], [write()]
24+
#' @keywords utilities
25+
#' @concept list file types that can be read or write
26+
#' @examples
27+
#' \dontrun{
28+
#' data_types()
29+
#' data_types(TRUE)
30+
#' }
31+
#' # For non-interactive use, specify view = FALSE
32+
#' data_types(view = FALSE)
33+
#' data_types(TRUE, view = FALSE)
34+
data_types <- function(types_only = FALSE, view = TRUE) {
35+
`data types` <- getOption("read_write")
36+
# If not installed yet, do it now!
37+
if (is.null(`data types`))
38+
`data types` <- read_write_option()
39+
40+
if (isTRUE(types_only))
41+
`data types` <- `data types`$type
42+
43+
if (isTRUE(view)) {
44+
# We don't necessarily want to use utils::View(). For instance, RStudio
45+
# defines another version of that function, and we ant to use it instead!
46+
view <- get0("View", mode = "function")
47+
if (is.null(view)) {
48+
warning("'View' function not found, return the data instead")
49+
`data types`
50+
} else {
51+
view(`data types`)
52+
invisible(`data types`)
53+
}
54+
} else `data types`
55+
}
56+

R/internal.R

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
11
.onLoad <- function(libname, pkgname) {
22
read_write_option()
33
}
4+
5+
.get_function <- function(fun) {
6+
# In case we have ns::fun
7+
fun <- strsplit(fun, "::", fixed = TRUE)[[1L]]
8+
if (length(fun) == 2L) {
9+
res <- try(getExportedValue(fun[1L], fun[2L]), silent = TRUE)
10+
if (inherits(res, "try-error"))
11+
stop("You need function '", fun[2L], "' from package '", fun[1L],
12+
"' to read these data. Please, install the package first",
13+
" and make sure the function is available there.")
14+
} else {
15+
if (is.na(fun[1L]))
16+
return(NA)
17+
res <- get0(fun[1L], envir = parent.frame(), mode = "function",
18+
inherits = TRUE)
19+
if (is.null(res))
20+
stop("function '", fun[1], "' not found")
21+
}
22+
res
23+
}

R/read.R

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
#' accross several \R packages. See `getOption("read_write")`.
5454
#' @author Philippe Grosjean <phgrosjean@sciviews.org>
5555
#' @export
56-
#' @seealso [read_csv()]
56+
#' @seealso [data_types()], [write()], [read_csv()]
5757
#' @keywords utilities
5858
#' @concept read and import data
5959
#' @examples
@@ -303,32 +303,12 @@ sidecar_file = TRUE, fun_list = NULL, hfun = NULL, fun = NULL, ...) {
303303
} else fun_item <- fun_list[fun_list$type == type, ]
304304
}
305305

306-
get_function <- function(fun) {
307-
# In case we have ns::fun
308-
fun <- strsplit(fun, "::", fixed = TRUE)[[1L]]
309-
if (length(fun) == 2L) {
310-
res <- try(getExportedValue(fun[1L], fun[2L]), silent = TRUE)
311-
if (inherits(res, "try-error"))
312-
stop("You need function '", fun[2L], "' from package '", fun[1L],
313-
"' to read these data. Please, install the package first",
314-
" and make sure the function is available there.")
315-
} else {
316-
if (is.na(fun[1L]))
317-
return(NA)
318-
res <- get0(fun[1L], envir = parent.frame(), mode = "function",
319-
inherits = TRUE)
320-
if (is.null(res))
321-
stop("function '", fun[1], "' not found")
322-
}
323-
res
324-
}
325-
326306
# If header is not NULL and a hread_xxx() function is available,
327307
# read as many lines as there are starting with this string
328308
# and decrypt header data/metadata
329309
attribs <- NULL
330310
if (is.null(hfun))
331-
hfun <- get_function(fun_item$read_header)
311+
hfun <- .get_function(fun_item$read_header)
332312
if (is.function(hfun) && !is.null(header) && header != "") {
333313
dat <- hfun(file = file, header.max = header.max, skip = skip,
334314
locale = locale)
@@ -398,7 +378,7 @@ sidecar_file = TRUE, fun_list = NULL, hfun = NULL, fun = NULL, ...) {
398378

399379
# Do we have a function to read these data?
400380
if (is.null(fun))
401-
fun <- get_function(fun_item$read_fun)
381+
fun <- .get_function(fun_item$read_fun)
402382

403383
# Read the data
404384
skip_all <- skip + n_header

R/read_write_option.R

Lines changed: 41 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -24,87 +24,91 @@
2424
#' (read_write_option())
2525
#' # To add a new type:
2626
#' tail(read_write_option(data.frame(type = "png", read_fun = "png::readPNG",
27-
#' read_header = NA, write_fun = "png::writePNG")))
27+
#' read_header = NA, write_fun = "png::writePNG", comment = "PNG image")))
2828
read_write_option <- function(new_type) {
2929
opts <- getOption("read_write", default = tibble::tribble(
3030
~type, ~read_fun, ~read_header,
31-
~write_fun,
31+
~write_fun, ~comment,
3232
"csv", "readr::read_csv", "data.io::hread_text",
33-
"readr::write_csv",
33+
"readr::write_csv", "comma separated values",
3434
"csv2", "readr::read_csv2", "data.io::hread_text",
35-
NA,
35+
NA, "semicolon separated values",
3636
"xlcsv", "readr::read_csv", "data.io::hread_text",
37-
"readr::write_excel_csv",
37+
"readr::write_excel_csv", "write a CSV file more easily readable by Excel",
3838
"tsv", "readr::read_tsv", "data.io::hread_text",
39-
"readr::write_tsv",
39+
"readr::write_tsv", "tab separated values",
4040
"fwf", "readr::read_fwf", "data.io::hread_text",
41-
NA, # TODO: a writer here!
41+
NA, "fixed width file", # TODO: a writer here!
4242
"log", "readr::read_log", NA,
43-
NA, # TODO: a writer here!
43+
NA, "standard log file", # TODO: a writer here!
4444
"rds", "readr::read_rds", NA,
45-
"readr::write_rds",
45+
"readr::write_rds", "R data file (no compression by default)",
4646
"txt", "readr::read_file", NA,
47-
"readr::write_file",
47+
"readr::write_file", "text file (as length 1 character vector)",
4848
"raw", "readr::read_file_raw", NA,
49-
NA, # TODO: a writer here!
49+
NA, "binary file (read as raw vector)",
50+
# TODO: a writer here!
5051
"ssv", "readr::read_table", "data.io::hread_text",
51-
NA,#Space separated values
52+
NA, "space separated values (strict)",
5253
"ssv2", "readr::read_table2", "data.io::hread_text",
53-
NA,
54+
NA, "space separated values (relaxed)",
5455
"csv.gz", "readr::read_csv", "data.io::hread_text",
55-
"readr::write_csv",
56+
"readr::write_csv", "gz compressed comma separated values",
5657
"csv2.gz", "readr::read_csv2", "data.io::hread_text",
57-
NA,
58+
NA, "gz compressed semicolon separated values",
5859
"tsv.gz", "readr::read_tsv", "data.io::hread_text",
59-
"readr::write_tsv",
60+
"readr::write_tsv", "gz compressed tab separated values",
6061
"txt.gz", "readr::read_file", NA,
61-
"readr::write_file",
62+
"readr::write_file", "gz compressed text file",
6263
"csv.bz2", "readr::read_csv", "data.io::hread_text",
63-
"readr::write_csv",
64+
"readr::write_csv", "bz2 compressed comma separated values",
6465
"csv2.bz2","readr::read_csv2", "data.io::hread_text",
65-
NA,
66+
NA, "bz2 compressed semicolon separated values",
6667
"tsv.bz2", "readr::read_tsv", "data.io::hread_text",
67-
"readr::write_tsv",
68+
"readr::write_tsv", "bz2 compressed tab separated values",
6869
"txt.bz2", "readr::read_file", "data.io::hread_text",
69-
"readr::write_file",
70+
"readr::write_file", "bz2 compressed text file",
7071
"csv.xz", "readr::read_csv", "data.io::hread_text",
71-
"readr::write_csv",
72+
"readr::write_csv", "xz compressed comma separated values",
7273
"csv2.xz", "readr::read_csv2", "data.io::hread_text",
73-
NA,
74+
NA, "xz compressed semicolon separated values",
7475
"tsv.xz", "readr::read_tsv", "data.io::hread_text",
75-
"readr::write_tsv",
76+
"readr::write_tsv", "xz compressed tab separated values",
7677
"txt.xz", "readr::read_file", NA,
77-
"readr::write_file",
78+
"readr::write_file", "xz compressed text file",
7879
# Buggy right now!! "csvy", "csvy::read_csvy", NA, "csvy::write_csvy",
80+
# "comma separated value with YAML header",
7981
"xls", "readxl::read_excel", "data.io::hread_xls",
80-
"WriteXLS::WriteXLS",
82+
"WriteXLS::WriteXLS", "Excel old .xls format",
8183
"xlsx", "readxl::read_excel", "data.io::hread_xlsx",
82-
"openxlsx::write.xlsx",
84+
"writexl::write_xlsx", "Excel new .xlsx format", #"openxlsx::write.xlsx",
8385
"dta", "haven::read_dta", NA,
84-
"haven::write_dta",
86+
"haven::write_dta", "Stata DTA format",
8587
# read_dta() = read_stata()
8688
"sas", "haven::read_sas", NA,
87-
"haven::write_sas",
89+
"haven::write_sas", "SAS format",
8890
"sas7bdat","haven::read_sas", NA,
89-
"haven::write_sas",
91+
"haven::write_sas", "SAS format (sas7bdat)",
9092
"sav", "haven::read_sav", NA,
91-
"haven::write_sav",
93+
"haven::write_sav", "SPSS .sav format",
94+
"zsav", "haven::read_sav", NA,
95+
"haven::write_sav", "SPSS .zsav format",
9296
"por", "haven::read_por", NA,
93-
NA,
97+
NA, "SPSS .por format",
9498
# read_por()/read_sav() = read_spss()
9599
"xpt", "haven::read_xpt", NA,
96-
"haven::write_xpt" #,
100+
"haven::write_xpt", "SPSS transport format (FDA compliant)"#,
97101
#"feather", "feather::read_feather",NA,
98-
#"feather::write_feather"
102+
#"feather::write_feather", "transportable feather format"
99103
))
100104

101105
if (!missing(new_type)) {
102106
# Check it is in a correct format
103107
if (!is.data.frame(new_type))
104108
stop("new_type must be a data.frame")
105-
if (ncol(new_type) != 4)
106-
stop("new_type must contain 4 columns",
107-
" (type, read_fun, read_header & write_fun")
109+
if (ncol(new_type) != 5)
110+
stop("new_type must contain 5 columns",
111+
" (type, read_fun, read_header, write_fun & comment")
108112
names(new_type) <- names(opts)
109113
opts <- rbind(opts, new_type)
110114
}

0 commit comments

Comments
 (0)