ropensci / popler

The R package to browse and query the popler database
https://docs.ropensci.org/popler
MIT License
8 stars 7 forks source link

Update select_by_criteria function to be more readable #13

Open bibsian opened 7 years ago

bibsian commented 7 years ago

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 commented 7 years ago

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 commented 7 years ago

suggest closing this issue.