utils.R 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. # convert string vectors to single quote-marked
  2. # string separated by comma
  3. quote_str = function(x) {
  4. paste0("\'", paste(x, collapse = "','"), "\'")
  5. }
  6. # get directory of the file
  7. file_dir = function(x) {
  8. dirname(normalizePath(x))
  9. }
  10. # get file name with extension
  11. file_name = function(x) {
  12. basename(normalizePath(x))
  13. }
  14. # get file name without extension
  15. sans_ext = tools::file_path_sans_ext
  16. file_name_sans = function(x) {
  17. basename(sans_ext(normalizePath(x)))
  18. }
  19. # generate UUID for Docker container names
  20. # derived from https://stackoverflow.com/questions/10492817/
  21. uuid = function() {
  22. id = paste(sample(c(letters[1:6], 0:9), 30, replace = TRUE), collapse = '')
  23. paste(
  24. substr(id, 1, 8), '_', substr(id, 9, 12), '_', '4',
  25. substr(id, 13, 15), '_', sample(c('8', '9', 'a', 'b'), 1),
  26. substr(id, 16, 18), '_', substr(id, 19, 30), sep = '',
  27. collapse = '')
  28. }
  29. #' check if from Bioconductor base images
  30. #' @importFrom stringr str_trim
  31. #' @noRd
  32. is_from_bioc = function(x) {
  33. substr(str_trim(x), 1L, 13L) == 'bioconductor/'
  34. }
  35. #' check if from the rocker/rstudio base image
  36. #' @importFrom stringr str_trim
  37. #' @noRd
  38. is_from_rstudio = function(x) {
  39. substr(str_trim(x), 1L, 14L) == 'rocker/rstudio'
  40. }
  41. # remove consecutive blank lines and only keep one
  42. sanitize_blank = function(txt) {
  43. blank = which(txt == "")
  44. # if no blank lines at all
  45. if (length(blank) == 0L) return(txt)
  46. idx = which(diff(blank) == 1L) + 1L
  47. # if no consecutive blank lines
  48. if (length(idx) == 0L) return(txt)
  49. txt = txt[-blank[idx]]
  50. txt
  51. }
  52. # determine os type
  53. check_os = function() {
  54. if (Sys.info()[['sysname']] == 'Darwin') 'mac' else if
  55. (.Platform$OS.type == 'windows') 'win' else 'lnx'
  56. }