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.

82 lines
2.2KB

  1. #' Create a Frappe Chart
  2. #'
  3. #' Creates a Frappe Chart
  4. #'
  5. #' @import htmlwidgets
  6. #'
  7. #' @export
  8. frappeChart <- function(
  9. data,
  10. type = c("line", "bar", "pie", "percentage", "heatmap"),
  11. title = "",
  12. ...,
  13. is_navigable = TRUE,
  14. width = NULL,
  15. height = 250,
  16. elementId = NULL
  17. ) {
  18. # forward options using x
  19. x = list(
  20. title = title,
  21. type = match.arg(type),
  22. height = height,
  23. data = data,
  24. isNavigable = is_navigable,
  25. ...
  26. )
  27. # create widget
  28. htmlwidgets::createWidget(
  29. name = 'frappeChart',
  30. x,
  31. width = width,
  32. height = height,
  33. package = 'frappeCharts',
  34. elementId = elementId
  35. )
  36. }
  37. #' Shiny bindings for frappeChart
  38. #'
  39. #' Output and render functions for using frappeChart within Shiny
  40. #' applications and interactive Rmd documents.
  41. #'
  42. #' @param outputId output variable to read from
  43. #' @param width,height Must be a valid CSS unit (like \code{'100\%'},
  44. #' \code{'400px'}, \code{'auto'}) or a number, which will be coerced to a
  45. #' string and have \code{'px'} appended.
  46. #' @param expr An expression that generates a frappeChart
  47. #' @param env The environment in which to evaluate \code{expr}.
  48. #' @param quoted Is \code{expr} a quoted expression (with \code{quote()})? This
  49. #' is useful if you want to save an expression in a variable.
  50. #'
  51. #' @name frappeChart-shiny
  52. #'
  53. #' @export
  54. frappeChartOutput <- function(outputId, width = '100%', height = '400px'){
  55. htmlwidgets::shinyWidgetOutput(outputId, 'frappeChart', width, height, package = 'frappeCharts')
  56. }
  57. #' @rdname frappeChart-shiny
  58. #' @export
  59. renderFrappeChart <- function(expr, env = parent.frame(), quoted = FALSE) {
  60. if (!quoted) { expr <- substitute(expr) } # force quoted
  61. htmlwidgets::shinyRenderWidget(expr, frappeChartOutput, env, quoted = TRUE)
  62. }
  63. #' @rdname frappeChart-shiny
  64. #' @export
  65. updateFrappeChart <- function(inputId, data, session = shiny::getDefaultReactiveDomain()) {
  66. session$sendCustomMessage("frappeCharts:update", list(id = inputId, data = data))
  67. }
  68. .onLoad <- function(libname, pkgname) {
  69. shiny::registerInputHandler(
  70. type = "frappeCharts-selected",
  71. fun = function(value, session, inputName) {
  72. as.data.frame(value, stringsAsFactors = FALSE)
  73. }
  74. )
  75. }