Skip to content

Commit 4a5baa7

Browse files
authored
Merge pull request #120 from casperhart/convert-to-single-widget
convert to single widget
2 parents dda2035 + aedc110 commit 4a5baa7

File tree

70 files changed

+880
-1018
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+880
-1018
lines changed

NAMESPACE

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ export(clear_highlight)
99
export(clear_points)
1010
export(dependence_tour)
1111
export(detour)
12-
export(displayScatter2dOutput)
13-
export(displayScatter3dOutput)
14-
export(display_scatter_proxy)
12+
export(detourOutput)
13+
export(detour_proxy)
1514
export(enlarge_points)
1615
export(force_rerender)
1716
export(frozen_guided_tour)
@@ -24,8 +23,7 @@ export(is_detour)
2423
export(little_tour)
2524
export(local_tour)
2625
export(planned_tour)
27-
export(shinyRenderDisplayScatter2d)
28-
export(shinyRenderDisplayScatter3d)
26+
export(shinyRenderDetour)
2927
export(show_sage)
3028
export(show_scatter)
3129
export(show_slice)

R/shiny_bindings.r

Lines changed: 32 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
#' Shiny bindings for detourr
32
#'
43
#' Output and render functions for using detourr with shiny. The output
@@ -13,31 +12,15 @@
1312
#'
1413
#' @return An output or render function that enables the use of the widget
1514
#' within shiny applications
16-
#'
15+
#'
1716
#' @name detour-shiny
1817
#' @export
19-
displayScatter3dOutput <- function(output_id,
20-
width = "100%",
21-
height = "400px") {
22-
htmltools::attachDependencies(
23-
shiny::tagList(
24-
htmlwidgets::shinyWidgetOutput(output_id, "show_scatter_3d",
25-
width, height,
26-
package = "detourr"
27-
)
28-
),
29-
crosstalk::crosstalkLibs()
30-
)
31-
}
32-
33-
#' @rdname detour-shiny
34-
#' @export
35-
displayScatter2dOutput <- function(output_id,
36-
width = "100%",
37-
height = "400px") {
18+
detourOutput <- function(output_id,
19+
width = "100%",
20+
height = "400px") {
3821
htmltools::attachDependencies(
3922
shiny::tagList(
40-
htmlwidgets::shinyWidgetOutput(output_id, "show_scatter_2d",
23+
htmlwidgets::shinyWidgetOutput(output_id, "detourr",
4124
width, height,
4225
package = "detourr"
4326
)
@@ -48,40 +31,31 @@ displayScatter2dOutput <- function(output_id,
4831

4932
#' @rdname detour-shiny
5033
#' @export
51-
shinyRenderDisplayScatter2d <- function(expr, env = parent.frame(), quoted = FALSE) {
34+
shinyRenderDetour <- function(expr, env = parent.frame(), quoted = FALSE) {
5235
if (!quoted) {
5336
expr <- substitute(expr)
5437
}
55-
htmlwidgets::shinyRenderWidget(expr, displayScatter2dOutput, quoted = TRUE, env = env)
56-
}
57-
58-
#' @rdname detour-shiny
59-
#' @export
60-
shinyRenderDisplayScatter3d <- function(expr, env = parent.frame(), quoted = FALSE) {
61-
if (!quoted) {
62-
expr <- substitute(expr)
63-
}
64-
htmlwidgets::shinyRenderWidget(expr, displayScatter3dOutput, quoted = TRUE, env = env)
38+
htmlwidgets::shinyRenderWidget(expr, detourOutput, quoted = TRUE, env = env)
6539
}
6640

6741
#' Send commands to a detourr instance in a Shiny app
6842
#'
6943
#' Creates a proxy object that can be used to add
7044
#' or remove points to a detour instance that has
71-
#' already being rendered using \code{\link{shinyRenderDisplayScatter3d}}.
45+
#' already being rendered using \code{\link{shinyRenderDetour}}.
7246
#' To be used in Shiny apps only.
73-
#' @param id output id of the detourr instance
47+
#' @param id output id of the detour instance
7448
#' @param session the Shiny session object used in the app.
7549
#' Default should work for most cases
7650
#'
7751
#' @rdname detour-shiny
7852
#' @export
79-
display_scatter_proxy <- function(id, session = shiny::getDefaultReactiveDomain()) { #nolint
80-
structure(list(id = id, session = session), class = "detourr_proxy")
53+
detour_proxy <- function(id, session = shiny::getDefaultReactiveDomain()) { # nolint
54+
structure(list(id = id, session = session), class = "detour_proxy")
8155
}
8256

8357
#' @title Add a set of points to an existing detourr instance in Shiny
84-
#' @param proxy Proxy object created by \code{\link{display_scatter_proxy}}
58+
#' @param proxy Proxy object created by \code{\link{detour_proxy}}
8559
#' @param points Data.frame of points
8660
#' @param .data Original dataset used in creating the detourr instance
8761
#' @param .col_means Vector of column means of the original dataset.
@@ -95,15 +69,14 @@ display_scatter_proxy <- function(id, session = shiny::getDefaultReactiveDomain(
9569
#' @rdname detour-shiny
9670
#' @export
9771
add_points <- function(
98-
proxy,
99-
points,
100-
.data = NULL,
101-
.col_means = NULL,
102-
.scale_factor = NULL,
103-
colour = "black",
104-
size = 1,
105-
alpha = 1
106-
) {
72+
proxy,
73+
points,
74+
.data = NULL,
75+
.col_means = NULL,
76+
.scale_factor = NULL,
77+
colour = "black",
78+
size = 1,
79+
alpha = 1) {
10780
if (is.null(.data)) {
10881
if (is.null(.col_means) || is.null(.scale_factor)) {
10982
cli::cli_abort(c(
@@ -144,7 +117,7 @@ add_points <- function(
144117

145118
#' @title Function to add a bunch of lines to existing shiny instance
146119
#'
147-
#' @param proxy Proxy object created by \code{\link{display_scatter_proxy}}
120+
#' @param proxy Proxy object created by \code{\link{detour_proxy}}
148121
#' @param edge_list Data.frame with two columns with the `from` node at first.
149122
#' The indexing of points starts with the original dataset.
150123
#' If \code{\link{add_points}} has been called before hand,
@@ -153,7 +126,9 @@ add_points <- function(
153126
#' @rdname detour-shiny
154127
#' @export
155128
add_edges <- function(proxy, edge_list) {
156-
edge_list <- edge_list |> as.matrix() |> unname()
129+
edge_list <- edge_list |>
130+
as.matrix() |>
131+
unname()
157132
proxy$message$edges <- apply(edge_list, 1, as.list)
158133
proxy$session$sendCustomMessage("add-edges", proxy$message)
159134
return(proxy)
@@ -164,7 +139,7 @@ add_edges <- function(proxy, edge_list) {
164139
#' The given points will have the original opacity while the other points
165140
#' will have reduced opacity
166141
#'
167-
#' @param proxy proxy object created by \code{\link{display_scatter_proxy}}
142+
#' @param proxy proxy object created by \code{\link{detour_proxy}}
168143
#' @param point_list Numeric vector. indexes to highlight in the prinary dataset
169144
#' @param alpha The transparency value of the points outside of the point_list
170145
#' @rdname detour-shiny
@@ -183,7 +158,7 @@ highlight_points <- function(proxy, point_list, alpha = 0.3) {
183158
#' The given points will have a larger size while the rest
184159
#' remains the same
185160
#'
186-
#' @param proxy proxy object created by \code{\link{display_scatter_proxy}}
161+
#' @param proxy proxy object created by \code{\link{detour_proxy}}
187162
#' @param point_list Numeric vector. indexes to enlarge in the prinary dataset
188163
#' @param size the size of the points to be enlarged
189164
#' @rdname detour-shiny
@@ -200,7 +175,7 @@ enlarge_points <- function(proxy, point_list, size = 2) {
200175

201176

202177
#' Function to clear added points
203-
#' @param proxy proxy object created by \code{\link{display_scatter_proxy}}
178+
#' @param proxy proxy object created by \code{\link{detour_proxy}}
204179
#' @rdname detour-shiny
205180
#' @export
206181
clear_points <- function(proxy) {
@@ -209,7 +184,7 @@ clear_points <- function(proxy) {
209184
}
210185

211186
#' Function to clear added edges
212-
#' @param proxy proxy object created by \code{\link{display_scatter_proxy}}
187+
#' @param proxy proxy object created by \code{\link{detour_proxy}}
213188
#' @rdname detour-shiny
214189
#' @export
215190
clear_edges <- function(proxy) {
@@ -218,7 +193,7 @@ clear_edges <- function(proxy) {
218193
}
219194

220195
#' Function to clear highlighted points
221-
#' @param proxy proxy object created by \code{\link{display_scatter_proxy}}
196+
#' @param proxy proxy object created by \code{\link{detour_proxy}}
222197
#' @rdname detour-shiny
223198
#' @export
224199
clear_highlight <- function(proxy) {
@@ -227,7 +202,7 @@ clear_highlight <- function(proxy) {
227202
}
228203

229204
#' Function to clear enlarged points
230-
#' @param proxy proxy object created by \code{\link{display_scatter_proxy}}
205+
#' @param proxy proxy object created by \code{\link{detour_proxy}}
231206
#' @rdname detour-shiny
232207
#' @export
233208
clear_enlarge <- function(proxy) {
@@ -238,10 +213,10 @@ clear_enlarge <- function(proxy) {
238213
#' Function to force rerender of detourr
239214
#'
240215
#' Useful when detourr will not update unless put on focus
241-
#' @param proxy proxy object created by \code{\link{display_scatter_proxy}}
216+
#' @param proxy proxy object created by \code{\link{detour_proxy}}
242217
#' @rdname detour-shiny
243218
#' @export
244219
force_rerender <- function(proxy) {
245220
proxy$session$sendCustomMessage("clear-enlarge", list(id = proxy$id))
246221
return(proxy)
247-
}
222+
}

R/show_sage.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ show_sage <- function(x,
5959

6060
x <- make_detour(x, d)
6161

62-
widget <- paste0("show_sage", "_", tour_output_dim(x), "d")
62+
widget_type <- paste0("DisplaySage", tour_output_dim(x), "d")
6363

64-
make_widget(x, widget, dots$width, dots$height, d$crosstalk$crosstalk_libs)
64+
make_widget(x, widget_type, dots$width, dots$height, d$crosstalk$crosstalk_libs)
6565
}

R/show_scatter.R

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
#' 2D and 3D Scatter Plot Display for Tours
32
#'
43
#' @description
@@ -36,9 +35,9 @@ show_scatter <- function(x,
3635

3736
d <- attributes(x)
3837

39-
widget <- paste0("show_scatter", "_", tour_output_dim(x), "d")
38+
widget_type <- paste0("DisplayScatter", tour_output_dim(x), "d")
4039

41-
make_widget(x, widget, dots$width, dots$height, d$crosstalk$crosstalk_libs)
40+
make_widget(x, widget_type, dots$width, dots$height, d$crosstalk$crosstalk_libs)
4241
}
4342

4443
#' Internal method for 2D and 3D Scatter Plot Display

R/show_slice.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ show_slice <- function(x,
6363

6464
d$config$anchor <- anchor
6565

66-
x <- make_detour(x, d)
66+
widget_type <- paste0("DisplaySlice", tour_output_dim(x), "d")
6767

68-
widget <- paste0("show_slice", "_", tour_output_dim(x), "d")
68+
x <- make_detour(x, d)
6969

70-
make_widget(x, widget, dots$width, dots$height, d$crosstalk$crosstalk_libs)
70+
make_widget(x, widget_type, dots$width, dots$height, d$crosstalk$crosstalk_libs)
7171
}

R/utils.R

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,13 @@ check_dots <- function(dots, supported_arg_names) {
123123
}
124124
}
125125

126-
make_widget <- function(x, widget, width, height, dependencies) {
126+
make_widget <- function(x, widget_type, width, height, dependencies) {
127+
x <- as.list(x)
128+
x$widgetType <- widget_type
129+
127130
htmlwidgets::createWidget(
128-
widget,
129-
as.list(x),
131+
"detourr",
132+
x,
130133
sizingPolicy = htmlwidgets::sizingPolicy(
131134
viewer.padding = 0,
132135
viewer.paneHeight = 500,

demo/shiny_detourr/add_points_edges.R

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ dataset <- tourr::flea |>
66

77
create_fake_box <- function(datum) {
88
# expected tibble needs to have two rows with ncols(datum) + 1 columns
9-
box_dist <- 2
9+
box_dist <- 2
1010
bounds_list <- rbind(datum + box_dist, datum - box_dist) |>
1111
as.list()
1212
do.call(tidyr::expand_grid, bounds_list)
@@ -15,13 +15,16 @@ create_fake_box <- function(datum) {
1515
main_ui <- function(id) {
1616
ns <- NS(id)
1717
fluidPage(
18-
column(6,
19-
displayScatter2dOutput(
20-
ns("detourr_out"), width = "100%", height = "400px"
18+
column(
19+
6,
20+
detourOutput(
21+
ns("detourr_out"),
22+
width = "100%", height = "400px"
2123
),
2224
textOutput(ns("detour_click_output"))
2325
),
24-
column(6,
26+
column(
27+
6,
2528
h1("Adding points and edges to detourr through Shiny"),
2629
p(
2730
"In this demonstration,",
@@ -34,9 +37,10 @@ main_ui <- function(id) {
3437
}
3538

3639
main_server <- function(id) {
37-
moduleServer(id, function(input, output, session){
38-
output$detourr_out <- shinyRenderDisplayScatter2d({
39-
detour(dataset,
40+
moduleServer(id, function(input, output, session) {
41+
output$detourr_out <- shinyRenderDetour({
42+
detour(
43+
dataset,
4044
tour_aes(projection = -c(id, species), colour = species, label = id)
4145
) |>
4246
tour_path(grand_tour(2), fps = 60) |>
@@ -64,7 +68,7 @@ main_server <- function(id) {
6468

6569
cube_box <- geozoo::cube.iterate(p = ncol(data_to_send))
6670

67-
display_scatter_proxy(session$ns("detourr_out")) |>
71+
detour_proxy(session$ns("detourr_out")) |>
6872
add_points(
6973
box_to_send,
7074
.data = dataset |> dplyr::select(-c(id, species))
@@ -73,7 +77,6 @@ main_server <- function(id) {
7377
edge_list = cube_box$edges
7478
)
7579
})
76-
7780
})
7881
}
7982

demo/shiny_detourr/app.r

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ ui <- fluidPage(
1818

1919
# output function used matches display method (show_scatter) and tour
2020
# dimension (3d)
21-
column(8, displayScatter3dOutput("detour", height = "800px"))
21+
column(8, detourOutput("detour", height = "800px"))
2222
)
2323
)
2424

@@ -39,7 +39,7 @@ server <- function(input, output, session) {
3939
)
4040
})
4141

42-
output$detour <- shinyRenderDisplayScatter3d({
42+
output$detour <- shinyRenderDetour({
4343
set.seed(input$seed)
4444

4545
detour(shared_iris, tour_aes(projection = -Species, colour = Species)) |>

demo/shiny_detourr/click_events.R

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@ library(detourr)
44
ui <- function() {
55
fluidPage(
66
fluidRow(
7-
column(6,
8-
displayScatter3dOutput(
7+
column(
8+
6,
9+
detourOutput(
910
"detourr_out",
1011
width = "100%",
1112
height = "400px"
1213
)
1314
),
14-
column(6,
15+
column(
16+
6,
1517
h1("Click events through shiny"),
1618
p(
1719
"This app demonstrates how to obtain click events through Shiny.",
@@ -26,7 +28,7 @@ ui <- function() {
2628
}
2729

2830
server <- function(input, output, session) {
29-
output$detourr_out <- shinyRenderDisplayScatter2d({
31+
output$detourr_out <- shinyRenderDetour({
3032
detour(
3133
tourr::flea |>
3234
dplyr::mutate(id = dplyr::row_number()),
@@ -46,4 +48,4 @@ server <- function(input, output, session) {
4648
})
4749
}
4850

49-
shinyApp(ui, server, options = list(port = 5534))
51+
shinyApp(ui, server, options = list(port = 5534))

0 commit comments

Comments
 (0)