Skip to content

Commit ffef491

Browse files
committed
don't namespace under-the-hood
1 parent 4d8788b commit ffef491

File tree

4 files changed

+23
-17
lines changed

4 files changed

+23
-17
lines changed

R/plotly.R

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,7 @@
7878
plot_ly <- function(data = data.frame(), ..., type = NULL,
7979
color, colors = NULL, alpha = 1, symbol, symbols = NULL,
8080
size, sizes = c(10, 100), linetype, linetypes = NULL,
81-
width = NULL, height = NULL, source = "A",
82-
session = shiny::getDefaultReactiveDomain()) {
81+
width = NULL, height = NULL, source = "A") {
8382
if (!is.data.frame(data)) {
8483
stop("First argument, `data`, must be a data frame.", call. = FALSE)
8584
}
@@ -120,10 +119,7 @@ plot_ly <- function(data = data.frame(), ..., type = NULL,
120119
id <- new_id()
121120
# avoid weird naming clashes
122121
plotlyVisDat <- data
123-
# automatically namespace source
124-
if (!is.null(session)) {
125-
source <- session$ns(source)
126-
}
122+
127123
p <- list(
128124
visdat = setNames(list(function() plotlyVisDat), id),
129125
cur_data = id,

R/shiny.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ event_data <- function(event = c("plotly_hover", "plotly_click", "plotly_selecte
5454
stop("No reactive domain detected. This function can only be called \n",
5555
"from within a reactive shiny context.")
5656
}
57-
src <- sprintf(".clientValue-%s-%s", event[1], session$ns(source))
57+
src <- sprintf(".clientValue-%s-%s", event[1], source)
5858
val <- session$rootScope()$input[[src]]
5959
if (is.null(val)) val else jsonlite::fromJSON(val)
6060
}

inst/examples/plotlyShinyModules/app.R

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,31 @@ reusableUI <- function(id = NULL) {
77
fluidRow(
88
column(4, plotlyOutput(ns("p1"))),
99
column(4, plotlyOutput(ns("p2"))),
10-
column(4, verbatimTextOutput(ns("ev")))
10+
column(4, verbatimTextOutput(ns("ev1"))),
11+
column(4, verbatimTextOutput(ns("ev2")))
1112
)
1213
}
1314

14-
viz <- function(input, output, session) {
15+
viz <- function(input, output, scope, src) {
16+
17+
# if you want, you can define multiple sources here
18+
src2 <- paste0(src, "2")
19+
1520
output$p1 <- renderPlotly({
1621
plot_ly(mtcars, x = ~mpg, y = ~disp,
17-
key = row.names(mtcars), session = session)
22+
key = row.names(mtcars), source = src)
1823
})
1924
output$p2 <- renderPlotly({
2025
plot_ly(mtcars, x = ~mpg, y = ~disp,
21-
key = row.names(mtcars), session = session)
26+
key = row.names(mtcars), source = src2)
2227
})
23-
output$ev <- renderPrint({
24-
d <- event_data("plotly_hover", session = session)
25-
if (is.null(d)) print(paste("Module", session$ns(NULL))) else d
28+
output$ev1 <- renderPrint({
29+
event_data("plotly_hover", source = src)
2630
})
31+
output$ev2 <- renderPrint({
32+
event_data("plotly_hover", source = src2)
33+
})
34+
2735
}
2836

2937
ui <- fluidPage(
@@ -32,8 +40,9 @@ ui <- fluidPage(
3240
)
3341

3442
server <- function(input, output, session) {
35-
callModule(viz, "one", session = session)
36-
callModule(viz, "two", session = session)
43+
# use the src argument to namespace plotly events
44+
callModule(viz, "one", src = "A")
45+
callModule(viz, "two", src = "B")
3746
}
3847

3948
shinyApp(ui, server)

man/event_data.Rd

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)