Skip to content

Commit

Permalink
Updated _select_by_criteria() to be more readable
Browse files Browse the repository at this point in the history
Specific changes here: #13 (comment)
  • Loading branch information
bochocki committed Mar 12, 2017
1 parent 031ec53 commit ed8e67d
Showing 1 changed file with 34 additions and 14 deletions.
48 changes: 34 additions & 14 deletions R/browse.R
Original file line number Diff line number Diff line change
Expand Up @@ -191,25 +191,26 @@ key_arg <- function(x,keyword,criteria){

}


# Summarizing function
# function to subset dataframe by criteria and do error checking
select_by_criteria <- function(x,criteria){

if(!is.null(criteria)) {
r <- which(eval(criteria, x, parent.frame()))
if( length(r) != 0 ) {
subsetDat <- tbl_df(x[r,,drop=F]) #tbl_df() to make object "work" with dplyr functions
}
if( length(r) == 0 ) {
stop( "No matches found. Either:
1. the name of variable(s) you specified is/are incorrect or
2. the values you are looking for are not contained in the variable(s) you specified")
}
# if criteria are specified, subset the dataframe accordingly
out <- subset(x,eval(criteria))

} else {
subsetDat <- tbl_df(x)
# if no criteria are specified, do nothing
out <- x
}
return(subsetDat)

# if no results are returned, return an error
if( nrow(out) == 0 ) {
stop( "No matches found. Either:
1. the name of variable(s) you specified is/are incorrect or
2. the values you are looking for are not contained in the variable(s) you specified")
}

return(tbl_df(out))
}


Expand Down Expand Up @@ -436,7 +437,6 @@ table_select <- function(x, full_tbl = FALSE, possible_args){

}


# changes clss to class and ordr to order
class_order_names <- function(x){

Expand All @@ -445,4 +445,24 @@ class_order_names <- function(x){

return(x)

}

# Summarizing function
select_by_criteria_OLD <- function(x,criteria){

if(!is.null(criteria)) {
r <- which(eval(criteria, x, parent.frame()))
if( length(r) != 0 ) {
subsetDat <- tbl_df(x[r,,drop=F]) #tbl_df() to make object "work" with dplyr functions
}
if( length(r) == 0 ) {
stop( "No matches found. Either:
1. the name of variable(s) you specified is/are incorrect or
2. the values you are looking for are not contained in the variable(s) you specified")
}
} else {
subsetDat <- tbl_df(x)
}
return(subsetDat)

}

0 comments on commit ed8e67d

Please sign in to comment.