Skip to content

Commit

Permalink
data.io-lang option for default language to use with read() and the d…
Browse files Browse the repository at this point in the history
…ata is recorded as attribute of the object's comment
  • Loading branch information
phgrosjean committed Sep 8, 2018
1 parent 0b0743a commit 2456cb9
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 14 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: data.io
Type: Package
Version: 1.1.0
Version: 1.2.0
Title: Data Input/Output, Read or Write Data from Files or Datasets in R Packages in Different Formats
Description: Read or write data from many different formats (tabular datasets,
from statistic software, ...) into R objects. Add labels and units in
Expand Down
9 changes: 9 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# data.io News

## Changes in data.io 1.2.0

- It is now possible to specify the default language to use for read() with
the option `data.io-lang`.

- `lang` and `lang_encoding` are now recoarde as attributes of the comment of
the imported object.


## Changes in data.io 1.1.0

- A basic version of write() is now available.
Expand Down
19 changes: 12 additions & 7 deletions R/read.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
#' defaults as US English + UTF-8 encoding, and it is advised to be used as
#' much as possible.
#' @param lang The language to use (mainly for comment, label and units), but
#' also for factor levels or other chanracter strings if a translation exists
#' also for factor levels or other character strings if a translation exists
#' and if the language is spelled with uppercase characters (e.g., `"FR"`).
#' The default value can be set with, e.g., `options(data.io_lang = "fr")` for
#' French.
#' @param lang_encoding Encoding used by R scripts for translation. They should
#' all be encoded as `UTF-8`, which is the default. However, this argument
#' allows to specify a different encoding if needed.
Expand Down Expand Up @@ -189,9 +191,10 @@
#' (afalfa <- read(data_example("afalfa.xpt"))) # SAS transport file
#' }
read <- structure(function(file, type = NULL, header = "#", header.max = 50L,
skip = 0L, locale = default_locale(), lang = "en", lang_encoding = "UTF-8",
as_dataframe = TRUE, as_labelled = FALSE, comments = NULL, package = NULL,
sidecar_file = TRUE, fun_list = NULL, hfun = NULL, fun = NULL, ...) {
skip = 0L, locale = default_locale(), lang = getOption("data.io_lang", "en"),
lang_encoding = "UTF-8", as_dataframe = TRUE, as_labelled = FALSE,
comments = NULL, package = NULL, sidecar_file = TRUE, fun_list = NULL,
hfun = NULL, fun = NULL, ...) {
if (!is.null(lang)) {
if (length(lang) != 1 || !is.character(lang))
stop("lang must be a single character string or NULL")
Expand Down Expand Up @@ -436,18 +439,20 @@ sidecar_file = TRUE, fun_list = NULL, hfun = NULL, fun = NULL, ...) {
}
}

# Record the comments and origin of the data
# Record the comments, lang, lang_encoding and origin of the data
cmt <- comment(res)
cmt[] <- c(cmt, comments)
if (is.null(cmt)) cmt2 <- "" else cmt2 <- cmt
attr(cmt2, "lang") <- lang
attr(cmt2, "lang_encoding") <- lang_encoding
if (!is.null(srcfile)) {
attr(cmt2, "srcfile") <- srcfile
comment(res) <- cmt2
} else if (!is.null(src)) {
attr(cmt2, "src") <- src
comment(res) <- cmt2
} else if (!is.null(cmt)) {
comment(res) <- cmt
} else {
comment(res) <- cmt2
}

if (isTRUE(as_dataframe)) {# Try to convert the object into a dataframe
Expand Down
14 changes: 8 additions & 6 deletions man/read.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2456cb9

Please sign in to comment.