#' Download a single opal dataset as tibble
#'
#' @param username opal username as string
#' @param password opal password as string (do not store in any script!)
#' @param table opal tablename as string
#' @param version_suffix suffix refering to the db version i.e. 'table' will be
#' downloaded from 'CENTER-TBIversion_suffix'
#'
#' @value a tibble
#'
#' @export
download <- function(username, password, table, version_suffix = "_versionCurrent") {
require(opal)
o <- opal.login(username, password, url = 'https://uda.wbic.cam.ac.uk:9443')
tryCatch({
opal.assign(o, 'query', sprintf('CENTER-TBI%s.%s', version_suffix, table), id.name = "ID")
df <- opal.execute(o, 'query') %>% as_tibble()
}, error = function(e) {opal.logout(o); print(e)}
)
opal.logout(o)
return(df)
}
#' Download entire CENTER-TBI version
#'
#' @param username opal username as string
#' @param password opal password as string (do not store in any script!)
#' @param outdir (relative) path to store datasets in (as .rds per table)
#' @param version_suffix suffix refering to the db version i.e. tables will be
#' downloaded from 'CENTER-TBIversion_suffix'
#'#'
#' @export
save_all <- function(username, password, outdir, version_suffix = "_versionCurrent") {
require(opal)
dir.create(outdir, showWarnings = FALSE)
db_name = sprintf('CENTER-TBI%s', version_suffix)
o <- opal.login(username, password, url = 'https://uda.wbic.cam.ac.uk:9443')
tables <- tryCatch({
opal.tables(o, db_name)
}, error = function(e) {opal.logout(o); print(e)}
)
opal.logout(o)
for (tbl in tables) {
df_tbl <- download(username, password, tbl$name, version_suffix = version_suffix)
saveRDS(df_tbl, file = sprintf("%s/df_%s.rds", outdir, tbl$name))
}
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.