Skip to content

Commit

Permalink
Merge pull request #22 from pitakakariki/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
pitakakariki committed Oct 11, 2015
2 parents e0de062 + 0e05d37 commit 6c19bad
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 4 deletions.
9 changes: 8 additions & 1 deletion R/getData.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,14 @@ getData <- function(object) {
if(!is.null(newData)) return(newData)

#
# 2nd choice: Use the `data` argument
# 2nd choice: @frame for merMod, $model for lm.
#

if(is(object, "merMod")) return(object@frame)
if(is(object, "lm")) return(object$model)

#
# 3rd choice: Use the `data` argument
#

dataName <- as.character(getCall(object)$data)
Expand Down
59 changes: 59 additions & 0 deletions R/maybe.R
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,65 @@ maybe <- function(f) {
}
}

maybe2 <- function(f) {

cfw <- "(converted from warning)"

function(...) {

opts <- options(warn=2)
on.exit(options(opts))

returnValue <- NULL
warningValue <- NULL
warningTag <- NULL
errorValue <- NULL
errorTag <- NULL

returnValue <- tryCatch(

withCallingHandlers(eval.parent(f(...)),

error=function(e) {

if(str_detect(e$message, cfw)) {

# get rid of conversions junk
msg <- e$message
msg <- substring(msg, 24+str_locate(msg, cfw)[1,1])
msg <- str_trim(msg)

warningValue <<- append(warningValue, msg)
wtag <- if(is.null(e$tag)) "" else e$tag
warningTag <<- append(warningTag, wtag)
message(msg)
print(computeRestarts())
invokeRestart("muffleWarning")

} else {

errorValue <<- e$message
errorTag <<- if(is.null(e$tag)) "" else e$tag
return(NULL)
}
}
)
)

rval <- list()
class(rval) <- "Maybe"

rval["value"] <- list(returnValue) # nb returnValue might be NULL
rval["warning"] <- list(warningValue)
rval["warningtag"] <- list(warningTag)
rval["error"] <- list(errorValue)
rval["errortag"] <- list(errorTag)

return(rval)
}
}


list2maybe <- function(x) {

rval <- list()
Expand Down
8 changes: 6 additions & 2 deletions R/testLibrary.R
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ fixeddesc <- function(text, xname) {
function(fit, sim) {

# test used
rval <- if(text=="default") defaultdesc(fit) else text
rval <- if(text=="default") defaultdesc(fit, xname) else text

# effect size
fe <- maybe(fixef)(sim)$value
Expand All @@ -122,6 +122,8 @@ fixeddesc <- function(text, xname) {
# glmer - ztest
defaulttest <- function(fit, xname) {

if(is.factor(getData(fit)[[xname]])) return(lrtest(fit, xname))

switch(class(fit)[1],

lm = ttest(fit, xname),
Expand All @@ -132,7 +134,9 @@ defaulttest <- function(fit, xname) {
)
}

defaultdesc <- function(fit) {
defaultdesc <- function(fit, xname) {

if(is.factor(getData(fit)[[xname]])) return("Likelihood ratio")

switch(class(fit)[1],

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ helperopts <- simrOptions(nsim=test.nsim, progress=test.progress)
# Useful to have an example model.
#

fm1 <- lmer(y ~ x + (1|g), data=example)
fm1 <- lmer(y ~ x + (1|g), data=simdata)

0 comments on commit 6c19bad

Please sign in to comment.