1
0
mirror of https://github.com/msberends/AMR.git synced 2024-12-26 06:46:11 +01:00
AMR/R/clipboard.R

166 lines
6.6 KiB
R
Raw Normal View History

2018-06-27 15:54:56 +02:00
#' Import/export from clipboard
#'
#' @description These are helper functions around \code{\link{read.table}} and \code{\link{write.table}} to import from and export to clipboard with support for Windows, Linux and macOS.
#'
#' The data will be read and written as tab-separated by default, which makes it possible to copy and paste from other software like Excel and SPSS without further transformation.
#'
2018-06-29 08:56:03 +02:00
#' This also supports automatic column type transformation, with AMR classes \code{\link{as.rsi}} and \code{\link{as.mic}}.
2018-06-27 15:54:56 +02:00
#' @rdname clipboard
#' @name clipboard
2018-07-16 16:41:48 +02:00
#' @inheritParams base::data.frame
2018-06-27 15:54:56 +02:00
#' @inheritParams utils::read.table
#' @inheritParams utils::write.table
#' @inheritParams readr::locale
#' @param startrow \emph{n}th row to start importing from. When \code{header = TRUE}, the import will start on row \code{startrow} \emph{below} the header.
#' @param as_vector a logical value indicating whether data consisting of only one column should be imported as vector using \code{\link[dplyr]{pull}}. This will strip off the header.
2018-06-29 08:56:03 +02:00
#' @param guess_col_types a logical value indicating whether column types should be guessed and transformed automatically with \code{\link[readr]{parse_guess}} from the \code{readr} package. Besides, the antimicrobial classes in this AMR package (\code{\link{as.rsi}} and \code{\link{as.mic}}) are also supported.
2018-07-23 15:09:19 +02:00
#' @param remove_ASCII_escape_char remove ASCII escape character
2018-06-29 08:56:03 +02:00
#' @param info print info to console
2018-06-27 15:54:56 +02:00
#' @keywords clipboard clipboard_import clipboard_export import export
#' @importFrom dplyr %>% pull as_tibble
#' @importFrom clipr read_clip_tbl write_clip
#' @importFrom utils read.delim write.table object.size
#' @importFrom readr parse_guess locale
#' @details
2018-07-16 16:41:48 +02:00
#' The parameter \code{stringsAsFactors} defaults to \code{FALSE}, as opposed to most base \R methods.
#'
#' The parameters \code{date_format} and \code{time_format} also support generic date and time formats like \code{"dd-mm-yyyy"} like Excel.
#'
2018-06-27 15:54:56 +02:00
#' \if{html}{
#' \strong{Example for copying from Excel:}
#' \out{<div style="text-align: left">}\figure{clipboard_copy.png}\out{</div>}
#' \cr
#' \strong{And pasting in R:} \cr
#' \cr
#' \code{> data <- clipboard_import()} \cr
#' \code{> data} \cr
#' \out{<div style="text-align: left">}\figure{clipboard_paste.png}\out{</div>}
#' \cr
#' \strong{The resulting data contains the right RSI-classes:} \cr
#' \cr
#' \code{> data$amox} \cr
#' \out{<div style="text-align: left">}\figure{clipboard_rsi.png}\out{</div>}
#' }
#' @export
2018-07-16 16:41:48 +02:00
#' @examples
#' \dontrun{
#'
#' df1 <- data.frame(a = letters[1:12],
#' b = runif(n = 12, min = 1000, max = 2000),
#' stringsAsFactors = FALSE)
#' clipboard_export(df1)
#' df2 <- clipboard_import()
#' identical(df1, df2)
#'
#' # send frequency table to clipboard (e.g. for pasting in Excel)
#' septic_patients %>%
#' freq(age) %>%
#' format() %>% # this will format the percentages
#' clipboard_export()
#' }
2018-06-27 15:54:56 +02:00
clipboard_import <- function(sep = '\t',
2018-07-23 15:09:19 +02:00
quote = "",
2018-06-27 15:54:56 +02:00
header = TRUE,
dec = ".",
na = c("", "NA", "NULL"),
2018-07-16 16:41:48 +02:00
stringsAsFactors = FALSE,
2018-06-27 15:54:56 +02:00
startrow = 1,
as_vector = TRUE,
guess_col_types = TRUE,
date_names = 'en',
date_format = '%Y-%m-%d',
time_format = '%H:%M',
2018-07-23 15:09:19 +02:00
remove_ASCII_escape_char = FALSE,
2018-08-01 10:32:24 +02:00
tz = "UTC",
2018-06-27 15:54:56 +02:00
encoding = "UTF-8",
2018-06-29 08:56:03 +02:00
info = TRUE) {
2018-06-27 15:54:56 +02:00
2018-07-17 10:32:26 +02:00
if (!clipr::clipr_available() & Sys.info()['sysname'] == "Linux") {
# try to support on X11, by setting the R variable DISPLAY
Sys.setenv(DISPLAY = "localhost:10.0")
}
# this will fail when clipr is (still) not available
2018-06-27 15:54:56 +02:00
import_tbl <- clipr::read_clip_tbl(file = file,
sep = sep,
2018-07-23 15:09:19 +02:00
quote = quote,
2018-06-27 15:54:56 +02:00
header = header,
strip.white = TRUE,
dec = dec,
na.strings = na,
encoding = 'UTF-8',
2018-07-16 16:41:48 +02:00
stringsAsFactors = stringsAsFactors)
2018-06-27 15:54:56 +02:00
# use tibble, so column types will be translated correctly
import_tbl <- as_tibble(import_tbl)
if (startrow > 1) {
# would else lose column headers
2018-06-29 08:56:03 +02:00
import_tbl <- import_tbl[startrow:NROW(import_tbl),]
2018-06-27 15:54:56 +02:00
}
colnames(import_tbl) <- gsub('[.]+', '_', colnames(import_tbl))
if (guess_col_types == TRUE) {
if (info == TRUE) {
2018-06-29 08:56:03 +02:00
cat('Transforming data by guessing column types...')
2018-06-27 15:54:56 +02:00
}
2018-06-29 08:56:03 +02:00
import_tbl <- tbl_parse_guess(tbl = import_tbl,
date_names = date_names,
date_format = date_format,
time_format = time_format,
decimal_mark = dec,
tz = tz,
encoding = encoding,
2018-07-23 15:09:19 +02:00
remove_ASCII_escape_char = remove_ASCII_escape_char,
2018-06-29 08:56:03 +02:00
na = na)
2018-06-27 15:54:56 +02:00
if (info == TRUE) {
cat('OK\n')
}
}
if (NCOL(import_tbl) == 1 & as_vector == TRUE) {
import_tbl <- import_tbl %>% pull(1)
}
2018-07-16 16:41:48 +02:00
# and transform back to data.frame
import_tbl <- as.data.frame(import_tbl, stringsAsFactors = stringsAsFactors)
2018-06-27 15:54:56 +02:00
if (info == TRUE) {
cat("Successfully imported from clipboard:", NROW(import_tbl), "obs. of", NCOL(import_tbl), "variables.\n")
}
import_tbl
}
#' @rdname clipboard
#' @importFrom dplyr %>% pull as_tibble
#' @export
clipboard_export <- function(x,
sep = '\t',
dec = ".",
na = "",
header = TRUE,
info = TRUE) {
2018-07-17 10:32:26 +02:00
if (!clipr::clipr_available() & Sys.info()['sysname'] == "Linux") {
# try to support on X11, by setting the R variable DISPLAY
Sys.setenv(DISPLAY = "localhost:10.0")
}
2018-06-27 15:54:56 +02:00
clipr::write_clip(content = x,
na = na,
sep = sep,
row.names = FALSE,
col.names = header,
dec = dec,
quote = FALSE)
if (info == TRUE) {
cat("Successfully exported to clipboard:", NROW(x), "obs. of", NCOL(x), "variables.\n")
}
}