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.

752 lines
21KB

  1. ---
  2. output: github_document
  3. ---
  4. # Building an HTML Widget: Frappe Charts
  5. [htmlwidget]: https://www.htmlwidgets.org/
  6. [frappe-charts]: https://frappe.io/charts
  7. [rmd-book-htmlwidget]: https://bookdown.org/yihui/rmarkdown/html-widgets.html
  8. This repository walks through the building of an [htmlwidget]
  9. around the JavaScript library [Frappe Charts][frappe-charts].
  10. Because there are many files and moving pieces involved in this process,
  11. I've created a git repository that walks through the changes
  12. at each step of the process.
  13. With my notes about each step,
  14. I've included the SHA linked to the updates made during the step.
  15. If you're viewing this on GitHub,
  16. those SHA hashes should be converted to links
  17. that will take you to a summary of which files changed at each step.
  18. This introduction focuses on the mechanics of htmlwidgets.
  19. For a much more detailed summary,
  20. the [HTML Widgets][rmd-book-htmlwidget] chapter of the R Markdown book
  21. is an excellent introduction.
  22. ```{r setup, include=FALSE}
  23. knitr::opts_chunk$set(eval = FALSE)
  24. github_sha_link <- function(sha) {
  25. glue::glue("[changelog: {substr(sha, 1, 6)}](https://github.com/gadenbuie/js4shiny-frappeCharts/commit/{sha})")
  26. }
  27. ```
  28. ## Setup R Package
  29. * `r github_sha_link("a3f6fdd986d2b98323b5be43e323df4f6a19f1f3")`
  30. Create a package for this HTML widget.
  31. We're not going to publish this, so you can call it whatever you want
  32. ```{r create-package}
  33. usethis::create_package("frappeCharts")
  34. ```
  35. Add a dev script for notes
  36. ```{r dev}
  37. dir.create("dev")
  38. file.create("dev/dev.R")
  39. rstudioapi::navigateToFile("dev/dev.R")
  40. ```
  41. ### Add the R package dependencies for an htmlwidget package
  42. ```{r r-deps}
  43. usethis::use_package("htmlwidgets")
  44. usethis::use_package("htmltools")
  45. usethis::use_package("jsonlite")
  46. usethis::use_package("shiny")
  47. usethis::use_package("yaml")
  48. ```
  49. ## Setup npm package
  50. * `r github_sha_link("256f0ca112b2685608f9a17a4fb4e35d279c9830")`
  51. Same process again, but this time for npm.
  52. ```bash
  53. npm init
  54. # or
  55. npm init -y
  56. ```
  57. Open `package.json` and take a look
  58. ```json
  59. {
  60. "name": "frappecharts",
  61. "version": "0.0.1",
  62. "description": "",
  63. "main": "index.js",
  64. "scripts": {
  65. "test": "echo \"Error: no test specified\" && exit 1"
  66. },
  67. "author": "",
  68. "license": "MIT"
  69. }
  70. ```
  71. From Frappe Charts [docs#installation](https://frappe.io/charts/docs#installation):
  72. ```bash
  73. npm install frappe-charts
  74. ```
  75. We now have a dependency in `package.json` and there's a `package-lock.json` file.
  76. ```json
  77. "dependencies": {
  78. "frappe-charts": "^1.3.0"
  79. }
  80. ```
  81. ## Ignore node_modules but add package-lock
  82. There's also a `node_modules/` folder with `frappe-charts/` inside.
  83. Add `node_modules` to `.Rbuildignore` and `.gitignore`.
  84. (BTW, you can and are supposed to commit `package-lock.json`.)
  85. ```{r ignore-node-module}
  86. usethis::use_build_ignore("node_modules")
  87. usethis::use_build_ignore("package.json")
  88. usethis::use_build_ignore("package-lock.json")
  89. usethis::use_git_ignore("node_modules")
  90. ```
  91. ## Scaffold the HTML widget
  92. * `r github_sha_link("38bac2c65cf54816525076690310008e62ab99a1")`
  93. ```{r htmlwidgets-scaffold}
  94. htmlwidgets::scaffoldWidget("frappeChart")
  95. ```
  96. This adds files in `inst/htmlwidgets`
  97. ```
  98. inst
  99. └── htmlwidgets
  100. ├── frappeChart.js #<< R <-> JS code
  101. └── frappeChart.yaml #<< list of dependencies
  102. ```
  103. and creates a file `R/frappeChart.R` with the functions
  104. - `frappeChart()`
  105. - `frappeChartOutput()` (for shiny)
  106. - `renderFrappeChart()` (for shiny)
  107. ## Use `npm` to get our dependencies in the right place
  108. * `r github_sha_link("7abf0224345a67217c4a476f04eafe581f0ecec0")`
  109. `htmlwidgets` load dependencies in a way that's exactly the same as using a
  110. `<script>` tag in the HTML `<head>`.
  111. Look at the
  112. [documentation on Frappe Charts](https://frappe.io/charts/docs#installation)
  113. and decide which file we should use.
  114. Here's the block from their docs
  115. ```
  116. <script src="https://cdn.jsdelivr.net/npm/frappe-charts@1.2.4/dist/frappe-charts.min.iife.js"></script>
  117. <!-- or -->
  118. <script src="https://unpkg.com/frappe-charts@1.2.4/dist/frappe-charts.min.iife.js"></script>
  119. ```
  120. We need to get our dependecy into a subfolder of `inst/htmlwidgets`.
  121. Convention is `inst/htmlwidgets/lib/<dependency_name>`.
  122. Rather than creating the directoy and copying over, etc.,
  123. we can have an `npm` build script do this for us.
  124. To avoid issues with mac/windows,
  125. we'll add a dev dependency on [`cpy-cli`](https://github.com/sindresorhus/cpy-cli).
  126. Dev dependencies are node modules
  127. that are used to build a package,
  128. rather than required for the package to work.
  129. ```bash
  130. npm install cpy-cli --save-dev
  131. ```
  132. Then we create the folder `frappe-charts` under `inst/htmlwidgets/lib`
  133. that will hold the Frappe Charts JavaScript dependency.
  134. (If the library included other required files,
  135. we would move these too.)
  136. ```{r create-lib-dir}
  137. dir.create("inst/htmlwidets/lib/frappe-charts", recursive = TRUE)
  138. ```
  139. And then edit `package.json` to add a copy task.
  140. You can define scripts that are runnable with `npm run <script-name>`.
  141. For small build tasks, this is an easy to implement build solution.
  142. ```
  143. "scripts": {
  144. "copy-js": "cpy 'node_modules/frappe-charts/dist/frappe-charts.min.iife*' inst/htmlwidgets/lib/frappe-charts/",
  145. "build": "npm run copy-js"
  146. }
  147. ```
  148. Notice that running `npm run build` will also call `npm run copy-js`.
  149. If we had more build tasks related to our JavaScript dependencies,
  150. like linting or testing,
  151. we could add them as separate scripts
  152. and have them run in the build process with
  153. `npm run <task-1> && npm run <task-2>`, etc.
  154. ## Create a demo html_document_plain()
  155. * `r github_sha_link("036d454f80d6036fc1ba35db92161fd19c053635")`
  156. ```{r create-demo-html}
  157. dir.create("dev/demo")
  158. js4shiny::js4shiny_rmd(path = "dev/demo/demo.Rmd")
  159. ```
  160. Use the example in the [Frappe Charts Docs](https://frappe.io/charts/docs).
  161. ```{r}
  162. tagList(
  163. div(id = "chart"),
  164. htmltools::htmlDependency(
  165. name = "frappe-charts",
  166. version = "1.3.0",
  167. package = "frappeCharts",
  168. src = "htmlwidgets/lib/frappe-charts",
  169. script = "frappe-charts.min.iife.js",
  170. all_files = TRUE
  171. )
  172. )
  173. ```
  174. And copy the JS into a javascript chunk.
  175. `r emo::ji("warning")` The dependencies won't be found until you build/install.
  176. ```{r build-install}
  177. devtools::document()
  178. devtools::install()
  179. ```
  180. If you get a path not found error
  181. ```
  182. Error: path for html_dependency not found: inst/htmlwidgets/lib/frappe-charts
  183. ```
  184. it's most likely because
  185. ```
  186. src = "inst/htmlwidgets/lib/frappe-charts"
  187. ```
  188. should be relative to `inst`.
  189. ## Replace the example data with another data set and example
  190. * `r github_sha_link("8fd703a08b021b8466171b83506f5fb0bf92f2ac")`
  191. The first demo mixes chart types and we don't want to do that.
  192. Use the example from
  193. [Basic Chart](https://frappe.io/charts/docs/basic/basic_chart#adding-more-datasets).
  194. ```js
  195. const data = {
  196. labels: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"],
  197. datasets: [
  198. { name: "R", values: [18, 40, 30, 35, 8, 52, 17, -4] },
  199. { name: "Python", values: [30, 50, -10, 15, 18, 32, 27, 14] }
  200. ]
  201. }
  202. ```
  203. Then re-create this data in an R chunk (`r github_sha_link("881c12ffdbdaa017863c918f61fa6208400d6130")`:
  204. ```{r data-in-r}
  205. data <- list(
  206. labels = c("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"),
  207. datasets = list(
  208. list(name = "R", values = c(18, 40, 30, 35, 8, 52, 17, -4)),
  209. list(name = "Python", values = c(30, 50, -10, 15, 18, 32, 27, 14))
  210. )
  211. )
  212. ```
  213. To get the data out of R and make it available in the document,
  214. `htmlwidgets` embeds the data in a `<script type="application/json">...</script>`
  215. element in the page.
  216. Embed the data from the R chunk in a `<script>` tag with an ID
  217. so that we can find it later.
  218. ```{r embed-r-data-in-script}
  219. tags$script(
  220. id = "data",
  221. type = "application/json",
  222. htmlwidgets:::toJSON(data)
  223. )
  224. ```
  225. Change to `js4shiny::html_document_js()` so that we can see the `console.log()`
  226. from JavaScript just like R code.
  227. And then find the `<script>` tag and get it's `.textContent`.
  228. ```{js find-r-data-script-2}
  229. let rData = document.getElementById('data')
  230. rData.textContent
  231. ```
  232. Use `JSON.parse()` to turn the data into a JS object
  233. and replace the data used in the chart (`r github_sha_link("7201e436e72ebddee271cbf7c02a733ac81a5d86")`.
  234. ```{js find-r-data-script-3}
  235. let rData = document.getElementById('data')
  236. rData = JSON.parse(rData.textContent)
  237. ```
  238. Switch between `data` and `rData` and it should be the same!
  239. Change the values of the data in the R side to be random
  240. so that each re-run gives a new plot.
  241. ~~Delete the `data` in the JS side.~~
  242. Comment out the `data` on the JS side (but we'll want to see the structure later).
  243. ## Augment data to set options for the chart
  244. * `r github_sha_link("3e1d9bee03fdf621f5dc5ec46e0e92f603ebe219")`
  245. Embed `data` in another list `opts` that will carry additional options,
  246. such as `title`, `type` and `colors`.
  247. Parse the embedded `<script>` and pass the whole object to `frappe.Chart()`.
  248. Change the colors to
  249. - `#466683` (dark blue)
  250. - `#44bc96` (green)
  251. - `#d33f49` (red)
  252. - `#993d70` (purple)
  253. ## Learn about other options for line charts
  254. * `r github_sha_link("340d516ee4c7788e4f7e5089c4957ee9ffd1333e")`
  255. Read <https://frappe.io/charts/docs/basic/trends_regions>
  256. and add and test additional line options.
  257. Goal: shaded area chart with lines only.
  258. Make the `labels` one week and repeat 4 times.
  259. Generate `runif(7 * 4)` random numbers.
  260. ```r
  261. rep(c("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"), 4)
  262. ```
  263. Find and implement an option to reduce the number of labels on the x-axis.
  264. ## Turn on dots again and make navigable
  265. * `r github_sha_link("93d4c74f4b30a819b5c22fd7cc8ff238fc62f572")`
  266. ```{r}
  267. opts <- list(
  268. title = "My AwesomeR Chart",
  269. type = "bar",
  270. height = 250,
  271. colors = c("#466683", "#44bc96"),
  272. data = data,
  273. axisOptions = list(xIsSeries = TRUE),
  274. isNavigable = TRUE
  275. )
  276. ```
  277. ## Add a real data source
  278. * `r github_sha_link("7a988739e3b5ff0572f4c16ce5110f52936550c3")`
  279. This is where you decide how much work you want to do on the R side
  280. and how much work should be done on the JavaScript side.
  281. One thing is clear though,
  282. R users should not be expected
  283. to construct the nested list data structure
  284. just to use your HTML widget.
  285. We are used to data.frames and tibbles,
  286. so these should be supported out of the box!
  287. To practice our JavaScript skills,
  288. I've chosen to do most of the work in the browser.
  289. We'll just ask our users to give us rectangular data,
  290. where the first column will provide the x axis labels
  291. and the remaining columns are series.
  292. Sidenote: As you can tell, there's a lot of validation that should happen,
  293. but that part is not as much fun so we're going to pretend
  294. our users will always give us perfectly formatted data.
  295. If you do take on an htmlwidget package project,
  296. _don't skimp on this step_.
  297. Having a friendly R API will have huge impact on the use of your widget.
  298. We'll use the `babynames` package for our demo dataset,
  299. pick two names completely at random to compare.
  300. ```{r babynames}
  301. library(dplyr)
  302. library(babynames)
  303. data <-
  304. babynames %>%
  305. filter(
  306. name %in% c("Ruth", "August"),
  307. year >= 1980
  308. ) %>%
  309. group_by(year, name) %>%
  310. summarize(n = sum(n)) %>%
  311. ungroup() %>%
  312. pivot_wider(year, name, values_from = n)
  313. ```
  314. At this point the chart won't work,
  315. but you can use the browser dev console
  316. to find the right steps to reformat the data into the expected format.
  317. We'll make the **strong** assumption that the tibble in R
  318. should always be formatted with the columns
  319. 1. `labels`
  320. 2. first series...
  321. 3. second series...
  322. 4. etc.
  323. * `repl_example('reformat-r2js-data')`
  324. <details><summary>Answer</summary>
  325. ```js
  326. const chartData = {labels: [], datasets: []}
  327. // Get keys of data, assume that first entry is for labels, the rest are data
  328. let labelColumn = Object.keys(x.data)[0]
  329. let columns = Object.keys(x.data).slice(1)
  330. // First column in x.data is the labels
  331. chartData.labels = x.data[labelColumn]
  332. // Create an appropriate object for each column, reformat data and add to chartData
  333. columns.forEach(function(col) {
  334. chartData.datasets.push({name: col, values: x.data[col]})
  335. })
  336. x.data = chartData
  337. ```
  338. </details>
  339. ## This is basically what `htmlwidgets` does, just inside a framework
  340. * `r github_sha_link("a0614d9699aefc0eda82b3e368b48370be0ae9ba")`
  341. We now have all of the pieces of an `htmlwidget`,
  342. it's just a bit less coordinated.
  343. 1. `htmlwidgets` gives us a slightly nicer way of specifying dependencies
  344. in `inst/htmlwidgets/frappeChart.yaml`. We'll have to update that file.
  345. 2. When we started we added a `div(id = "chart")`.
  346. It would be annoying to have to make sure that each `id` is always unique.
  347. `htmlwidgets` will add this `div` for us and give each one a unique id.
  348. We won't have to write any code for this, it just happens.
  349. 3. We'll write an R function that will take input data and options and format
  350. it into a list, like the `opts` we've been using.
  351. Then we hand the data to `htmlwidgets` and it embeds it in a `<script>` tag
  352. for us.
  353. It will also find that data automatically and make it available on the JS side.
  354. 4. Finally, we wrote some code in JavaScript to initialize the chart.
  355. In the same way, we'll write some code in `inst/htmlwidgets/frappeChart.js`
  356. which is where we'll reformat the data and options passed from the R world
  357. by htmlwidgets. We also need to instantiate the chart object. For advanced
  358. usage, this is also where we'll put code that would let us update the
  359. widget in place without having to re-render the whole chart.
  360. To create the htmlwidget,
  361. we're going to work through each of these pieces
  362. and put them in the right places.
  363. # Make it an htmlwidget
  364. ## Declare dependencies
  365. * `r github_sha_link("969fd962edf0be8f98ffff1823f8e08960ffb31a")`
  366. **FILE:** `inst/htmlwidgets/frappeChart.yaml`
  367. Take the `htmltools::htmlDependency()` and
  368. turn it into `inst/htmlwidgets/frappeChart.yaml`.
  369. ```{r}
  370. rstudioapi::navigateToFile("inst/htmlwidgets/frappeChart.yaml")
  371. ```
  372. Note: keep `htmlwidgets` in `src`!
  373. ## Write the R function
  374. * `r github_sha_link("cbc25a8f7bbf7e522f369f5fec7c2517ba768656")`
  375. **FILE:** `R/frappeChart.r`
  376. Add appropriate arguments to `frappeChart()`.
  377. * [title](https://frappe.io/charts/docs/reference/configuration#title)
  378. * [type](https://frappe.io/charts/docs/reference/configuration#type)
  379. * [colors](https://frappe.io/charts/docs/reference/configuration#colors)?
  380. * [is_navigable](https://frappe.io/charts/docs/reference/configuration#isnavigable)
  381. Structure the arguments into `x` and pass `...` for the "extra bits".
  382. Rebuild the package,
  383. then create a new R markdown document:
  384. `js4shiny::js4shiny_doc()`.
  385. Move the code loading `dplyr`, `tidyr`, `babynames`
  386. and formatting the data.
  387. Then call `frappeCharts::frappeChart()`.
  388. Render and open dev tools in the browser to see that it "works".
  389. Meaning that the data and dependencies are included,
  390. but the chart won't.
  391. Point out the random ID.
  392. Then go back and change it so we can find the element better.
  393. ## Write JavaScript binding
  394. * `r github_sha_link("6f141c4341a2c4f8615df81887e7927d2e765f11")`
  395. **FILE:** `inst/htmlwidgets/frappeChart.js`
  396. The final step is to move the Javascript we wrote before into the js binding.
  397. * Just put in `console.log(x)`, rebuild, rerender
  398. * Verify that this `x` looks the same as our `opts` from before
  399. * Copy all of the JS we wrote to reconfigure the data into the widget
  400. * Use `el` instead of `#chart`
  401. * Rebuild, rerender
  402. * it works!
  403. * Try adding other options
  404. ### Writing JavaScript in R
  405. * `r github_sha_link("8d442e3c842154adbae87dab5e9289cbb1333187")`
  406. The [tooltips](https://frappe.io/charts/docs/basic/annotations#tooltips)
  407. can be formatted using the `tooltipOptions` property:
  408. ```
  409. tooltipOptions: {
  410. formatTooltipX: d => (d + '').toUpperCase(),
  411. formatTooltipY: d => d + ' pts',
  412. }
  413. ```
  414. To write this in R (add to `widget_demo.R`)
  415. ```r
  416. tooltipOptions = list(
  417. formatTooltipX = htmlwidgets::JS("d => 'Year: ' + d"),
  418. formatTooltipY = htmlwidgets::JS("d => d + ' babies'")
  419. )
  420. ```
  421. ## Shiny comes for free!
  422. * `r github_sha_link("739d5945010d5e46ab3f9847fd412beb0766805d")`
  423. Create a basic Shiny app with
  424. 1. Slider input to pick number of values (1:26 letters)
  425. 1. A new data button that generates new data of same dimension
  426. 1. The data are reactive, `x = letters[1:n]`, `y = runif(n)`
  427. 1. Use `frappeCharts::frappeChartOutput()` linked to `frappeCharts::renderFrappeChart()`
  428. - bar plot
  429. - fix `tooltipOptions` to turn the `runif()` into a percent.
  430. `dev/shiny/app.R`
  431. Make a mistake in the spelling for `formatTooltipY`
  432. and demo how hard it is for the end user to track down what's wrong.
  433. This points to how important it is to do the validation on the R side
  434. or to do the extra work to make the R API friendly.
  435. It's also a good place to demo debug strategies for Shiny and regular widgets.
  436. Open the app in an external window,
  437. show the dev console,
  438. find the frappeCharts binding
  439. and add a breakpoint.
  440. Then reload and show how you an use the dev console there to figure things out.
  441. ## Better data updates
  442. Frappe Charts,
  443. like many JS libraries,
  444. includes a method for updating the widget
  445. without having to redraw the whole chart/plot/viz/etc.
  446. In Frappe Charts, the
  447. [full data update](https://frappe.io/charts/docs/update_state/modify_data#updating-full-data)
  448. method is
  449. ```js
  450. chart.update(data)
  451. ```
  452. where `data` is the `data` part of the initial options object.
  453. Let's make this work...
  454. ### Refactor the JS-side data processing code
  455. `r github_sha_link("b19e33af8fdca579a8578bcd7a39c6d1e43fb32c")`
  456. - Create a `prepareChartData()` function from the code we wrote for
  457. `renderValue()`. The goal is that this will let us use the function in
  458. multiple places.
  459. #### Make `chart` generally available
  460. `r github_sha_link("d114592668ca63f06f593f4f247432eec218894b")`
  461. Make the created `chart` object available outside `renderValue()`
  462. #### Expose the context inside the factory function to the world (and yourself)
  463. `r github_sha_link("f0a3bf9fd5e60cda9b2b7ace004f360c36bf6610")`
  464. - bind the factory function context to `el` as `widget`
  465. - Demo this by opening a rendered widget and showing `widget` as attached to the div
  466. #### Expose `chart` with a `chart()` method
  467. `r github_sha_link("f0a3bf9fd5e60cda9b2b7ace004f360c36bf6610")`
  468. Add a chart method to the widget object so that we can get to the current chart.
  469. 1. Demo by finding widget div and running
  470. ```
  471. let c = $0.widget.chart()
  472. c.addDataPoint(2017, [2500, 1500])
  473. ```
  474. 1. Now, if nothing else, the `chart` object is accessible
  475. so others can use or extend it.
  476. #### Create an update method that takes new data and updates an existing chart
  477. `r github_sha_link("5da4b68b5f60d8e6ee17cc8c4a009121539a2653")`
  478. Demo with `app.R`
  479. ```js
  480. let el = document.getElementById('chart')
  481. el.widget.update({x: ['A', 'B', 'C', 'D'], Frequency: [1, 2, 3, 4]})
  482. ```
  483. Try with various values. You can increase the number of data points
  484. but you can't add or change the series.
  485. #### Add a custom message handler that dependes on `HTMLWidgets.shinyMode`
  486. `r github_sha_link("5da4b68b5f60d8e6ee17cc8c4a009121539a2653")`
  487. ```js
  488. // after factory function
  489. if (HTMLWidgets.shinyMode) {
  490. Shiny.addCustomMessageHandler('frappeCharts:update', function({id, data}) {
  491. let el = document.getElementById(id)
  492. el.widget.update(data)
  493. })
  494. }
  495. ```
  496. Restructure the app code so that the chart initializes with flat data (0.5).
  497. Use `session$sendCustomMessage` to trigger the update.
  498. Note that the JS function above takes `id` and `data` using destructuring.
  499. It's easy to write `function(id, data)` but this won't work because
  500. the handler can only take one argument.
  501. Demo the app, now updates are fast!
  502. #### Write a user-friendly wrapper around `sendCustomMessage` called `updateFrappeChart()`
  503. `r github_sha_link("4706d89183aaa9a3721599ef13c6f7af4955808b")`
  504. #### Now add an event listener to send chart navigation back to Shiny
  505. `r github_sha_link("0b4f7ea16f378ec5a53d81260c8f9056fabbcaba")`
  506. Attach the event listener during `renderValue()` and watch for the `data-select` event.
  507. Use the `el.id` to create a new id, like `el.id + '_selected'`.
  508. Send back `index` and `values` from the event.
  509. Add `verbatimTextOutput('selected')` to show `input$chart_selected`.
  510. #### Return better values
  511. `r github_sha_link("e7fe0e1d87977823e6a040434b33e6d5cdf8eac1")`
  512. You would probably want to do some work for the user and return more meaningful values.
  513. We'll probably just copy and paste this during the workshop,
  514. but here's a potential method.
  515. This function basically reverses the chart processing and
  516. and returns a list that should be a dataframe.
  517. ```js
  518. if (HTMLWidgets.shinyMode && x.isNavigable) {
  519. el.addEventListener('data-select', function(ev) {
  520. let {index, values} = ev
  521. let chart = el.widget.chart()
  522. let label = chart.data.labels[index]
  523. let names = chart.data.datasets.map(d => d.name)
  524. let data = values.reduce(function(acc, v, idx) {
  525. acc[names[idx]] = v
  526. return acc
  527. }, {})
  528. data[labelsName] = label
  529. Shiny.setInputValue(el.id + '_selected', data)
  530. })
  531. }
  532. ```
  533. #### Process the returned data for the user in Shiny
  534. `r github_sha_link("000de60582f277e29983f6c5803de112ca1ade99")`
  535. But now in Shiny it needs to go from a list to a data.frame.
  536. To do this we use `shiny::registerInputHandler()` in R and
  537. give the input event a type: `inputId_selected:frappeCharts-selected`.
  538. ```r
  539. .onLoad <- function(libname, pkgname) {
  540. shiny::registerInputHandler(
  541. type = "frappeCharts-selected",
  542. fun = function(value, session, inputName) {
  543. as.data.frame(value, stringsAsFactors = FALSE)
  544. }
  545. )
  546. }
  547. ```