Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

138 rindas
3.0KB

  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. ```