Great reprex! And thank you for the main functions to look at!
There is a javascript method that Shiny adds called Shiny.setInputValue(key, value). You can read more about it here: https://shiny.rstudio.com/articles/communicating-with-js.html
In the updated example, I used the dyCallbacks(clickCallback = "JavaScriptCode") functionality to set a callback that will call Shiny.setInputValue. The key used in Shiny.setInputValue can then be read as a regular input in the application.
Once the value is retrieved, it is standard shiny code.
library(shiny)
library(dygraphs)
X <- c(1,2,3,4,5,6,7,8,9,10)
Y<-c(100,200,250,267,234,88,78,90,15,32)
data <- data.frame(X,Y)
ui <- fluidPage(
titlePanel("Test"),
sidebarLayout(
sidebarPanel(
### Add a status area to display output
verbatimTextOutput("status")
),
mainPanel(
dygraphOutput("plot")
)
)
)
server <- function(input, output) {
output$plot <- renderDygraph({
p <-
dygraph(data, xlab = "X", ylab = "Y") %>%
### Add a `clickCallback` that will set the shiny input value.
### With JavaScript, you can look at all the arguments supplied to the function.
### For the click callback, the arguments are the Event, some location information, all location information.
### JavaScript is a 0 based counting language, so we pull the '3rd' value from the '2nd' position
dyCallbacks(clickCallback = "function() { console.log(arguments); Shiny.setInputValue('dygraph_click', arguments[2]); }") %>%
dyRangeSelector()
})
### Make sure the input value is a list
dygraph_click <- reactive({
as.list(input$dygraph_click)
})
### Extract possibly useful information
clickX <- reactive({dygraph_click()$xval})
clickY <- reactive({dygraph_click()$yval})
### Print the information
printVal <- reactive({
paste0(capture.output({str(dygraph_click())}), collapse = "\n")
})
### Display the information
output$status <- renderText({
paste0(
"X: ", clickX(), "\n",
"Y: ", clickY(), "\n",
"Object:\n",
printVal()
)
})
}
shinyApp(ui = ui, server = server)