-
-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathshiny.Rmd
More file actions
199 lines (157 loc) · 5.17 KB
/
Copy pathshiny.Rmd
File metadata and controls
199 lines (157 loc) · 5.17 KB
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
---
title: "Shiny"
author: "John Coene"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Shiny}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r setup, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)
library(echarts4r)
e_common(
font_family = "Raleway"
)
```
<div class="panel panel-default">
<div class="panel-heading">New Features</div>
<div class="panel-body">
Use (almost) any function on a dynamic proxy!
</div>
<div class="panel-footer">
<a href="development" class="btn btn-warning">
Documentation
</a>
</div>
</div>
`echarts4r` comes with proxies (functions ending in `_p`) as well as the ability access selected data. You will find a demo of shiny working with `echarts4r` [here](http://shiny.john-coene.com/echarts4rShiny/).
<a href = "http://shiny.john-coene.com/echarts4rShiny/" target ="_blank" class = "btn btn-default">
Shiny Demo
</a>
## Deploy
A note if you are publishing on [shinyapps.io](https://www.shinyapps.io/), if you have _locally_ installed the development version of the package from [Github](https://github.com/JohnCoene/echarts4r) with `remotes` as indicated on the [homepage](/) of the website please reinstall it `devtools`. Otherwise shinyapps.io will intall the CRAN version which might lead to issues.
```r
# install.packages("devtools")
devtools::install_github("JohnCoene/echarts4r")
```
If you are using the [Shiny Server](), remember to install the package on the respective server with the following command:
```
# stable version
sudo su - -c "R -e \"install.packages('echarts4r', repos = 'https://cran.rstudio.com')\""
# dev version
sudo su - -c "R -e \"remotes::install_github('JohnCoene/echarts4r')\""
```
## Events
Get data from shiny with the following:
* `elementId` + `_brush` - returns data on brushed data points.
* `elementId` + `_legend_change` - returns series name of legend selected/unselected.
* `elementId` + `_legend_selected` - returns list of selected status for each series.
* `elementId` + `_clicked_data` - returns data of clicked data point.
* `elementId` + `_clicked_data_value` - returns value of clicked data point.
* `elementId` + `_clicked_row` - returns row number of clicked data point.
* `elementId` + `_clicked_serie` - returns name of serie of clicked data point.
* `elementId` + `_mouseover_data` - returns data on hovered data point.
* `elementId` + `_mouseover_data_value` - returns value of hovered data point.
* `elementId` + `_mouseover_row` - returns row o hovered data point.
* `elementId` + `_mouseover_serie` - returns name of serie of hovered data point.
If you want to implement a missing callback with `e_distpatch_action_p`.
[Paul Simmering](https://github.com/psimm) kindly created a demo app showcasing callbacks.
<a href = "https://psim.shinyapps.io/echarts4r_callbacks/" target ="_blank" class = "btn btn-default">
Callbacks demo
</a>
## Proxies
Interact with the charts without redrawing (proxies):
* `e_append1_p` and `e_append2_p` - to add data to your chart.
* `e_highlight_p` and `e_downplay_p` - to highlight or downplay a serie.
* `e_showtip_p` and `e_hidetip_p` - to Show or hide the tooltip.
* `e_draw_p` to draw the chart, see function documentation for examples.
* `e_focus_adjacency` and `e_unfocus_adjacency` - to focus or unfocus on adjacent nodes (networks).
* `e_mark_p` to dynamically mark points, lines, and areas.
See the example below and the various proxies documentation.
```{r, eval=FALSE}
library(shiny)
library(echarts4r)
ui <- fluidPage(
actionButton("add", "Add Data to y"),
echarts4rOutput("plot"),
verbatimTextOutput("selected")
)
server <- function(input, output, session){
data <- data.frame(x = rnorm(10, 5, 3), y = rnorm(10, 50, 12), z = rnorm(10, 50, 5))
react <- eventReactive(input$add, {
set.seed(sample(1:1000, 1))
data.frame(x = rnorm(10, 5, 2), y = rnorm(10, 50, 10))
})
output$plot <- renderEcharts4r({
data |>
e_charts(x) |>
e_scatter(y) |>
e_scatter(z) |>
e_brush(throttleDelay = 1000)
})
observeEvent(input$add, {
echarts4rProxy("plot") |>
e_append1_p(0, react(), x, y)
})
output$selected <- renderPrint({
input$plot_brush
})
}
shinyApp(ui, server)
```
## Loading
You can also show a spinner while shiny recalculates.
Without loading, chart redraws with neat animation.
```{r, eval=FALSE}
# no redraw
# no loading
library(shiny)
ui <- fluidPage(
fluidRow(
column(12, actionButton("update", "Update"))
),
fluidRow(
column(12, echarts4rOutput("plot"))
)
)
server <- function(input, output){
data <- eventReactive(input$update, {
data.frame(
x = 1:10,
y = rnorm(10)
)
})
output$plot <- renderEcharts4r({
data() |>
e_charts(x) |>
e_bar(y)
})
}
shinyApp(ui, server)
```
With loading.
```{r, eval=FALSE}
# keep UI
# add loading
server <- function(input, output){
data <- eventReactive(input$update, {
Sys.sleep(1) # sleep one second to show loading
data.frame(
x = 1:10,
y = rnorm(10)
)
})
output$plot <- renderEcharts4r({
data() |>
e_charts(x) |>
e_bar(y) |>
e_show_loading()
})
}
shinyApp(ui, server)
```