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.

205 lines
7.8KB

  1. #' Print method for desctable
  2. #'
  3. #' @param x A desctable
  4. #' @param ... Additional print parameters
  5. #' @return A flat dataframe
  6. #' @export
  7. print.desctable <- function(x, ...) {
  8. print(as.data.frame(x))
  9. }
  10. #' As.data.frame method for desctable
  11. #'
  12. #' @param x A desctable
  13. #' @param ... Additional as.data.frame parameters
  14. #' @return A flat dataframe
  15. #' @export
  16. as.data.frame.desctable <- function(x, ...) {
  17. # Discard "markdown" formatting of variable names
  18. x$Variables$Variables <- gsub("\\*\\*(.*?)\\*\\*", "\\1", x$Variables$Variables)
  19. x$Variables$Variables <- gsub("\\*(.*?)\\*", "\\1", x$Variables$Variables)
  20. # Create a dataframe header
  21. header <- header(x, "dataframe")
  22. # Make a standard dataframe
  23. x %>%
  24. flatten_desctable() %>%
  25. data.frame(check.names = F, ...) %>%
  26. stats::setNames(header)
  27. }
  28. #' Pander method for desctable
  29. #'
  30. #' Pander method to output a desctable
  31. #'
  32. #' Uses \code{pandoc.table}, with some default parameters (\code{digits = 2}, \code{justify = "left"}, \code{missing = ""}, \code{keep.line.breaks = T}, \code{split.tables = Inf}, and \code{emphasize.rownames = F}), that you can override if needed.
  33. #'
  34. #' @param x A desctable
  35. #' @inheritParams pander::pandoc.table
  36. #' @seealso \code{\link{pandoc.table}}
  37. #' @export
  38. pander.desctable <- function(x = NULL,
  39. digits = 2,
  40. justify = "left",
  41. missing = "",
  42. keep.line.breaks = T,
  43. split.tables = Inf,
  44. emphasize.rownames = F,
  45. ...) {
  46. if (is.null(digits)) digits <- pander::panderOptions("digits")
  47. # Discard "markdown" and insert 4 NbSp before factor levels
  48. x$Variables$Variables <- gsub("\\*\\*(.*?)\\*\\*: \\*(.*?)\\*", "&nbsp;&nbsp;&nbsp;&nbsp;\\2", x$Variables$Variables)
  49. # Create a pander header
  50. header <- header(x, "pander")
  51. # Make a dataframe and push it to pandoc
  52. x %>%
  53. flatten_desctable %>%
  54. data.frame(check.names = F, stringsAsFactors = F) %>%
  55. stats::setNames(header) %>%
  56. pander::pandoc.table(justify = justify,
  57. digits = digits,
  58. missing = missing,
  59. keep.line.breaks = keep.line.breaks,
  60. split.tables = split.tables,
  61. emphasize.rownames = emphasize.rownames,
  62. ...)
  63. }
  64. #' Create an HTML table widget using the DataTables library
  65. #'
  66. #' This function creates an HTML widget to display rectangular data (a matrix or data frame) using the JavaScript library DataTables, with a method for \code{desctable} objects.
  67. #'
  68. #' @note
  69. #' You are recommended to escape the table content for security reasons (e.g. XSS attacks) when using this function in Shiny or any other dynamic web applications.
  70. #' @references
  71. #' See \url{http://rstudio.github.io/DT} for the full documentation.
  72. #' @examples
  73. #' library(DT)
  74. #'
  75. #' # see the package vignette for examples and the link to website
  76. #' vignette('DT', package = 'DT')
  77. #'
  78. #' # some boring edge cases for testing purposes
  79. #' m = matrix(nrow = 0, ncol = 5, dimnames = list(NULL, letters[1:5]))
  80. #' datatable(m) # zero rows
  81. #' datatable(as.data.frame(m))
  82. #'
  83. #' m = matrix(1, dimnames = list(NULL, 'a'))
  84. #' datatable(m) # one row and one column
  85. #' datatable(as.data.frame(m))
  86. #'
  87. #' m = data.frame(a = 1, b = 2, c = 3)
  88. #' datatable(m)
  89. #' datatable(as.matrix(m))
  90. #'
  91. #' # dates
  92. #' datatable(data.frame(
  93. #' date = seq(as.Date("2015-01-01"), by = "day", length.out = 5), x = 1:5
  94. #' ))
  95. #' datatable(data.frame(x = Sys.Date()))
  96. #' datatable(data.frame(x = Sys.time()))
  97. #'
  98. #' ###
  99. #' @inheritParams DT::datatable
  100. #' @export
  101. datatable <- function(data, ...) {
  102. UseMethod("datatable", data)
  103. }
  104. #' @rdname datatable
  105. #' @export
  106. datatable.default <- function(data,
  107. options = list(),
  108. class = "display",
  109. callback = DT::JS("return table;"),
  110. caption = NULL,
  111. filter = c("none", "bottom", "top"),
  112. escape = TRUE,
  113. style = "default",
  114. width = NULL,
  115. height = NULL,
  116. elementId = NULL,
  117. fillContainer = getOption("DT.fillContainer", NULL),
  118. autoHideNavigation = getOption("DT.autoHideNavigation", NULL),
  119. selection = c("multiple", "single", "none"),
  120. extensions = list(),
  121. plugins = NULL, ...) {
  122. DT::datatable(data, options = options, class = class, callback = callback, caption = caption, filter = filter, escape = escape, style = style, width = width, height = height, elementId = elementId, fillContainer = fillContainer, autoHideNavigation = autoHideNavigation, selection = selection, extensions = extensions, plugins = plugins, ...)
  123. }
  124. #' @rdname datatable
  125. #' @inheritParams base::prettyNum
  126. #' @export
  127. datatable.desctable <- function(data,
  128. options = list(paging = F,
  129. info = F,
  130. search = F,
  131. dom = "Brtip",
  132. fixedColumns = T,
  133. fixedHeader = T,
  134. buttons = c("copy", "excel")),
  135. class = "display",
  136. callback = DT::JS("return table;"),
  137. caption = NULL,
  138. filter = c("none", "bottom", "top"),
  139. escape = FALSE,
  140. style = "default",
  141. width = NULL,
  142. height = NULL,
  143. elementId = NULL,
  144. fillContainer = getOption("DT.fillContainer", NULL),
  145. autoHideNavigation = getOption("DT.autoHideNavigation", NULL),
  146. selection = c("multiple", "single", "none"),
  147. extensions = c("FixedHeader", "FixedColumns", "Buttons"),
  148. plugins = NULL,
  149. rownames = F,
  150. digits = 2, ...) {
  151. # Discard "markdown" and insert 4 NbSp before factor levels
  152. data$Variables$Variables <- gsub("\\*\\*(.*?)\\*\\*: \\*(.*?)\\*", "&nbsp;&nbsp;&nbsp;&nbsp;\\2", data$Variables$Variables)
  153. data$Variables$Variables <- gsub("\\*\\*(.*?)\\*\\*", "<b>\\1</b>", data$Variables$Variables)
  154. # Create a datatable header
  155. header <- header(data, "datatable")
  156. # Flatten desctable
  157. flat <- flatten_desctable(data)
  158. # Replace NAs and apply digits arg
  159. if (!is.null(digits))
  160. {
  161. flat %>%
  162. lapply(prettyNum, digits = digits) %>%
  163. lapply(gsub, pattern = "^NA$", replacement = "") -> flat
  164. }
  165. # Make a dataframe and push it to datatable, with its custom header
  166. flat %>%
  167. data.frame(check.names = F, stringsAsFactors = F) %>%
  168. DT::datatable(container = header,
  169. options = options,
  170. extensions = extensions,
  171. escape = escape,
  172. class = class,
  173. callback = callback,
  174. caption = caption,
  175. filter = filter,
  176. style = style,
  177. width = width,
  178. height = height,
  179. elementId = elementId,
  180. fillContainer = fillContainer,
  181. autoHideNavigation = autoHideNavigation,
  182. selection = selection,
  183. plugins = plugins,
  184. rownames = rownames, ...)
  185. }