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.

198 line
4.4KB

  1. ```{r setup, include=FALSE}
  2. knitr::opts_chunk$set(eval = FALSE)
  3. ```
  4. ## Setup R Package
  5. Create a package for this HTML widget.
  6. We're not going to publish this, so you can call it whatever you want
  7. ```{r create-package}
  8. usethis::create_package("frappeCharts")
  9. ```
  10. Add a dev script for notes
  11. ```{r dev}
  12. dir.create("dev")
  13. file.create("dev/dev.R")
  14. rstudioapi::navigateToFile("dev/dev.R")
  15. ```
  16. ## Setup npm package
  17. Same process again, but this time for npm.
  18. ```bash
  19. npm init
  20. # or
  21. npm init -y
  22. ```
  23. Open `package.json` and take a look
  24. ```json
  25. {
  26. "name": "frappecharts",
  27. "version": "0.0.1",
  28. "description": "",
  29. "main": "index.js",
  30. "scripts": {
  31. "test": "echo \"Error: no test specified\" && exit 1"
  32. },
  33. "author": "",
  34. "license": "MIT"
  35. }
  36. ```
  37. From Frappe Charts [docs#installation](https://frappe.io/charts/docs#installation):
  38. ```bash
  39. npm install frappe-charts
  40. ```
  41. We now have a dependency in `package.json` and there's a `package-lock.json` file.
  42. ```json
  43. "dependencies": {
  44. "frappe-charts": "^1.3.0"
  45. }
  46. ```
  47. ## Ignore node_modules but add package-lock
  48. There's also a `node_modules/` folder with `frappe-charts/` inside.
  49. Add `node_modules` to `.Rbuildignore` and `.gitignore`.
  50. (BTW, you can and are supposed to commit `package-lock.json`.)
  51. ```{r ignore-node-module}
  52. usethis::use_build_ignore("node_modules")
  53. usethis::use_build_ignore("package.json")
  54. usethis::use_build_ignore("package-lock.json")
  55. usethis::use_git_ignore("node_modules")
  56. ```
  57. ## Scaffold the HTML widget
  58. ```{r htmlwidgets-scaffold}
  59. htmlwidgets::scaffoldWidget("frappeChart")
  60. ```
  61. This adds files in `inst/htmlwidgets`
  62. ```
  63. inst
  64. └── htmlwidgets
  65. ├── frappeChart.js #<< R <-> JS code
  66. └── frappeChart.yaml #<< list of dependencies
  67. ```
  68. and creates a file `R/frappeChart.R` with the functions
  69. - `frappeChart()`
  70. - `frappeChartOutput()` (for shiny)
  71. - `renderFrappeChart()` (for shiny)
  72. ## Use `npm` to get our dependencies in the right place
  73. `htmlwidgets` load dependencies in a way that's exactly the same as using a
  74. `<script>` tag in the HTML `<head>`.
  75. Look at the documentation on Frappe Charts and decide which file we should use.
  76. Here's the block from their docs
  77. ```
  78. <script src="https://cdn.jsdelivr.net/npm/frappe-charts@1.2.4/dist/frappe-charts.min.iife.js"></script>
  79. <!-- or -->
  80. <script src="https://unpkg.com/frappe-charts@1.2.4/dist/frappe-charts.min.iife.js"></script>
  81. ```
  82. We need to get our dependecy into a subfolder of `inst/htmlwidgets`.
  83. Convention is `inst/htmlwidgets/lib/<dependency_name>`.
  84. Rather than creating the directoy and copying over, etc.,
  85. we can have an `npm` build script do this for us.
  86. To avoid issues with mac/windows,
  87. we'll add a dev dependency on [`cpy-cli`](https://github.com/sindresorhus/cpy-cli)
  88. ```bash
  89. npm install cpy-cli --save-dev
  90. ```
  91. and
  92. ```{r create-lib-dir}
  93. dir.create("inst/htmlwidets/lib/frappe-charts", recursive = TRUE)
  94. ```
  95. and then edit `package.json` to add copy tasks
  96. ```
  97. "scripts": {
  98. "copy-js": "cpy 'node_modules/frappe-charts/dist/frappe-charts.min.iife*' inst/htmlwidgets/lib/frappe-charts/",
  99. "build": "npm run copy-js"
  100. }
  101. ```
  102. ## Create a demo html_document_plain()
  103. ```{r create-demo-html}
  104. dir.create("dev/demo")
  105. js4shiny::js4shiny_rmd(path = "dev/demo/demo.Rmd")
  106. ```
  107. Use the example in the [Frappe Charts Docs](https://frappe.io/charts/docs).
  108. ```{r}
  109. tagList(
  110. div(id = "chart"),
  111. htmltools::htmlDependency(
  112. name = "frappe-charts",
  113. version = "1.3.0",
  114. package = "frappeCharts",
  115. src = "htmlwidgets/lib/frappe-charts",
  116. script = "frappe-charts.min.iife.js",
  117. all_files = TRUE
  118. )
  119. )
  120. ```
  121. And copy the JS into a javascript chunk.
  122. `r emo::ji("warning")` The dependencies won't be found until you build/install.
  123. ```{r build-install}
  124. devtools::document()
  125. devtools::install()
  126. ```
  127. If you get a path not found error
  128. ```
  129. Error: path for html_dependency not found: inst/htmlwidgets/lib/frappe-charts
  130. ```
  131. it's most likely because
  132. ```
  133. src = "inst/htmlwidgets/lib/frappe-charts"
  134. ```
  135. ## Replace the example data with another data set and example
  136. The first demo mixes chart types and we don't want to do that.
  137. Use the example from
  138. [Basic Chart](https://frappe.io/charts/docs/basic/basic_chart#adding-more-datasets).
  139. ```js
  140. const data = {
  141. labels: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"],
  142. datasets: [
  143. { name: "R", values: [18, 40, 30, 35, 8, 52, 17, -4] },
  144. { name: "Python", values: [30, 50, -10, 15, 18, 32, 27, 14] }
  145. ]
  146. }
  147. ```