appling / unitted Goto Github PK
View Code? Open in Web Editor NEWAn R package that lets you attach smart, self-updating units to your data
License: Creative Commons Zero v1.0 Universal
An R package that lets you attach smart, self-updating units to your data
License: Creative Commons Zero v1.0 Universal
supposed to be much faster. question is whether it's sufficiently flexible, or could be made so.
this might be possible for data.frames using nested data.frames. possibly leverage tidyr as in http://blog.rstudio.org/2016/02/02/tidyr-0-4-0/?
expect_more_than loses the units of one of the two objects. For example:
> expect_more_than(u(1,"ho"),u(2,"ho"))
Error in unitted_Ops(.Generic, e1, unitted(e2, NA)) :
Units mismatch in comparison 'e1 (ho) < e2 ()'
header \t header \t etc.
data \t data \t etc
this breaks:
> tb <- u(dplyr::tbl_df(data.frame(site_name="nwis_01484680", lat=u(38.6, "degN"), lon=u(-75.1, "degE"))))
> tb
Source: local data frame [1 x 3]
site_name lat lon
U degN degE
1 nwis_01484680 38.6 -75.1
> tb$lat
Error in unitbundle(units) :
invalid class for units specification - must be data.frame, character, NA, unitbundle, or missing
the newest dplyr might help with preserving attributes:
http://blog.rstudio.org/2015/09/04/dplyr-0-4-3/
Looking for a boolean response for the following thing:
has_units(.Object, 'degC')
likewise, this would be nice too:
has_units(c(.Object,.Object2), c('degC','mg ^-L'))
Many standard conversions could be made available. I'm envisioning a function that takes the current and desired units and finds the appropriate conversion between them.
Some assignments to new columns work, others don't:
> library(unitted)
> df <- u(data.frame(x=1:3, y=4:6), c('a','b'))
> df$c <- TRUE
> df['d'] <- FALSE
Error in unitted(object, units, ...) :
Number of units must equal number of data.frame columns
> df[,'d'] <- FALSE
Error in unitted(object, units, ...) :
Number of units must equal number of data.frame columns
> df[['d']] <- TRUE
package-unitted.R (or unitted-package.R?) should contain overview of functions the user might want to use, e.g., u, v, get_units, and verify_units
I wanted to let the user create new unitted classes at will, but neither S3 nor S4 classes are unittable with new_unitted_class
when unitted is loaded as a package rather than in dev mode. These examples were in @examples
section of new_unitted_class
, before I took them out and made that function not-exported:
#' ## S4 ##
#'
#' # "myS4flower": An S4 class to be made unitted
#' setClass("myS4flower", contains="numeric", slots=c(leaves="numeric", petals="numeric", color="character"))
#'
#' # Define the unitted version of myS4flower
#' new_unitted_class("myS4flower")
#'
#' # Try out the new unitted_myS4flower class
#' petunia <- new("unitted_myS4flower", c(frontyard=7, backyard=3, planters=12), leaves=4, petals=5, color="purple", units="stems ft^-2")
#' petunia * u(1/2,"blooming")
#' ## S3 ##
#'
#' # "quote": An S3 class to be made unitted
#' two_cities <- c("It was the best of times, it was the worst of times, ",
#' "it was the age of wisdom, it was the age of foolishness, ",
#' "it was the epoch of belief, it was the epoch of incredulity, ",
#' "it was the season of Light, it was the season of Darkness, ",
#' "it was the spring of hope, it was the winter of despair...")
#' class(two_cities) <- "quote"
#' attr(two_cities, "author") <- "Charles Dickens"
#' print.quote <- function(x) { cat(paste(paste(x, collapse="\n"),"\n - ",attr(x,"author"))) }
#' print(two_cities)
#'
#' # Now make it unittable
#' setClass("quote", contains="character", slots=c(author="character"))
#'
#' # And now make it unitted
#' new_unitted_class("quote", overwrite=TRUE)
#' two_cities_unitted <- new("unitted_quote",
#' two_cities, author=attr(two_cities, "author"), units="lines")
#' see_spot_run_unitted <- new("unitted_quote",
#' c("Here is Spot.","See Spot run.","Run, Spot, Run!"), author="unknown", units="sentences")
#' print(two_cities_unitted)
#' print(see_spot_run_unitted)
#' sum(!is.na(see_spot_run_unitted))
https://ci.appveyor.com/project/appling/unitted/build/1.0.3#L231
** preparing package for lazy loading
Error in makePrototypeFromClassDef(properties, ClassDef, immediate, where) :
in constructing the prototype for class "unitted_ordered": prototype has class "S4", but the data part specifies class "integer"
Error : unable to load R code in package 'unitted'
ERROR: lazy loading failed for package 'unitted'
readr
?
jordan's excellent suggestion: "Possibly turn your vignette into an html vignette? I found the ppt style to be kind of tricky to take in. Laura has a good one for EGRETci if you want to take a look"
In this function in arithmetic.R:
setMethod(
"unitted_Ops", c("ANY","ANY"), ...
a call to udf1 + udf2
breaks with non-numeric argument to binary operator
because the code is trying to call the operator (e.g., +
) on two lists of unitbundles here:
return(switch(
.Generic,
"+"=, "-"=, "*"=, "/"=, "%%"=, "%/%"= {
unitted(do.call(.Generic, list(deunitted(e1), deunitted(e2))),
do.call(.Generic, list(get_unitbundles(e1), get_unitbundles(e2)))) }, ...
That's not right. There should probably be an mapply
in there.
mean, sqrt, abs, min, max, ...
?Summary: "max", "min", "range", "prod", "sum", "any", "all"
?log: log, logb, log10, log2, log1p, exp, expm1
?Trig: sin, cos, ...
?mean: weighted.mean, colMeans
?Math: abs, sign, sqrt, ceiling, floor, trunc, cummax, cummin, cumprod, ...
?Math2: round, signif
?Complex: Arg, Conj, Im, Mod, Re
Alison, how do I format my units so they're ready for use as expressions on plot axes?
unitted dfs apparently don't work with chron
cana$dtime<-chron( as.numeric(cana$DateTime)/86400-0.25)
Error in (function (classes, fdef, mtable) :
unable to find an inherited method for function ‘unitted’ for signature ‘"chron"’
test_that("Known bug: sqrt doesn't get applied to elements of a unitted vector", {
miles_vec <- unitted(1:5,"mi")
-miles_vec
#sqrt seems not to get applied to elements of a unitted vector
x <- u(data.frame(a=1:3,b=3:5),c("a","q"))
expect_equal(v(sqrt(x)), sqrt(v(x)))
})
ugh to all of this:
library(dplyr)
library(unitted)
> tbl <- u(data_frame(a=1:4, b=5:8), c('A','B'))
> tbl
Source: local data frame [4 x 2]
a b
U A B
(dbl) (dbl)
1 1 5
2 2 6
3 3 7
4 4 8
> tbl$a
Error in unitbundle(units) :
invalid class for units specification - must be data.frame, character, NA, unitbundle, or missing
> tbl['a']
Error in unitted(vx, new_units) :
Number of units must equal number of data.frame columns
> tbl[,'a']
Error in unitted(vx, new_units) :
Number of units must equal number of data.frame columns
tibble + unitted = not yet harmony.
> library(unitted)
> library(tibble)
Attaching package: ‘tibble’
The following object is masked from ‘package:unitted’:
as_data_frame
> x <- tibble(x=1:3, y=2:4)
Found more than one class "tbl_df" in cache; using the first, from namespace 'tibble'
Found more than one class "tbl_df" in cache; using the first, from namespace 'tibble'
Found more than one class "tbl_df" in cache; using the first, from namespace 'tibble'
Found more than one class "tbl_df" in cache; using the first, from namespace 'tibble'
Found more than one class "tbl_df" in cache; using the first, from namespace 'tibble'
Found more than one class "tbl_df" in cache; using the first, from namespace 'tibble'
Found more than one class "tbl_df" in cache; using the first, from namespace 'tibble'
> sessionInfo()
R version 3.3.1 (2016-06-21)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1
locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] tibble_1.1 unitted_0.2.5
loaded via a namespace (and not attached):
[1] lazyeval_0.2.0 magrittr_1.5 R6_2.1.2 assertthat_0.1 plyr_1.8.4 rsconnect_0.4.3
[7] DBI_0.4-1 tools_3.3.1 dplyr_0.5.0 Rcpp_0.12.5
plot(gpp.disch$disch, gpp.disch$gpp, log='x')
Error in unitted_Ops(.Generic, e1, unitted(e2, NA)) :
Units mismatch in comparison 'e1 (ft^3 s^-1) <= e2 ()'
units of disch and gpp are different, yes. But no error for plot(gpp.disch$disch, gpp.disch$gpp, log='')
verify_units(u(as.POSIXct('2017-01-19'), ''), '')
gives
Error in verify_units(u(as.POSIXct("2017-01-19"), ""), "") :
Unrecognized data type in unitted object
Error in makePrototypeFromClassDef(properties, ClassDef, immediate, where) :
in constructing the prototype for class "unitted_ordered": prototype has class "S4", but the data part specifies class "integer"
Error : unable to load R code in package 'unitted'
ERROR: lazy loading failed for package 'unitted'
https://ci.appveyor.com/project/jread-usgs/mda-streams/build/0.1.101#L205
I tried to do this in another function:
#' @import dplyr
#' @importFrom unitted mutate_.unitted_data.frame
...
x <- function() {
...
y <- u(data.frame(i=1:4, j="k"), c("ii","jj"))
site_names <- c("A","B","C")
y < - mutate(y, h=site_names)
...
}
but that didn't work because the version of mutate that got called was all dplyr, no unitted.
Warning messages:
1: In class(x) <- c("tbl_df", "tbl", "data.frame") :
Setting class(x) to multiple strings ("tbl_df", "tbl", ...); result will no longer be an S4 object
So then I tried to specify the unitted version
#' @import dplyr
#' @importFrom unitted mutate_.unitted_data.frame
...
x <- function() {
...
y <- u(data.frame(i=1:4, j="k"), c("ii","jj"))
site_names <- c("A","B","C")
y < - mutate_.unitted_data.frame(y, h=site_names)
...
}
but the problem here is that site_names is a local variable, and it doesn't get properly evaluated in mutate_.
Error in unitted(object, units, ...) :
error in evaluating the argument 'object' in selecting a method for function 'unitted': Error in unitted(object, units, ...) : binding not found: 'A'
Similarly, if you change the mutate line to this:
y < - mutate_.unitted_data.frame(y, h="site_names")
you get this:
Error in unitted(object, units, ...) :
error in evaluating the argument 'object' in selecting a method for function 'unitted': Error in unitted(object, units, ...) : binding not found: 'site_names'
We need mutate() without the _, and we need to be able to specify that we want the unitted version.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.