utils.R 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. # convert string vectors to single quote-marked string sep by comma
  2. quote_str = function (x) paste0("\'", paste(x, collapse = "','"), "\'")
  3. squote = function(x){
  4. gsub("\"", "\'", x)
  5. }
  6. # get directory of the file
  7. file_dir = function (x) dirname(normalizePath(x))
  8. # get file name with extension
  9. file_name = function (x) basename(normalizePath(x))
  10. # get file name without extension
  11. sans_ext = tools::file_path_sans_ext
  12. file_name_sans = function (x) basename(sans_ext(normalizePath(x)))
  13. # generate uuid for Docker container names
  14. # derived from http://stackoverflow.com/questions/10492817/
  15. uuid = function () {
  16. id = paste(sample(c(letters[1:6], 0:9), 30, replace = TRUE),
  17. collapse = '')
  18. paste(substr(id, 1, 8), '_', substr(id, 9, 12), '_', '4',
  19. substr(id, 13, 15), '_', sample(c('8', '9', 'a', 'b'), 1),
  20. substr(id, 16, 18), '_', substr(id, 19, 30), sep = '',
  21. collapse = '')
  22. }
  23. #' check if from Bioconductor base images
  24. #' @importFrom stringr str_trim
  25. #' @noRd
  26. is_from_bioc = function (x) substr(str_trim(x), 1L, 13L) == 'bioconductor/'
  27. #' check if from the rocker/rstudio base image
  28. #' @importFrom stringr str_trim
  29. #' @noRd
  30. is_from_rstudio = function (x) substr(str_trim(x), 1L, 14L) == 'rocker/rstudio'
  31. deType <- function(x){
  32. ## string
  33. str_type <- c('STRING', 'STR', '<string>', '<str>', 'str', "character",
  34. "string", "String")
  35. ## int
  36. int_type <- c('INTEGER', 'INT', '<integer>', '<int>', 'int',
  37. "integer", "Integer")
  38. ## float
  39. float_type <- c('FLOAT', '<float>', 'float', 'Float', 'numeric')
  40. ## File
  41. file_type <- c('FILE', '<file>', 'File', 'file')
  42. ## enum
  43. enum_type <- c('ENUM', '<enum>', 'enum', "Enum")
  44. ## boolean
  45. boolean_type <- c('BOOLEAN', '<boolean>', 'boolean', "Boolean", "logical", "logic", "Logical")
  46. .array <- FALSE
  47. if(is.character(x)){
  48. res <- ""
  49. if(grepl("\\.\\.\\.", x)){
  50. .array <- TRUE
  51. x <- gsub("[^[:alnum:]]", "", x)
  52. }
  53. if(x %in% str_type){
  54. res <- "string"
  55. }else if(x %in% int_type){
  56. res <- "int"
  57. }else if(x %in% float_type){
  58. res <- "float"
  59. }else if(x %in% file_type){
  60. res <- "File"
  61. }else if(x %in% enum_type){
  62. res <- "enum"
  63. }else if(x %in% boolean_type){
  64. res <- "boolean"
  65. }else{
  66. res <- x
  67. }
  68. if(.array){
  69. res <- ItemArray(res)
  70. }
  71. }else{
  72. res <- x
  73. }
  74. res
  75. }
  76. ## copied from roxygen2
  77. rdarg <- function(topic, dots = FALSE){
  78. internal_f <- function(p, f) {
  79. stopifnot(is.character(p), length(p) == 1)
  80. stopifnot(is.character(f), length(f) == 1)
  81. get(f, envir = asNamespace(p))
  82. }
  83. get_rd <- function(topic, package = NULL) {
  84. help_call <- substitute(help(t, p), list(t = topic, p = package))
  85. top <- eval(help_call)
  86. if (length(top) == 0) return(NULL)
  87. internal_f("utils", ".getHelpFile")(top)
  88. }
  89. # get_rd should parse Rd into a rd_file so I don't need to maintain
  90. # two parallel apis
  91. get_tags <- function(rd, tag) {
  92. rd_tag <- function(x) attr(x, "Rd_tag")
  93. Filter(function(x) rd_tag(x) == tag, rd)
  94. }
  95. rd2rd <- function(x) {
  96. chr <- internal_f("tools", "as.character.Rd")(x)
  97. paste(unlist(chr), collapse = "")
  98. }
  99. # rd_arguments(get_rd("mean"))
  100. rd_arguments <- function(rd) {
  101. arguments <- get_tags(rd, "\\arguments")[[1]]
  102. items <- get_tags(arguments, "\\item")
  103. values <- lapply(items, function(x) rd2rd(x[[2]]))
  104. params <- vapply(items, function(x) rd2rd(x[[1]]), character(1))
  105. setNames(values, params)
  106. }
  107. res = rd_arguments(get_rd(topic))
  108. if(!dots){
  109. res = res[names(res) != "\\dots"]
  110. }
  111. res = sapply(res,
  112. function(x){
  113. x = gsub("\n" , "", x)
  114. x = gsub("\\\\", "", x)
  115. })
  116. nms = names(formals(topic))
  117. nms = setdiff(nms, "...")
  118. res = split_arg(res)
  119. res[names(res) %in% nms]
  120. }
  121. split_arg = function(x){
  122. .arg = c()
  123. for(nm in names(x)){
  124. if(grepl(",", nm)){
  125. nms = strsplit(nm, split = ",")[[1]]
  126. nms = gsub("^\\s+|\\s+$", "", nms)
  127. res = rep(x[nm], length(nms))
  128. names(res) = nms
  129. }else{
  130. res = x[nm]
  131. }
  132. .arg = c(.arg, res)
  133. }
  134. .arg
  135. }
  136. # get type from a input file
  137. get_type = function(input){
  138. if(!is.na(file.info(input)$isdir) && file.info(input)$isdir){
  139. if(is_shinyapp(input)){
  140. return("shinyapp")
  141. }else{
  142. return(NULL)
  143. }
  144. }else{
  145. ## treat as file
  146. if (!file.exists(input)){
  147. stop('input file or shiny app folder does not exist')
  148. }
  149. }
  150. .run = liftr:::parse_rmd(input)$runtime
  151. if(!is.null(.run) && .run == "shiny"){
  152. return("shinydoc")
  153. }else{
  154. return("rmd")
  155. }
  156. }
  157. is_shinydoc = function(input){
  158. get_type(input) == "shinydoc"
  159. }
  160. is_shinyapp = function(input){
  161. if(!is.na(file.info(input)$isdir) && file.info(input)$isdir){
  162. fls = list.files(input)
  163. res = "app.R" %in% fls | ("server.R" %in% fls && "ui.R" %in% fls)
  164. return(res)
  165. }else{
  166. return(FALSE)
  167. }
  168. }
  169. render_engine = function(input){
  170. switch(get_type(input),
  171. "rmd" = "render",
  172. "shinydoc" = "run",
  173. NULL)
  174. }
  175. .showFields <- function(x, title = NULL, values = NULL, full = FALSE){
  176. if (missing(values)){
  177. flds = names(x$getRefClass()$fields())
  178. }else{
  179. flds = values
  180. }
  181. if(!length(x))
  182. return(NULL)
  183. if(!full){
  184. idx <- sapply(flds, is.null)
  185. if(!is.null(title) && !all(idx)){
  186. message(title)
  187. }
  188. ## ugly, change later
  189. for (fld in flds[!idx]){
  190. if(is.list(x[[fld]])){
  191. if(length(x[[fld]])){
  192. message(fld, ":")
  193. .showList(x[[fld]], space = " ")
  194. }
  195. }else if(is(x[[fld]], "Item")){
  196. x[[fld]]$show()
  197. }else{
  198. if(is.character(x[[fld]])){
  199. if(x[[fld]] != "" && length(x[[fld]])){
  200. message(fld, " : ", paste0(x[[fld]], collapse = " "))
  201. }
  202. }else{
  203. if(!is.null(x[[fld]]) && length(x[[fld]]))
  204. message(fld, " : ", x[[fld]])
  205. }
  206. }
  207. }
  208. }else{
  209. message(title)
  210. ## ugly, change later
  211. for (fld in flds){
  212. if(is.list(x[[fld]])){
  213. message(fld, ":")
  214. .showList(x[[fld]], space = " ", full = full)
  215. }else if(is(x[[fld]], "Item")){
  216. x[[fld]]$show()
  217. }else{
  218. if(is.character(x[[fld]])){
  219. message(fld, " : ", paste0(x[[fld]], collapse = " "))
  220. }else{
  221. message(fld, " : ", x[[fld]])
  222. }
  223. }
  224. }
  225. }
  226. }