-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.Rmd
273 lines (181 loc) · 5.23 KB
/
index.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
---
title: "Creating a React, Highcharts, htmlwidget in R - using packer and highcharter"
date: "Updated Last: `r Sys.Date()`"
output:
rmdformats::downcute:
self_contained: true
default_style: "light"
downcute_theme: "chaos"
thumbnails: false
editor_options:
chunk_output_type: console
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE,eval = FALSE,messages = FALSE)
library(tidyverse)
library(highcharter)
```
# what-we-dev
Today we are going to build a `React.js`, `Highcharts.js`, `htmlwidget.R` using `packer` & `highcharter`.
```{r,eval = TRUE,echo=FALSE}
hc <- economics_long %>%
mutate(date = datetime_to_timestamp(date)) %>%
hchart("area", hcaes(date, value01, group = variable)) %>%
hc_xAxis(type = "datetime") %>%
hc_plotOptions(area = list(stacking = "normal")) %>%
hc_legend(layout = "proximate", align = "right", labelFormatter = JS("function () {
ydat = this.yData;
return this.name + ' ' + ydat[ydat.length-1].toFixed(2);
}"))
react.highcharts::reactHC(
options = hc$x$hc_opts
)
```
# create-pkg
We start by creating a package.
```{r}
usethis::create_package("~/r_projects/react.highcharts")
```
# init-widget
Once our package loads, we initialize a widget via packer.
```{r}
packer::scaffold_widget("reactHC")
```
# npm-install
Install React
- add react & babel
- add highcharts react
```{r}
packer:::apply_react(FALSE)
packer::npm_install("highcharts-react-official",scope = "prod")
```
# react-hc
Our first goal is to get to a reprex (reproducible example) `we can always improve it`.
We add in the example react-highcharts code into the widget scaffolding.
```{js}
import "widgets";
import React from "react";
import { render } from "react-dom";
import Highcharts from "highcharts";
import HighchartsReact from "highcharts-react-official";
HTMLWidgets.widget({
name: "reactHC",
type: "output",
factory: function (el, width, height) {
return {
renderValue: function (x) {
const App = () => (
<div>
<HighchartsReact highcharts={Highcharts} />
</div>
);
render(<App />, document.getElementById(el.id));
},
};
}
});
```
# bundle-webpack
- Bundle the Widget
- document & install the R pkg
```{r}
packer::bundle()
# ✓ Bundled
devtools::document()
devtools::install()
```
# Hooray! Our React plot works!
```{r}
react.highcharts::reactHC()
```
# ![Highcharts React htmlwidget](images/Screen%20Shot%202022-11-25%20at%203.48.12%20PM.png)improving-it-a-bit
## example-data
For this we will use the `ggplot2` data set economics.
```{r,eval = TRUE}
head(ggplot2::economics)
```
## highcharter
Within the existing R implementation - `highcharter`, we can create a quick chart with a javascript legend placement, like the below:
```{r,eval=TRUE}
library(tidyverse)
library(highcharter)
hc <- economics_long %>%
mutate(date = datetime_to_timestamp(date)) %>%
hchart("area", hcaes(date, value01, group = variable)) %>%
hc_xAxis(type = "datetime") %>%
hc_plotOptions(area = list(stacking = "normal")) %>%
hc_legend(layout = "proximate", align = "right", labelFormatter = JS("function () {
ydat = this.yData;
return this.name + ' ' + ydat[ydat.length-1].toFixed(2);
}"))
hc
```
## opts-export
we can then export the options into `JSON` directly via the R object, or alternatively export the `javascript`
```{r,eval=TRUE}
listviewer::jsonedit(
hc$x$hc_opts
)
# export JS options
usethis::use_directory("trash")
highcharter::export_hc(hc,filename = "trash/example-export.js")
```
## jsx-mod
We modify our `reactHC.jsx` file to accept the options
```{js,eval = FALSE}
renderValue: function (x) {
const options = x.options;
...
<HighchartsReact
highcharts={Highcharts}
options={options}/>
...
}
```
## R-mod
& modify our R function to pass these options to `react` & `javascript`
```{r}
reactHC <- function(options = NULL, width = NULL, height = NULL, elementId = NULL) {
# forward options using x
x = list(
options = options
)
# create widget
htmlwidgets::createWidget(
name = 'reactHC',
x,
width = width,
height = height,
package = 'react.highcharts',
elementId = elementId
)
}
```
# webpack-bundle
We then repeat the webpack (& R) bundle, document, install process
```{r}
packer::bundle()
devtools::document()
devtools::install()
```
## test-viola
Test & ... Viola!
We now have a React & Highcharts htmlwidget that we can use in R, Shiny, rmarkdown, quarto docs or anywhere you use R (or generally any of the other `64` languages - I'm certain I'm short on the count) that posit supports :) )
**Open Q:** I do wonder, is there a way for htmlwidgets to interoperate with JS? Can a `npm` package use a `npmR` package? `If you know, would you PM me?`
```{r,eval=TRUE}
react.highcharts::reactHC(
options = hc$x$hc_opts
)
```
While I plan to explore this further, that's all for now!
The source code can be found <a href = "https://github.com/zac-garland/react.highcharts" target="_blank"> Here </a>
```{css,eval=TRUE}
.jsoneditor.jsoneditor-mode-tree{
background: #ffffff;
}
```
```{js,eval=TRUE}
$(function(){
$("#toc-highcharts-react-htmlwidgetimproving-it-a-bit > img").remove();
})
```