You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The next step is to navigate to the newly-created `colorpicker` project and run
87
-
the following commands in the terminal:
88
+
the following commands in the terminal. If you're new the terminal, we recommend opening your newly created RStudio `~/colorpicker/colorpicker.Rproj` project file, then running the following in the [RStudio terminal tab](https://support.rstudio.com/hc/en-us/articles/115010737148-Using-the-RStudio-Terminal):
88
89
89
90
```
90
91
yarn install
@@ -172,38 +173,42 @@ accomplish these, `react-color` components can [optionally
172
173
take](http://casesandberg.github.io/react-color/#api) the following
accepts a JavaScript function taking a single argument, the new `color`, that
180
+
will be called when the new color is selected
181
+
182
+
Since this React component calls a configurable function (i.e., `onChangeComplete`) when the input (i.e., color) value changes, we can supply a function to inform Shiny about these changes. You could, in theory, do this by writing your own custom Shiny input wrapper around this component, but `reactR` provides some conventions to make it much easier. These conventions have two main parts (R and JavaScript):
183
+
184
+
1. Use `reactR::createReactShinyInput()` to construct the user-facing R input and route any user-supplied options (e.g., the `default` input value and other `configuration`) to the React component. This part was already done for us in the `R/colorpicker.R` file of our colorpicker project:
185
+
186
+
```r
187
+
colorpickerInput<-function(inputId, default="") {
188
+
reactR::createReactShinyInput(
189
+
inputId=inputId,
190
+
class="colorpicker",
191
+
dependencies=htmltools::htmlDependency(
192
+
name="colorpicker-input",
193
+
version="1.0.0",
194
+
src="www/colorpicker/colorpicker",
195
+
package="colorpicker",
196
+
script="colorpicker.js"
197
+
),
198
+
default=default,
199
+
configuration=list(),
200
+
container=htmltools::tags$span
201
+
)
202
+
}
203
+
```
199
204
200
-
So, in order to make the components delivered by `react-color` accessible on the
201
-
R side, we must create our own intermediate component that wraps one of
202
-
`react-color`'s pickers.
205
+
2. Design an *intermediate* React component that routes information from `colorpickerInput()` to the `<SketchPicker>` component and also inform Shiny when a new color is chosen. This intermediate component should be a [functional component](https://reactjs.org/docs/components-and-props.html#function-and-class-components) with three arguments:
203
206
204
-
### Create intermediate component
207
+
*`configuration`: The JSON equivalent of the `configuration` argument from `reactR::createReactShinyInput()`. In this particular example, `configuration` isn't used.
208
+
*`value`: The input's values over time, beginning with the `default` supplied from `reactR::createReactShinyInput()`.
209
+
*`setValue`: A JavaScript function to call with the input's new value when one is created. This function is not user supplied, but rather an internal hook for informing Shiny about changes to the component's current state (i.e. value).
205
210
206
-
Open `srcjs/colorpicker.jsx`and paste the following in:
211
+
Consider the following intermediate component, `PickerInput`. Note how this intermediate component allows one to set the default `value` from R and also calls `setValue()` inside `onChangeComplete` in order to inform Shiny about new color values. Finally, `reactR.reactShinyInput()` registers this intermediate component as a custom Shiny input binding named `colorpicker`.
called `PickerInput` that expects the props supplied by reactR and renders a
227
-
parameterized `SketchPicker` from `react-color`. The `configuration` value is
228
-
not yet used.
229
-
230
-
After saving the file, run `yarn run webpack` in the terminal and rebuild the
231
-
package.
232
-
233
-
## Trying it out
234
-
235
-
After rebuilding the JavaScript and the package, try running `app.R` again. You
236
-
should see something like this:
231
+
Open the `srcjs/colorpicker.jsx` file in your colorpicker project and paste this code into it. After saving the file, run `yarn run webpack` in the terminal, re-install the
0 commit comments