diff --git a/NAMESPACE b/NAMESPACE index 4388652b..b027ce30 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -25,8 +25,6 @@ export(anti_join_microorganisms) export(as.mic) export(as.rsi) export(atc_property) -export(clipboard_export) -export(clipboard_import) export(first_isolate) export(freq) export(frequency_tbl) @@ -65,7 +63,6 @@ importFrom(dplyr,all_vars) importFrom(dplyr,any_vars) importFrom(dplyr,arrange) importFrom(dplyr,arrange_at) -importFrom(dplyr,as_tibble) importFrom(dplyr,between) importFrom(dplyr,desc) importFrom(dplyr,filter) @@ -99,8 +96,5 @@ importFrom(rvest,html_table) importFrom(stats,fivenum) importFrom(stats,quantile) importFrom(stats,sd) -importFrom(utils,object.size) importFrom(utils,packageDescription) -importFrom(utils,read.delim) -importFrom(utils,write.table) importFrom(xml2,read_html) diff --git a/NEWS.md b/NEWS.md index 4d6f62ef..ec2a0d5a 100644 --- a/NEWS.md +++ b/NEWS.md @@ -4,7 +4,6 @@ * Full support for old R versions, only R-3.0.0 (April 2013) or later is needed (needed packages may have other dependencies) * Function `guess_bactid` to **determine the ID** of a microorganism based on genus/species or known abbreviations like MRSA * Function `guess_atc` to **determine the ATC** of an antibiotic based on name, trade name, or known abbreviations -* Functions `clipboard_import` and `clipboard_export` as helper functions to **quickly copy and paste** from/to software like Excel and SPSS * Function `freq` to create **frequency tables**, with additional info in a header * Function `MDRO` to **determine Multi Drug Resistant Organisms (MDRO)** with support for country-specific guidelines. * Suggest your own via [https://github.com/msberends/AMR/issues/new](https://github.com/msberends/AMR/issues/new?title=New%20guideline%20for%20MDRO&body=%3C--%20Please%20add%20your%20country%20code,%20guideline%20name,%20version%20and%20source%20below%20and%20remove%20this%20line--%3E) diff --git a/R/clipboard.R b/R/clipboard.R deleted file mode 100644 index 70c56c60..00000000 --- a/R/clipboard.R +++ /dev/null @@ -1,140 +0,0 @@ -#' Import/export from clipboard -#' -#' 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. See Details for an example. -#' @rdname clipboard -#' @name clipboard -#' @inheritParams utils::read.table -#' @inheritParams utils::write.table -#' @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. -#' @param info print info about copying -#' @keywords clipboard clipboard_import clipboard_export import export -#' @importFrom dplyr %>% pull as_tibble -#' @importFrom utils read.delim write.table object.size -#' @details For \code{clipboard_export()}, the reserved clipboard size for exporting will be set to 125\% of the object size of \code{x}. This way, it is possible to export data with thousands of rows as the only limit will be your systems RAM. -#' -#' \if{html}{ -#' Example for copying from Excel: -#' \out{
}\figure{Excel_copy.png}\out{
} -#' \cr -#' And pasting in R: \cr \cr -#' \code{> data <- clipboard_import()} \cr -#' \code{> data} \cr -#' \out{
}\figure{Excel_paste.png}\out{
} -#' } -#' @export -#' @return data.frame -clipboard_import <- function(sep = '\t', - header = TRUE, - dec = ".", - na = c("", "NA", "NULL"), - startrow = 1, - as_vector = TRUE) { - - if (is_Windows() == TRUE) { - file <- 'clipboard' - } else { - # use xclip package - check_xclip() - file <- pipe("xclip -o", "r") - on.exit(close(file)) - } - - import_tbl <- tryCatch(read.delim(file = file, - sep = sep, - header = header, - strip.white = TRUE, - dec = dec, - na.strings = na, - fileEncoding = 'UTF-8', - encoding = 'UTF-8', - stringsAsFactors = FALSE), - error = function(e) { - FALSE - }) - - if (all(import_tbl == FALSE)) { - cat("No clipboard content found.") - if (Sys.info()['sysname'] %like% "Linux") { - cat(" These functions do not work without X11 installed.") - } - cat("\n") - return(invisible()) - } - - # use tibble, so column types will be translated correctly - import_tbl <- as_tibble(import_tbl) - - if (startrow > 1) { - # would else lose column headers - import_tbl <- import_tbl[startrow:nrow(import_tbl),] - } - - colnames(import_tbl) <- gsub('[.]+', '_', colnames(import_tbl)) - - if (NCOL(import_tbl) == 1 & as_vector == TRUE) { - import_tbl %>% pull(1) - } else { - import_tbl - } -} - -#' @rdname clipboard -#' @importFrom dplyr %>% pull as_tibble -#' @export -clipboard_export <- function(x, - sep = '\t', - dec = ".", - na = "", - header = TRUE, - info = TRUE) { - - x <- deparse(substitute(x)) - size <- x %>% - get() %>% - object.size() %>% - formatC(format = 'd') %>% - as.integer() - - x <- get(x) - - if (is_Windows() == TRUE) { - # set size of clipboard to 125% of the object size of x - file <- paste0("clipboard-", size * 1.25) - } else { - # use xclip package - check_xclip() - file <- pipe("xclip -i", "w") - on.exit(close(file)) - } - - tryCatch(write.table(x = x, - file = file, - sep = sep, - na = na, - row.names = FALSE, - col.names = header, - dec = dec, - quote = FALSE), - error = function(e) { - FALSE - }) - - if (info == TRUE) { - cat("Successfully exported to clipboard:", NROW(x), "obs. of", NCOL(x), "variables.\n") - } - -} - -is_Windows <- function() { - Sys.info()['sysname'] %like% "Windows" -} -check_xclip <- function() { - if (!isTRUE(file.exists(Sys.which("xclip")[1L]))) { - if (Sys.info()['sysname'] %like% "Linux") { - stop("Please install Linux package xclip first.") - } else { - stop("Please install package xclip first (use `brew install xclip` on macOS).") - } - } -} diff --git a/man/clipboard.Rd b/man/clipboard.Rd deleted file mode 100644 index 09607b58..00000000 --- a/man/clipboard.Rd +++ /dev/null @@ -1,63 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/clipboard.R -\name{clipboard} -\alias{clipboard} -\alias{clipboard_import} -\alias{clipboard_export} -\title{Import/export from clipboard} -\usage{ -clipboard_import(sep = "\\t", header = TRUE, dec = ".", na = c("", "NA", - "NULL"), startrow = 1, as_vector = TRUE) - -clipboard_export(x, sep = "\\t", dec = ".", na = "", header = TRUE, - info = TRUE) -} -\arguments{ -\item{sep}{the field separator character. Values on each line of the - file are separated by this character. If \code{sep = ""} (the - default for \code{read.table}) the separator is \sQuote{white space}, - that is one or more spaces, tabs, newlines or carriage returns.} - -\item{header}{a logical value indicating whether the file contains the - names of the variables as its first line. If missing, the value is - determined from the file format: \code{header} is set to \code{TRUE} - if and only if the first row contains one fewer field than the - number of columns.} - -\item{dec}{the character used in the file for decimal points.} - -\item{na}{the string to use for missing values in the data.} - -\item{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.} - -\item{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.} - -\item{x}{the object to be written, preferably a matrix or data frame. - If not, it is attempted to coerce \code{x} to a data frame.} - -\item{info}{print info about copying} -} -\value{ -data.frame -} -\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. See Details for an example. -} -\details{ -For \code{clipboard_export()}, the reserved clipboard size for exporting will be set to 125\% of the object size of \code{x}. This way, it is possible to export data with thousands of rows as the only limit will be your systems RAM. - - \if{html}{ - Example for copying from Excel: - \out{
}\figure{Excel_copy.png}\out{
} - \cr - And pasting in R: \cr \cr - \code{> data <- clipboard_import()} \cr - \code{> data} \cr - \out{
}\figure{Excel_paste.png}\out{
} - } -} -\keyword{clipboard} -\keyword{clipboard_export} -\keyword{clipboard_import} -\keyword{export} -\keyword{import} diff --git a/tests/testthat/test-clipboard.R b/tests/testthat/test-clipboard.R deleted file mode 100644 index 9d30bba5..00000000 --- a/tests/testthat/test-clipboard.R +++ /dev/null @@ -1,12 +0,0 @@ -context("clipboard.R") - -test_that("clipboard works", { - if (grepl(Sys.info()['sysname'], "windows", ignore.case = TRUE)) { - t1 <<- AMR::antibiotics # why is the <<- needed? Won't work without it... - clipboard_export(t1, info = FALSE) - t2 <- clipboard_import() - expect_equal(t1, t2) - } else { - expect_equal(TRUE, TRUE) - } -})