diff --git a/R/datasets.R b/R/datasets.R index f631674..a766a89 100644 --- a/R/datasets.R +++ b/R/datasets.R @@ -228,3 +228,43 @@ delete.datasets <- function(ws, name, host){ refresh(ws, "datasets") ans } + +#' Update an existing R data frame in an AzureML workspace. +#' +#' Update an existing R data frame to an AzureML workspace using the \code{GenericTSV} format. +#' +#' @inheritParams refresh +#' @param x An R data frame object +#' @param name A character name for an existing AzureML dataset +#' @param description An optional character description of the dataset, description for the existing dataset will be used if none provided here +#' @param family_id An optional AzureML family identifier, family identifier for the existing dataset will be used if none provided here +#' @param ... Optional additional options passed to \code{write.table} +#' @note The additional \code{\link[utils]{write.table}} options may not include \code{sep} or \code{row.names} or \code{file}, but any other options are accepted. +#' +#' @return A single-row data frame of "Datasets" class that corresponds to the updated object now available in ws$datasets. +#' @importFrom curl curl_escape new_handle handle_setheaders handle_reset handle_setopt curl_fetch_memory +#' @importFrom jsonlite fromJSON +#' @export +#' @family dataset functions +#' @example inst/examples/example_update.R + +update.dataset <- function(x, ws, name, description = "", family_id = "", ...) +{ + + # take care of exceptions from delete: it's Microsoft dataset or dataset does not exist + if (name %in% datasets(ws, filter = "samples")$Name) stop("Update not allowed for Microsoft sample datasets.") + else if (!(name %in% datasets(ws, filter = "my datasets")$Name)) stop("The dataset named '", name, "' does not exist.") + + # check to make sure the existing file is in GenericTSV format + if (tolower(ws$datasets$DataTypeId[ws$datasets$Name == name]) != "generictsv") stop("The existing dataset is not of type GenericTSV.") + + # use description and family_id from the existing dataset if no new values are provided + if (description == "") description = ws$datasets$Description[ws$datasets$Name == name] + if (family_id == "") family_id = ws$datasets$FamilyId[ws$datasets$Name == name] + + # delete existing data + dr <- delete.datasets(ws, name) + + # upload + upload.dataset(x, ws, name, description, family_id) +} diff --git a/inst/examples/example_update.R b/inst/examples/example_update.R new file mode 100644 index 0000000..2a8dc13 --- /dev/null +++ b/inst/examples/example_update.R @@ -0,0 +1,17 @@ +\dontrun{ + library(AzureML) + + ws <- workspace() + + # Upload the R airquality data.frame to the workspace. + upload.dataset(airquality, ws, "airquality") + + # Make an update by using only the first 10 rows + update.dataset(airquality[1:10, ], ws, "airquality") + + # Download the updated dataset to check its content + download.datasets(ws, name = "airquality") + + # Now delete what we've just uploaded + delete.datasets(ws, "airquality") +}