I have
ui <- fluidPage(
# App title ----
titlePanel("Interactive CLicks"),
br(),
fluidRow(
br(),
verbatimTextOutput("info_hover"),
plotOutput(outputId = "distPlot",hover = "plot_hover",click= "plot_click", dblclick = "plot_db_click",height = 500, width = 1600 ),
br(),
verbatimTextOutput("info_click"),
verbatimTextOutput("info_db_click"),
verbatimTextOutput("double_to_single_click"))
)
## server.R
server <- function( input, output, session){
#Click Points
single.source_coords <- reactiveValues(xy=data.frame(x=c(1,1), y=c(1,1)))
double.source_coords <- reactiveValues(xy=data.frame(x=c(1,1), y=c(1,1)))
observeEvent(input$plot_click, {
single.source_coords$xy[2,] <- c(input$plot_click$x, input$plot_click$y)
})
observeEvent(input$plot_click, {
double.source_coords$xy[2,] <- c(input$plot_db_click$x, input$plot_db_click$y)
})
## RenderPlot
output$distPlot <- renderPlot({
plot(1, 1,
xlim=c(0,10), ylim=c(0,10))
#Click points
points( single.source_coords$xy[2,1], single.source_coords$xy[2,2], cex=3, pch=17)
points( double.source_coords$xy[2,1], double.source_coords$xy[2,2], cex=3, pch=17)
segments(x0 =single.source_coords$xy[2,1], y0 = single.source_coords$xy[2,2], x1 = double.source_coords$xy[2,1], y1 = double.source_coords$xy[2,2], col = "darkred", lwd = 2, lty = 3)
})
output$info_click <- renderText({
paste0("Single Click Multiple", "\nX=", input$plot_click$x*2, " Y= ",input$plot_click$y*2)
})
output$info_db_click <- renderText({
paste0("Double Click Multiple", "\nX=", input$plot_db_click$x*2, " Y= ",input$plot_db_click$y*2)
})
output$info_hover <- renderText({
paste0("Hover Click Multiple", "\nX=", input$plot_hover$x*2, " Y= ",input$plot_hover$y*2)
})
}
### Run Application
shinyApp(ui, server)
So I would like click action to add a point, double click to add another point, while maintaining the display of the values in and some operations on the values in verbatimTextOutput()
I can't seem to get it to work. I am getting an Warning: Error in <-: replacement has length zero
Also click and double click place one point only respectively. If I click at (x1,y1) and click at (x1,y2) the point should move from location 1 to location 2.
What am I missing?
CodePudding user response:
I think you had a typo in your second observeEvent
(again input$plot_click
instead of input$plot_db_click
) . That explains your error. Secondly, for your verbatimTextOutput
I think you should refer to the reactive values. Does this work for you, or am I missing your purpose?
library(shiny)
ui <- fluidPage(
# App title ----
titlePanel("Interactive CLicks"),
br(),
fluidRow(
br(),
verbatimTextOutput("info_hover"),
plotOutput(outputId = "distPlot",hover = "plot_hover",click= "plot_click", dblclick = "plot_db_click",height = 500, width = 1600 ),
br(),
verbatimTextOutput("info_click"),
verbatimTextOutput("info_db_click"),
verbatimTextOutput("double_to_single_click"))
)
## server.R
server <- function( input, output, session){
#Click Points
single.source_coords <- reactiveValues(xy=data.frame(x=c(1,1), y=c(1,1)))
double.source_coords <- reactiveValues(xy=data.frame(x=c(1,1), y=c(1,1)))
observeEvent(input$plot_click, {
single.source_coords$xy[2,] <- c(input$plot_click$x, input$plot_click$y)
})
observeEvent(input$plot_db_click, {
double.source_coords$xy[2,] <- c(input$plot_db_click$x, input$plot_db_click$y)
})
## RenderPlot
output$distPlot <- renderPlot({
plot(1, 1,
xlim=c(0,10), ylim=c(0,10))
#Click points
points( single.source_coords$xy[2,1], single.source_coords$xy[2,2], cex=3, pch=17)
points( double.source_coords$xy[2,1], double.source_coords$xy[2,2], cex=3, pch=17)
segments(x0 =single.source_coords$xy[2,1], y0 = single.source_coords$xy[2,2], x1 = double.source_coords$xy[2,1], y1 = double.source_coords$xy[2,2], col = "darkred", lwd = 2, lty = 3)
})
output$info_click <- renderText({
paste0("Single Click Multiple", "\nX=", single.source_coords$xy[2,1], " Y= ",single.source_coords$xy[2,2])
})
output$info_db_click <- renderText({
paste0("Double Click Multiple", "\nX=", double.source_coords$xy[2,1], " Y= ",double.source_coords$xy[2,2])
})
output$info_hover <- renderText({
paste0("Hover Click Multiple", "\nX=", input$plot_hover$x*2, " Y= ",input$plot_hover$y*2)
})
}
### Run Application
shinyApp(ui, server)