Onepunch-class.R 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. setClassUnion("characterORNULL", c("character", "NULL"))
  2. setClassUnion("logicalORNULL", c("logical", "NULL"))
  3. #' Creat a Onepunch object
  4. #'
  5. #' Onepunch object used for rendering
  6. #'
  7. #' @param input input Input (R Markdown or Shiny R markdown) file or shiny app folder.
  8. #' @param output_dir output_dir Directory to output \code{Dockerfile}.
  9. #' @param container container name
  10. #' @param image image name
  11. #' @param tag Docker image name to build, sent as docker argument \code{-t}.
  12. #' If not specified, it will use the same name as the input file.
  13. #' @param prebuild prebuild a command line string to call before docker build
  14. #' @param build_args build_args A character string specifying additional
  15. #' \code{docker build} arguments. For example,
  16. #' \code{--pull=true -m="1024m" --memory-swap="-1"}.
  17. #' @param liftr_template Rmarkdown template used to generate Dockerfile.
  18. #' @param dockerfile Dockerfile path.
  19. #' @param cache default TRUE, if FALSE, build with --no-cache=true
  20. #' @param rm efault FALSE, if TRUE build with --rm
  21. #' @param clean clean all container or not
  22. #' @param type "shinyapp" or "shinydoc" or "rmd"
  23. #' @param browseURL logical, default FALSE, to open browser automatically or not
  24. #' for shiny
  25. #' @param shiny_run how to launch shiny from command line
  26. #' @param url returned URL for browsing.
  27. #'
  28. #' @return Onepunch object
  29. #'
  30. #' @exportClass Onepunch
  31. #' @export Onepunch
  32. #' @examples
  33. #' \dontrun{
  34. #' o = Onepunch("~/liftr_docker/ShinyDoc.Rmd")
  35. #' o$punch()
  36. #' o$clean()
  37. #' }
  38. Onepunch = setRefClass("Onepunch",
  39. fields = list(input = "characterORNULL",
  40. output_dir = "characterORNULL",
  41. container = "characterORNULL",
  42. image = "characterORNULL",
  43. tag = "characterORNULL",
  44. prebuild = "characterORNULL",
  45. build_args = "characterORNULL",
  46. lift_template = "characterORNULL",
  47. dockerfile = "characterORNULL",
  48. cache = "logicalORNULL",
  49. rm = "logicalORNULL",
  50. clean = "logicalORNULL",
  51. type = "characterORNULL",
  52. browseURL = "logicalORNULL",
  53. shiny_run = "characterORNULL",
  54. url = "characterORNULL"
  55. ),
  56. methods = list(
  57. initialize = function(input = getwd(),
  58. output_dir = NULL,
  59. lift_template = system.file('template/Dockerfile.Rmd', package = 'liftr'),
  60. container = NULL,
  61. image = NULL,
  62. tag = NULL,
  63. prebuild = NULL,
  64. dockerfile = NULL,
  65. cache = TRUE,
  66. rm = FALSE,
  67. clean = FALSE,
  68. type = NULL,
  69. browseURL = FALSE,
  70. shiny_run = NULL,
  71. url = NULL,
  72. ...
  73. ){
  74. input <<- input
  75. if(is.null(output_dir))
  76. output_dir <<- dirname(input)
  77. lift_template <<- lift_template
  78. container <<- container
  79. image <<- image
  80. tag <<- tag
  81. prebuild <<- prebuild
  82. dockerfile <<- dockerfile
  83. cache <<- cache
  84. rm <<- rm
  85. clean <<- clean
  86. type <<- get_type(input)
  87. browseURL <<- browseURL
  88. shiny_run <<- shiny_run
  89. url <<- url
  90. },
  91. delete = function(id = NULL){
  92. if(is.null(id)){
  93. id <- container
  94. }
  95. system(paste("docker stop", id))
  96. system(paste("docker rm", id))
  97. },
  98. clean_container = function(){
  99. system("docker stop $(docker ps -a -q)")
  100. system("docker rm $(docker ps -a -q)")
  101. },
  102. clean_image = function(){
  103. system("docker rmi $(docker images | grep '^<none>' | awk '{print $3}')")
  104. },
  105. clean_all = function(){
  106. clean_container()
  107. clean_image()
  108. },
  109. lift = function(...){
  110. res = liftr::lift(input, dockerfile = lift_template, output_dir = output_dir, ...)
  111. dockerfile <<- res$dockerfile
  112. res
  113. },
  114. drender = function(...){
  115. res = liftr::drender(input, clean = clean, rm = rm, cache = cache, prebuild = prebuild,
  116. browseURL = browseURL, ...)
  117. image <<- res$image_name
  118. container <<- res$container_name
  119. shiny_run <<- res$shiny_run
  120. url <<- res$url
  121. res
  122. },
  123. onepunch = function(...){
  124. lift()
  125. drender()
  126. },
  127. deploy = function(script = NULL,
  128. ...){
  129. 'deploy inside container'
  130. if(is.null(container)){
  131. onepunch()
  132. }
  133. .base = paste("docker run ", image)
  134. if(!is.null(script))
  135. .base = paste0(.base,
  136. " /usr/bin/Rscript -e \"", script, ";")
  137. dots_arg = list(...)
  138. dots_arg$appDir = paste0("/srv/shiny-server/",file_name(input))
  139. tmp = tempfile()
  140. dput(dots_arg, file = tmp)
  141. render_args = squote(paste0(readLines(tmp), collapse = '\n'))
  142. render_cmd = paste0("library(rsconnect);do.call(deployApp, ", render_args, ")")
  143. docker_run_cmd = paste0(.base, render_cmd, "\"")
  144. message(docker_run_cmd)
  145. system(docker_run_cmd)
  146. },
  147. show = function(){
  148. .showFields(.self)
  149. }
  150. ))