123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276 |
- # convert string vectors to single quote-marked string sep by comma
- quote_str = function (x) paste0("\'", paste(x, collapse = "','"), "\'")
- squote = function(x){
- gsub("\"", "\'", x)
- }
- # get directory of the file
- file_dir = function (x) dirname(normalizePath(x))
- # get file name with extension
- file_name = function (x) basename(normalizePath(x))
- # get file name without extension
- sans_ext = tools::file_path_sans_ext
- file_name_sans = function (x) basename(sans_ext(normalizePath(x)))
- # generate uuid for Docker container names
- # derived from http://stackoverflow.com/questions/10492817/
- uuid = function () {
- id = paste(sample(c(letters[1:6], 0:9), 30, replace = TRUE),
- collapse = '')
- paste(substr(id, 1, 8), '_', substr(id, 9, 12), '_', '4',
- substr(id, 13, 15), '_', sample(c('8', '9', 'a', 'b'), 1),
- substr(id, 16, 18), '_', substr(id, 19, 30), sep = '',
- collapse = '')
- }
- #' check if from Bioconductor base images
- #' @importFrom stringr str_trim
- #' @noRd
- is_from_bioc = function (x) substr(str_trim(x), 1L, 13L) == 'bioconductor/'
- #' check if from the rocker/rstudio base image
- #' @importFrom stringr str_trim
- #' @noRd
- is_from_rstudio = function (x) substr(str_trim(x), 1L, 14L) == 'rocker/rstudio'
- deType <- function(x){
- ## string
- str_type <- c('STRING', 'STR', '<string>', '<str>', 'str', "character",
- "string", "String")
- ## int
- int_type <- c('INTEGER', 'INT', '<integer>', '<int>', 'int',
- "integer", "Integer")
- ## float
- float_type <- c('FLOAT', '<float>', 'float', 'Float', 'numeric')
- ## File
- file_type <- c('FILE', '<file>', 'File', 'file')
- ## enum
- enum_type <- c('ENUM', '<enum>', 'enum', "Enum")
- ## boolean
- boolean_type <- c('BOOLEAN', '<boolean>', 'boolean', "Boolean", "logical", "logic", "Logical")
- .array <- FALSE
- if(is.character(x)){
- res <- ""
- if(grepl("\\.\\.\\.", x)){
- .array <- TRUE
- x <- gsub("[^[:alnum:]]", "", x)
- }
- if(x %in% str_type){
- res <- "string"
- }else if(x %in% int_type){
- res <- "int"
- }else if(x %in% float_type){
- res <- "float"
- }else if(x %in% file_type){
- res <- "File"
- }else if(x %in% enum_type){
- res <- "enum"
- }else if(x %in% boolean_type){
- res <- "boolean"
- }else{
- res <- x
- }
- if(.array){
- res <- ItemArray(res)
- }
- }else{
- res <- x
- }
- res
- }
- ## copied from roxygen2
- rdarg <- function(topic, dots = FALSE){
- internal_f <- function(p, f) {
- stopifnot(is.character(p), length(p) == 1)
- stopifnot(is.character(f), length(f) == 1)
- get(f, envir = asNamespace(p))
- }
- get_rd <- function(topic, package = NULL) {
- help_call <- substitute(help(t, p), list(t = topic, p = package))
- top <- eval(help_call)
- if (length(top) == 0) return(NULL)
- internal_f("utils", ".getHelpFile")(top)
- }
- # get_rd should parse Rd into a rd_file so I don't need to maintain
- # two parallel apis
- get_tags <- function(rd, tag) {
- rd_tag <- function(x) attr(x, "Rd_tag")
- Filter(function(x) rd_tag(x) == tag, rd)
- }
- rd2rd <- function(x) {
- chr <- internal_f("tools", "as.character.Rd")(x)
- paste(unlist(chr), collapse = "")
- }
- # rd_arguments(get_rd("mean"))
- rd_arguments <- function(rd) {
- arguments <- get_tags(rd, "\\arguments")[[1]]
- items <- get_tags(arguments, "\\item")
- values <- lapply(items, function(x) rd2rd(x[[2]]))
- params <- vapply(items, function(x) rd2rd(x[[1]]), character(1))
- setNames(values, params)
- }
- res = rd_arguments(get_rd(topic))
- if(!dots){
- res = res[names(res) != "\\dots"]
- }
- res = sapply(res,
- function(x){
- x = gsub("\n" , "", x)
- x = gsub("\\\\", "", x)
- })
- nms = names(formals(topic))
- nms = setdiff(nms, "...")
- res = split_arg(res)
- res[names(res) %in% nms]
- }
- split_arg = function(x){
- .arg = c()
- for(nm in names(x)){
- if(grepl(",", nm)){
- nms = strsplit(nm, split = ",")[[1]]
- nms = gsub("^\\s+|\\s+$", "", nms)
- res = rep(x[nm], length(nms))
- names(res) = nms
- }else{
- res = x[nm]
- }
- .arg = c(.arg, res)
- }
- .arg
- }
- # get type from a input file
- get_type = function(input){
- if(!is.na(file.info(input)$isdir) && file.info(input)$isdir){
- if(is_shinyapp(input)){
- return("shinyapp")
- }else{
- return(NULL)
- }
- }else{
- ## treat as file
- if (!file.exists(input)){
- stop('input file or shiny app folder does not exist')
- }
- }
- .run = liftr:::parse_rmd(input)$runtime
- if(!is.null(.run) && .run == "shiny"){
- return("shinydoc")
- }else{
- return("rmd")
- }
- }
- is_shinydoc = function(input){
- get_type(input) == "shinydoc"
- }
- is_shinyapp = function(input){
- if(!is.na(file.info(input)$isdir) && file.info(input)$isdir){
- fls = list.files(input)
- res = "app.R" %in% fls | ("server.R" %in% fls && "ui.R" %in% fls)
- return(res)
- }else{
- return(FALSE)
- }
- }
- render_engine = function(input){
- switch(get_type(input),
- "rmd" = "render",
- "shinydoc" = "run",
- NULL)
- }
- .showFields <- function(x, title = NULL, values = NULL, full = FALSE){
- if (missing(values)){
- flds = names(x$getRefClass()$fields())
- }else{
- flds = values
- }
- if(!length(x))
- return(NULL)
- if(!full){
- idx <- sapply(flds, is.null)
- if(!is.null(title) && !all(idx)){
- message(title)
- }
- ## ugly, change later
- for (fld in flds[!idx]){
- if(is.list(x[[fld]])){
- if(length(x[[fld]])){
- message(fld, ":")
- .showList(x[[fld]], space = " ")
- }
- }else if(is(x[[fld]], "Item")){
- x[[fld]]$show()
- }else{
- if(is.character(x[[fld]])){
- if(x[[fld]] != "" && length(x[[fld]])){
- message(fld, " : ", paste0(x[[fld]], collapse = " "))
- }
- }else{
- if(!is.null(x[[fld]]) && length(x[[fld]]))
- message(fld, " : ", x[[fld]])
- }
- }
- }
- }else{
- message(title)
- ## ugly, change later
- for (fld in flds){
- if(is.list(x[[fld]])){
- message(fld, ":")
- .showList(x[[fld]], space = " ", full = full)
- }else if(is(x[[fld]], "Item")){
- x[[fld]]$show()
- }else{
- if(is.character(x[[fld]])){
- message(fld, " : ", paste0(x[[fld]], collapse = " "))
- }else{
- message(fld, " : ", x[[fld]])
- }
- }
- }
- }
- }
|