-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add write(), data_types() and comment to "read_write" options
- Loading branch information
1 parent
7ce085c
commit 0b0743a
Showing
14 changed files
with
387 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#' List recognized file formats (types) for read() and write() | ||
#' | ||
#' @param types_only If `TRUE`, only a vector of types is returned, otherwise, | ||
#' a `tibble` with fll specifications is provided. | ||
#' @param view If `TRUE`, the result is "viewed" (displayed in a table in a | ||
#' separate window, if the user interface allows it, e.g., in RStudio) and | ||
#' returned invisibly. Otherwise, the results are returned normally. | ||
#' | ||
#' @description Dispaly information about data types that can read() and write() | ||
#' can use, as well as, the original functions that are delegated (see they | ||
#' respective help pages for more info and to know which additional parameters | ||
#' can be used in read() and write()). | ||
#' | ||
#' @return An `tibble` with `types_only = FALSE`, or a character vector. | ||
#' @details The function is mainly designed to be used interactively and to | ||
#' provide information about file types that can be read() or write(). This | ||
#' cannot be done through a man page because this list is dynamic and other | ||
#' packages could add or change entries there. With `view = FALSE`, the function | ||
#' can, nevertheless, be also used in a script or a R Markdown/Notebook | ||
#' document. | ||
#' @author Philippe Grosjean <phgrosjean@sciviews.org> | ||
#' @export | ||
#' @seealso [read()], [write()] | ||
#' @keywords utilities | ||
#' @concept list file types that can be read or write | ||
#' @examples | ||
#' \dontrun{ | ||
#' data_types() | ||
#' data_types(TRUE) | ||
#' } | ||
#' # For non-interactive use, specify view = FALSE | ||
#' data_types(view = FALSE) | ||
#' data_types(TRUE, view = FALSE) | ||
data_types <- function(types_only = FALSE, view = TRUE) { | ||
`data types` <- getOption("read_write") | ||
# If not installed yet, do it now! | ||
if (is.null(`data types`)) | ||
`data types` <- read_write_option() | ||
|
||
if (isTRUE(types_only)) | ||
`data types` <- `data types`$type | ||
|
||
if (isTRUE(view)) { | ||
# We don't necessarily want to use utils::View(). For instance, RStudio | ||
# defines another version of that function, and we ant to use it instead! | ||
view <- get0("View", mode = "function") | ||
if (is.null(view)) { | ||
warning("'View' function not found, return the data instead") | ||
`data types` | ||
} else { | ||
view(`data types`) | ||
invisible(`data types`) | ||
} | ||
} else `data types` | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,23 @@ | ||
.onLoad <- function(libname, pkgname) { | ||
read_write_option() | ||
} | ||
|
||
.get_function <- function(fun) { | ||
# In case we have ns::fun | ||
fun <- strsplit(fun, "::", fixed = TRUE)[[1L]] | ||
if (length(fun) == 2L) { | ||
res <- try(getExportedValue(fun[1L], fun[2L]), silent = TRUE) | ||
if (inherits(res, "try-error")) | ||
stop("You need function '", fun[2L], "' from package '", fun[1L], | ||
"' to read these data. Please, install the package first", | ||
" and make sure the function is available there.") | ||
} else { | ||
if (is.na(fun[1L])) | ||
return(NA) | ||
res <- get0(fun[1L], envir = parent.frame(), mode = "function", | ||
inherits = TRUE) | ||
if (is.null(res)) | ||
stop("function '", fun[1], "' not found") | ||
} | ||
res | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.