Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update select_by_criteria function to be more readable #13

Open
bibsian opened this issue Mar 3, 2017 · 2 comments
Open

Update select_by_criteria function to be more readable #13

bibsian opened this issue Mar 3, 2017 · 2 comments

Comments

@bibsian
Copy link
Collaborator

bibsian commented Mar 3, 2017

Change the sytanx to make it more apparent that select_by_criteria() is just a wrapper for subset() that works with lazy eval calls and does error handling.

@bochocki
Copy link
Contributor

bochocki commented Mar 12, 2017

made the following changes, and checked that results are identical:

# function to subset dataframe by criteria and do error checking
select_by_criteria <- function(x,criteria){
  
  if(!is.null(criteria)) {
    # if criteria are specified, subset the dataframe accordingly
    out <- subset(x,eval(criteria))
    
  } else { 
    # if no criteria are specified, do nothing
    out <- x
  }
  
  # 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))
}
> new <- browse(structure == "size", full_tbl = F)
> identical(old,new)
[1] TRUE

Moved the old function to the UNUSED FUNCTIONS section

bochocki added a commit that referenced this issue Mar 12, 2017
@bochocki
Copy link
Contributor

suggest closing this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants