Klimi's new dotfiles with stow.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

143 lines
4.7 KiB

5 years ago
  1. .ess_weave <- function(command, file, encoding = NULL){
  2. cmd_symb <- substitute(command)
  3. if (grepl('knit|purl', deparse(cmd_symb))) require(knitr)
  4. od <- getwd()
  5. on.exit(setwd(od))
  6. setwd(dirname(file))
  7. frame <- parent.frame()
  8. if (is.null(encoding))
  9. eval(bquote(.(cmd_symb)(.(file))), envir = frame)
  10. else
  11. eval(bquote(.(cmd_symb)(.(file), encoding = .(encoding))), envir = frame)
  12. }
  13. .ess_knit <- function(file, output = NULL){
  14. library(knitr)
  15. frame <- parent.frame()
  16. od <- getwd()
  17. on.exit(setwd(od))
  18. setwd(dirname(file))
  19. ## this bquote is really needed for data.table := operator to work correctly
  20. eval(bquote(knit(.(file), output = .(output))), envir = frame)
  21. }
  22. .ess_sweave <- function(file, output = NULL){
  23. od <- getwd()
  24. frame <- parent.frame()
  25. on.exit(setwd(od))
  26. setwd(dirname(file))
  27. eval(bquote(Sweave(.(file), output = .(output))), envir = frame)
  28. }
  29. ## Users might find it useful. So don't prefix with .ess.
  30. .ess_htsummary <- function(x, hlength = 4, tlength = 4, digits = 3) {
  31. ## fixme: simplify and generalize
  32. snames <- c("mean", "sd", "min", "max", "nlev", "NAs")
  33. d <- " "
  34. num_sumr <- function(x){
  35. c(f(mean(x, na.rm = TRUE)),
  36. f(sd(x, na.rm = TRUE)),
  37. f(min(x, na.rm = TRUE)),
  38. f(max(x, na.rm = TRUE)),
  39. d,
  40. f(sum(is.na(x), na.rm = TRUE)))
  41. }
  42. f <- function(x) format(x, digits = digits)
  43. if (is.data.frame(x) | is.matrix(x)) {
  44. if (nrow(x) <= tlength + hlength){
  45. print(x)
  46. } else {
  47. if (is.matrix(x))
  48. x <- data.frame(unclass(x))
  49. ## conversion needed, to avoid problems with derived classes suchs
  50. ## as data.table
  51. h <- as.data.frame(head(x, hlength))
  52. t <- as.data.frame(tail(x, tlength))
  53. for (i in 1:ncol(x)) {
  54. h[[i]] <- f(h[[i]])
  55. t[[i]] <- f(t[[i]])
  56. }
  57. ## summaries
  58. sumr <- sapply(x, function(c){
  59. if (is.logical(c))
  60. ## treat logical as numeric; it's harmless
  61. c <- as.integer(c)
  62. if (is.numeric(c))
  63. num_sumr(c)
  64. else if (is.factor(c)) c(d, d, d, d, nlevels(c), sum(is.na(c)))
  65. else rep.int(d, length(snames))
  66. })
  67. sumr <- as.data.frame(sumr)
  68. row.names(sumr) <- snames
  69. dots <- rep("...", ncol(x))
  70. empty <- rep.int(" ", ncol(x))
  71. lines <- rep.int(" ", ncol(x))
  72. df <- rbind(h, ... = dots, t, `_____` = lines, sumr, ` ` = empty)
  73. print(df)
  74. }
  75. } else {
  76. cat("head(", hlength, "):\n", sep = "")
  77. print(head(x, hlength))
  78. if (length(x) > tlength + hlength){
  79. cat("\ntail(", tlength, "):\n", sep = "")
  80. print(tail(x, tlength))
  81. }
  82. cat("_____\n")
  83. if (is.numeric(x) || is.logical(x))
  84. print(structure(num_sumr(x), names = snames), quote = FALSE)
  85. else if (is.factor(x)){
  86. cat("NAs: ", sum(is.na(x), na.rm = TRUE), "\n")
  87. cat("levels: \n")
  88. print(levels(x))
  89. }
  90. }
  91. invisible(NULL)
  92. }
  93. .ess_vignettes <- function(all=FALSE) {
  94. vs <- unclass(browseVignettes(all = all))
  95. vs <- vs[sapply(vs, length) > 0]
  96. mat2elist <- function(mat) {
  97. if (!is.null(dim(mat))){
  98. apply(mat, 1, function(r)
  99. sprintf("(list \"%s\")",
  100. paste0(gsub("\"", "\\\\\"",
  101. as.vector(r[c("Title", "Dir", "PDF",
  102. "File", "R")])),
  103. collapse = "\" \"")))
  104. }
  105. }
  106. cat("(list \n",
  107. paste0(mapply(function(el, name) {
  108. sprintf("(list \"%s\" %s)",
  109. name, paste0(mat2elist(el), collapse = "\n"))
  110. },
  111. vs, names(vs)), collapse = "\n"), ")\n")
  112. }
  113. .ess_Rd2txt <- function(rd) {
  114. fun <- tools::Rd2txt
  115. if (length(formals(fun)["stages"]))# newer R version
  116. fun(rd, stages = c("build", "install", "render"))
  117. else
  118. fun(rd)
  119. }
  120. ## Hacked help.start() to use with ess-rutils.el
  121. .ess_help_start <- function(update=FALSE, remote=NULL) {
  122. home <- if (is.null(remote)) {
  123. port <- tools::startDynamicHelp(NA)
  124. if (port > 0L) {
  125. if (update)
  126. make.packages.html(temp=TRUE)
  127. paste0("http://127.0.0.1:", port)
  128. }
  129. else stop(".ess_help_start() requires the HTTP server to be running",
  130. call.=FALSE)
  131. } else remote
  132. paste0(home, "/doc/html/index.html")
  133. }