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

Added 'typecast' option for insert and update record functions #17

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 21 additions & 14 deletions R/airtabler.R
Original file line number Diff line number Diff line change
Expand Up @@ -280,15 +280,18 @@ air_parse <- function(res) {
#' @param table_name Table name
#' @param record_data Named list of values. You can include all, some, or none
#' of the field values
#' @param typecast Boolean. Whether to allow Airtable to create new
#' categorical variables if they do not already exist in the table.
#' @export
air_insert <- function(base, table_name, record_data) {
air_insert <- function(base, table_name, record_data, typecast = FALSE) {

if( inherits(record_data, "data.frame")) {
return( air_insert_data_frame(base, table_name, record_data))
return( air_insert_data_frame(base, table_name, record_data, typecast))
}

record_data <- air_prepare_record(as.list(record_data))
json_record_data <- jsonlite::toJSON(list(fields = record_data))
json_record_data <- jsonlite::toJSON(list(fields = record_data,
typecast = jsonlite::unbox(typecast)))

request_url <- sprintf("%s/%s/%s", air_url, base, table_name)

Expand All @@ -306,22 +309,23 @@ air_insert <- function(base, table_name, record_data) {
air_parse(res) # returns R object
}

air_insert_data_frame <- function(base, table_name, records) {
air_insert_data_frame <- function(base, table_name, records, typecast) {
lapply(seq_len(nrow(records)), function(i) {
record_data <-
as.list(records[i,])
air_insert(base = base, table_name = table_name, record_data = record_data)
air_insert(base = base, table_name = table_name, record_data = record_data, typecast = typecast)
})
}

air_update_data_frame <- function(base, table_name, record_ids, records) {
air_update_data_frame <- function(base, table_name, record_ids, records, typecast) {
lapply(seq_len(nrow(records)), function(i) {
record_data <-
unlist(as.list(records[i,]), recursive = FALSE)
air_update(base = base,
table_name = table_name,
record_id = ifelse(is.null(record_ids), record_data$id, record_ids[i]),
record_data = record_data)
record_data = record_data,
typecast = typecast)
})
}

Expand Down Expand Up @@ -395,14 +399,17 @@ air_delete_vec <- Vectorize(air_delete, vectorize.args = "record_id", SIMPLIFY =
#' @param record_id An id of the record
#' @param record_data Named list of values. You can include all, some, or none
#' of the field values
#' @param typecast Boolean. Whether to allow Airtable to create new
#' categorical variables if they do not already exist in the table.
#' @export
air_update <- function(base, table_name, record_id, record_data) {
air_update <- function(base, table_name, record_id, record_data, typecast = FALSE) {

if(inherits(record_data, "data.frame")) {
return(air_update_data_frame(base, table_name, record_id, record_data))
return(air_update_data_frame(base, table_name, record_id, record_data, typecast))
}
record_data <- air_prepare_record(record_data)
json_record_data <- jsonlite::toJSON(list(fields = record_data))
json_record_data <- jsonlite::toJSON(list(fields = record_data,
typecast = jsonlite::unbox(typecast)))

request_url <- sprintf("%s/%s/%s/%s", air_url, base, table_name, record_id)

Expand Down Expand Up @@ -515,12 +522,12 @@ air_table_funs <- function(base, table_name) {
air_get(base, table_name, record_id, limit, offset, view, sortField, sortDirection, combined_result)
}
res_list[["insert"]] <-
function(record_data) {
air_insert(base, table_name, record_data)
function(record_data, typecast = FALSE) {
air_insert(base, table_name, record_data, typecast)
}
res_list[["update"]] <-
function(record_id, record_data) {
air_update(base, table_name, record_id, record_data)
function(record_id, record_data, typecast = FALSE) {
air_update(base, table_name, record_id, record_data, typecast)
}
res_list[["delete"]] <-
function(record_id) {
Expand Down
18 changes: 18 additions & 0 deletions readme.rmd
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,24 @@ cat("Updated a record with ID=", new_hotel$id, ". ",
"New price: ", new_hotel$fields$`Price/night`, sep = "")
```

### Typecasting

When creating or updating records, if the choice string does not exactly match an existing option, the request will fail with an INVALID_MULTIPLE_CHOICE_OPTIONS error unless the typecast parameter is enabled. With both Insert and Update, you can set `typecast = TRUE`, and a new choice will be created if one does not exactly match.

```{r insert}
record_data <- list(
Name = "Newest hotel",
`Price/night` = 300,
Stars = "***",
Amenities = c("WiFi") # does not exist as an option in table yet
)

new_hotel <-
TravelBucketList$Hotels$insert(record_data)

cat("Inserted a record with ID=", new_hotel$id, sep = "")
```

### Delete a record
```{r delete}
TravelBucketList$Hotels$delete(new_hotel$id)
Expand Down