Skip to content

Commit

Permalink
Suggest OK pk_var in flatten_pv_data
Browse files Browse the repository at this point in the history
  • Loading branch information
crew102 committed May 25, 2017
1 parent a4698da commit 0c4bbb9
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions R/flatten-pv-data.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
get_ok_pk <- function(endpoint) {
ifelse(
endpoint %in% c('uspc_mainclasses', 'nber_subcategories'),
gsub("i?es$", "_id", endpoint),
gsub("s$", "_id", endpoint)
)
}

#' Flatten PatentsView Data
#'
#' This function converts a single data frame that has subentity-level list columns in it into multiple data frames, one for each entity/subentity type. The multiple data frames can be merged together using the primary key variable specified by the user.
Expand Down Expand Up @@ -27,11 +35,13 @@ flatten_pv_data <- function(data, pk_var) {
sub_ent_df <- df[, !prim_ent_var, drop = FALSE]
sub_ents <- colnames(sub_ent_df)

ok_pk <- get_ok_pk(endpoint = names(data))

out_sub_ent <- sapply(sub_ents, FUN = function(x) {
temp <- sub_ent_df[[x]]
asrt(length(unique(df[, pk_var])) == length(temp),
pk_var, " cannot act as a primary key")

pk_var, " cannot act as a primary key because it is not a ",
"unique identifier.\n\nTry using ", ok_pk, " instead.")
names(temp) <- df[, pk_var]
xn <- do.call("rbind", temp)
xn[, pk_var] <- gsub("\\.[0-9]*$", "", rownames(xn))
Expand Down

0 comments on commit 0c4bbb9

Please sign in to comment.