1
0
mirror of https://github.com/msberends/AMR.git synced 2024-12-25 19:26:13 +01:00

removed clipboard functions as it violated CRAN policies

This commit is contained in:
dr. M.S. (Matthijs) Berends 2018-09-28 16:05:18 +02:00
parent 2b0080995e
commit 768fe17324
12 changed files with 3551 additions and 451 deletions

View File

@ -35,12 +35,6 @@ matrix:
r_packages: covr
cache: packages
cran: https://cran.rstudio.com
before_install:
# needed for clipboard function:
# - if [ $TRAVIS_OS_NAME = linux ]; then sudo apt-get -qq update; fi
# - if [ $TRAVIS_OS_NAME = linux ]; then sudo apt-get install -y xclip; fi
- if [ $TRAVIS_OS_NAME = osx ]; then brew install xclip; fi
- if [ $TRAVIS_OS_NAME = osx ]; then brew install libgit2; fi
install:
- Rscript -e "install.packages(c('devtools', 'backports', 'clipr', 'curl', 'data.table', 'dplyr', 'hms', 'knitr', 'readr', 'rlang', 'rvest', 'xml2', 'covr', 'ggplot2', 'rmarkdown', 'testthat', 'tidyr'))"
@ -48,8 +42,7 @@ install:
# postrun
after_success:
# only run codecov on osx, so clipboard functions can be checked
- if [ $TRAVIS_OS_NAME = osx ]; then Rscript -e 'covr::codecov()'; fi
- Rscript -e 'covr::codecov()'
notifications:
email:
recipients:

View File

@ -1,5 +1,5 @@
Package: AMR
Version: 0.3.0.9009
Version: 0.3.0.9010
Date: 2018-09-27
Title: Antimicrobial Resistance Analysis
Authors@R: c(
@ -46,13 +46,11 @@ Depends:
R (>= 3.1.0)
Imports:
backports,
clipr,
curl,
data.table (>= 1.9.0),
dplyr (>= 0.7.0),
hms,
knitr (>= 1.0.0),
readr,
rlang (>= 0.2.0),
rvest (>= 0.3.2),
xml2 (>= 1.0.0)

View File

@ -57,8 +57,6 @@ export(as.rsi)
export(atc_ddd)
export(atc_groups)
export(atc_property)
export(clipboard_export)
export(clipboard_import)
export(count_I)
export(count_IR)
export(count_R)
@ -159,8 +157,6 @@ exportMethods(skewness.default)
exportMethods(skewness.matrix)
exportMethods(summary.mic)
exportMethods(summary.rsi)
importFrom(clipr,read_clip_tbl)
importFrom(clipr,write_clip)
importFrom(curl,nslookup)
importFrom(data.table,as.data.table)
importFrom(data.table,data.table)
@ -200,8 +196,6 @@ importFrom(graphics,hist)
importFrom(graphics,plot)
importFrom(graphics,text)
importFrom(knitr,kable)
importFrom(readr,locale)
importFrom(readr,parse_guess)
importFrom(rvest,html_children)
importFrom(rvest,html_node)
importFrom(rvest,html_nodes)
@ -217,7 +211,4 @@ importFrom(stats,sd)
importFrom(utils,View)
importFrom(utils,browseVignettes)
importFrom(utils,installed.packages)
importFrom(utils,object.size)
importFrom(utils,read.delim)
importFrom(utils,write.table)
importFrom(xml2,read_html)

View File

@ -54,9 +54,9 @@
* Function `ab_property` and its aliases: `ab_name`, `ab_tradenames`, `ab_certe`, `ab_umcg` and `ab_trivial_nl`
* Introduction to AMR as a vignette
* Removed clipbaord functions as it violated the CRAN policy
#### Changed
* Added almost 200 microorganisms to the `microorganisms` data set and removed the few viruses it contained
* Added three antimicrobial agents to the `antibiotics` data set: Terbinafine (D01BA02), Rifaximin (A07AA11) and Isoconazole (D01AC05)
* Added 163 trade names to the `antibiotics` data set, it now contains 298 different trade names in total, e.g.:
```r

View File

@ -1,165 +0,0 @@
#' 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.
#'
#' This also supports automatic column type transformation, with AMR classes \code{\link{as.rsi}} and \code{\link{as.mic}}.
#' @rdname clipboard
#' @name clipboard
#' @inheritParams base::data.frame
#' @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.
#' @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.
#' @param remove_ASCII_escape_char remove ASCII escape character
#' @param info print info to console
#' @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
#' 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.
#'
#' \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
#' @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()
#' }
clipboard_import <- function(sep = '\t',
quote = "",
header = TRUE,
dec = ".",
na = c("", "NA", "NULL"),
stringsAsFactors = FALSE,
startrow = 1,
as_vector = TRUE,
guess_col_types = TRUE,
date_names = 'en',
date_format = '%Y-%m-%d',
time_format = '%H:%M',
remove_ASCII_escape_char = FALSE,
tz = "UTC",
encoding = "UTF-8",
info = TRUE) {
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
import_tbl <- clipr::read_clip_tbl(file = file,
sep = sep,
quote = quote,
header = header,
strip.white = TRUE,
dec = dec,
na.strings = na,
encoding = 'UTF-8',
stringsAsFactors = stringsAsFactors)
# 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 (guess_col_types == TRUE) {
if (info == TRUE) {
cat('Transforming data by guessing column types...')
}
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,
remove_ASCII_escape_char = remove_ASCII_escape_char,
na = na)
if (info == TRUE) {
cat('OK\n')
}
}
if (NCOL(import_tbl) == 1 & as_vector == TRUE) {
import_tbl <- import_tbl %>% pull(1)
}
# and transform back to data.frame
import_tbl <- as.data.frame(import_tbl, stringsAsFactors = stringsAsFactors)
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) {
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")
}
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")
}
}

View File

@ -110,99 +110,3 @@ size_humanreadable <- function(bytes, decimals = 1) {
out <- paste(sprintf(paste0("%.", decimals, "f"), bytes / (1024 ^ factor)), size[factor + 1])
out
}
# based on readr::parse_guess
tbl_parse_guess <- function(tbl,
date_names = 'en',
date_format = '%Y-%m-%d',
time_format = '%H:%M',
decimal_mark = '.',
tz = "UTC",
encoding = "UTF-8",
remove_ASCII_escape_char = FALSE,
na = c("", "NA", "NULL")) {
date_format <- date_generic(date_format)
time_format <- date_generic(time_format)
# set col types with readr
for (i in 1:ncol(tbl)) {
if (!all(tbl %>% pull(i) %>% class() %in% c('list', 'matrix'))) {
tbl[, i] <- readr::parse_guess(x = tbl %>% pull(i) %>% as.character(),
na = na,
locale = readr::locale(date_names = date_names,
date_format = date_format,
time_format = time_format,
decimal_mark = decimal_mark,
encoding = encoding,
tz = tz,
asciify = FALSE))
}
if (any(tbl %>% pull(i) %>% class() %in% c('factor', 'character'))) {
if (remove_ASCII_escape_char == TRUE) {
# remove ASCII escape character: https://en.wikipedia.org/wiki/Escape_character#ASCII_escape_character
tbl[, i] <- tbl %>% pull(i) %>% gsub('\033', ' ', ., fixed = TRUE)
}
if (tbl %>% pull(i) %>% is.rsi.eligible()) {
# look for RSI
tbl[, i] <- as.rsi(tbl[, i])
}
}
# convert to MIC class when ends on `_mic`
if (colnames(tbl)[i] %like% '_mic$') {
tbl[, i] <- as.mic(tbl[, i])
}
}
tbl
}
# transforms date format like "dddd d mmmm yyyy" to "%A %e %B %Y"
date_generic <- function(format) {
if (!grepl('%', format, fixed = TRUE)) {
# first months and minutes, after that everything is case INsensitive
format <- gsub('mmmm', '%B1', format, fixed = TRUE)
format <- gsub('mmm', '%b', format, fixed = TRUE)
format <- gsub('mm', '%m', format, fixed = TRUE)
format <- gsub('MM', '%M1', format, fixed = TRUE)
format <- format %>%
tolower() %>%
gsub('%b1', '%B', ., fixed = TRUE) %>%
gsub('%m1', '%M', ., fixed = TRUE)
# dates
format <- gsub('dddd', '%A', format, fixed = TRUE)
format <- gsub('ddd', '%a', format, fixed = TRUE)
format <- gsub('dd', '%!', format, fixed = TRUE)
format <- gsub('d', '%e', format, fixed = TRUE)
format <- gsub('%!', '%d', format, fixed = TRUE)
format <- gsub('ww', '%V', format, fixed = TRUE)
format <- gsub('w', '%V', format, fixed = TRUE)
format <- gsub('qq', 'Qq', format, fixed = TRUE) # so will be 'Q%%q' after this
format <- gsub('kk', 'Kq', format, fixed = TRUE)
format <- gsub('k', 'q', format, fixed = TRUE)
format <- gsub('q', '%%q', format, fixed = TRUE)
format <- gsub('yyyy_iso', '%G', format, fixed = TRUE)
format <- gsub('jjjj_iso', '%G', format, fixed = TRUE)
format <- gsub('yyyy', '%Y', format, fixed = TRUE)
format <- gsub('jjjj', '%Y', format, fixed = TRUE)
format <- gsub('yy_iso', '%g', format, fixed = TRUE)
format <- gsub('jj_iso', '%g', format, fixed = TRUE)
format <- gsub('yy', '%y', format, fixed = TRUE)
format <- gsub('jj', '%y', format, fixed = TRUE)
# time
format <- gsub('hh', '%H', format, fixed = TRUE)
format <- gsub('h', '%k', format, fixed = TRUE)
format <- gsub('ss', '%S', format, fixed = TRUE)
# seconds since the Epoch, 1970-01-01 00:00:00
format <- gsub('unix', '%s', format, fixed = TRUE)
# Equivalent to %Y-%m-%d (the ISO 8601 date format)
format <- gsub('iso', '%F', format, fixed = TRUE)
}
format
}

View File

@ -1,4 +1,4 @@
# `AMR`
# `AMR` <img src="man/figures/logo_amr.png" align="right" height="120px" />
### An [R package](https://www.r-project.org) to simplify the analysis and prediction of Antimicrobial Resistance (AMR) and work with antibiotic properties by using evidence-based methods.
This R package was created for academic research by PhD students of the Faculty of Medical Sciences of the [University of Groningen](https://www.rug.nl) and the Medical Microbiology & Infection Prevention (MMBI) department of the [University Medical Center Groningen (UMCG)](https://www.umcg.nl).

View File

@ -1,139 +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", quote = "", header = TRUE, dec = ".",
na = c("", "NA", "NULL"), stringsAsFactors = FALSE, startrow = 1,
as_vector = TRUE, guess_col_types = TRUE, date_names = "en",
date_format = "\%Y-\%m-\%d", time_format = "\%H:\%M",
remove_ASCII_escape_char = FALSE, tz = "UTC", encoding = "UTF-8",
info = 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{quote}{the set of quoting characters. To disable quoting
altogether, use \code{quote = ""}. See \code{\link{scan}} for the
behaviour on quotes embedded in quotes. Quoting is only considered
for columns read as character, which is all of them unless
\code{colClasses} is specified.}
\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{stringsAsFactors}{logical: should character vectors be converted
to factors? The \sQuote{factory-fresh} default is \code{TRUE}, but
this can be changed by setting \code{\link{options}(stringsAsFactors
= FALSE)}.}
\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{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.}
\item{date_names}{Character representations of day and month names. Either
the language code as string (passed on to \code{\link[=date_names_lang]{date_names_lang()}})
or an object created by \code{\link[=date_names]{date_names()}}.}
\item{date_format}{Default date and time formats.}
\item{time_format}{Default date and time formats.}
\item{remove_ASCII_escape_char}{remove ASCII escape character}
\item{tz}{Default tz. This is used both for input (if the time zone isn't
present in individual strings), and for output (to control the default
display). The default is to use "UTC", a time zone that does not use
daylight savings time (DST) and hence is typically most useful for data.
The absence of time zones makes it approximately 50x faster to generate
UTC times than any other time zone.
Use \code{""} to use the system default time zone, but beware that this
will not be reproducible across systems.
For a complete list of possible time zones, see \code{\link{OlsonNames}()}.
Americans, note that "EST" is a Canadian time zone that does not have
DST. It is \emph{not} Eastern Standard Time. It's better to use
"US/Eastern", "US/Central" etc.}
\item{encoding}{encoding to be assumed for input strings. It is
used to mark character strings as known to be in
Latin-1 or UTF-8 (see \code{\link{Encoding}}): it is not used to
re-encode the input, but allows \R to handle encoded strings in
their native encoding (if one of those two). See \sQuote{Value}
and \sQuote{Note}.
}
\item{info}{print info to console}
\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.}
}
\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.
This also supports automatic column type transformation, with AMR classes \code{\link{as.rsi}} and \code{\link{as.mic}}.
}
\details{
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.
\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>}
}
}
\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()
}
}
\keyword{clipboard}
\keyword{clipboard_export}
\keyword{clipboard_import}
\keyword{export}
\keyword{import}

BIN
man/figures/logo_amr.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 56 KiB

3547
man/figures/logo_amr.svg Normal file

File diff suppressed because it is too large Load Diff

After

Width:  |  Height:  |  Size: 222 KiB

View File

@ -1,24 +0,0 @@
context("clipboard.R")
test_that("clipboard works", {
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")
}
skip_if_not(clipr::clipr_available())
# clipboard_export(antibiotics)
# imp <- clipboard_import(guess_col_types = FALSE,
# stringsAsFactors = FALSE)
# expect_identical(as.data.frame(antibiotics, stringsAsFactors = FALSE),
# imp)
clipboard_export(septic_patients[1:100,])
imp <- clipboard_import(guess_col_types = TRUE,
stringsAsFactors = FALSE)
expect_identical(as.data.frame(tbl_parse_guess(septic_patients[1:100,]),
stringsAsFactors = FALSE),
imp)
})

View File

@ -18,8 +18,3 @@ test_that("functions missing in older R versions work", {
expect_equal(trimws(" test ", "l"), "test ")
expect_equal(trimws(" test ", "r"), " test")
})
test_that("generic dates work", {
expect_equal(date_generic("yyyy-mm-dd"), "%Y-%m-%d")
expect_equal(date_generic("dddd d mmmm yyyy"), "%A %e %B %Y")
})