私は何かを思いついた。
与えられた例の予想出力はこれを実行することによって得られるであろう:ここ
con <- ROracle::dbConnect(drv, username, password, dbname) # your connection parameters
dplyr::tbl(con,"orders") %>%
extract_o("Month","order_date",append = FALSE,force_upper_case = FALSE)
関数のコードであり、Iは大文字列(デフォルト)を強制するために、既存のものに新しい列を追加するいくつかのパラメータが含まれ(デフォルト)。新しい列の名前を定義するか、デフォルトでは抽出する値の型として名前が付けられます。
#' use Oracle EXTRACT function
#'
#' Will add a column to the table, containing extracted value,
#' optionally returns only this column
#' @param data tbl_lazy object
#' @param what type of data to extract
#' @param from column to extract from
#' @param new_col name of new column
#' @param append keep existing columns,
#' FALSE ditches them and keep only extracted column
#' @param force_upper_case make new column name uppercase
extract_o <-function(data, what, from, new_col = what,
append = TRUE,force_upper_case = TRUE) {
allowed <- c("day","month","year","hour","minute","second",
"timezone_hour","timezone_minute",
"timezone_region","timezone_abbr")
assertthat::assert_that(
tolower(what) %in% allowed,
msg=paste("Choose 'what' among",
paste0("'",allowed,"'",collapse=", ")))
if(force_upper_case) new_col <- toupper(new_col)
tbl_query <- as.character(dbplyr::sql_render(data)) # previous query
append_sql <- if(append)
paste0(paste(colnames(data),collapse=", "),", ") else ""
query <- paste0("SELECT ", append_sql, # initial cols or none
"EXTRACT(",what," FROM ",from,") \"",new_col, # new col
"\" FROM (",tbl_query,")") # previous query
dplyr::tbl(data$src$con,sql(query))
}