0

In this example - observeEvent(input$plot_dblclick{code}) having some problem. Couldn't able to double-click after brush. It gives no error message either. Can anyone help me to find the problem? You can find input files here.

ui <- fluidPage(

  titlePanel("Example"),

  sidebarLayout(
    sidebarPanel(

      textInput("numb", "Entre a id between G1-G19:"),
      actionButton("find", "Find")

    ),

    mainPanel(

      tabsetPanel(type = "tabs",
                  tabPanel("Interactive Plot", 
                           plotOutput("plot", click = "plot_click",
                                      height = 300,
                                      dblclick = "plot_dblclick",
                                      brush = brushOpts(
                                        id = "plot_brush",
                                        resetOnNew = TRUE
                                      )),
                           verbatimTextOutput("info")

                  ),

                  tabPanel("Table", dataTableOutput("table"))

      ) #tabstPanel ends here

    ) # mainPanel ends here
  ) 

)

I guess the recursive observeEvent object in bellow script not able to call in ui.

server <- function(input, output) {

  # Loading packages
  library(data.table)
  library(maptools)
  library(maps)
  library(ggmap)
  library(ggplot2)
  library(plyr)

  observeEvent(input$find, { # "Find" button event

      area_density <- read.csv("density.csv", sep = ",", row.names = 1)
      cordinates <- read.csv("cordinate.csv", sep = ",")

      input_id <- input$numb
      area_density_t <- t(area_density[input_id,])
      area_density_t_df <- as.data.frame(area_density_t)
      area_density_t_df_data <- setDT(area_density_t_df, keep.rownames = TRUE)[]
      colnames(area_density_t_df_data)[1] <- "id"

      final_table <- merge(x=area_density_t_df_data, y=cordinates, by= "id", all=TRUE)
      colnames(final_table)[2] <- "density"

      top3 <- head(arrange(final_table, desc(final_table$density)) , n = 3)
      last3 <- tail(arrange(final_table, desc(final_table$density)) , n = 3)


      ############## Map ####################

      ranges <- reactiveValues(x = NULL, y = NULL)
      # Generating Map
      mapWorld <- borders("world", colour="gray50", fill="gray50") # create a layer of borders
      mp <- ggplot() +   mapWorld
      mp <- mp+ geom_point(aes(x=final_table$longitude, y=final_table$latitude) ,color="blue", size=3)+
                coord_cartesian(xlim = ranges$x, ylim = ranges$y, expand = FALSE) 
      mp <- mp+ geom_point(aes(x=top3$longitude, y=top3$latitude) ,color="red", size=3)
      mp <- mp+ geom_point(aes(x=last3$longitude, y=last3$latitude) ,color="green", size=3)


      # Printing Map to screen
      output$plot <- renderPlot({
        mp
      })

      ################# Interactive Plot (Map) modifications #################

      # Plot Click
      output$info <- renderText({
        paste("\nLongitude=", input$plot_click$x, 
              "\nLatitude=", input$plot_click$y
        )
      })

      # Zoom
      # When a double-click happens, check if there's a brush on the plot.
      # If so, zoom to the brush bounds; if not, reset the zoom.
      observeEvent(input$plot_dblclick, {
        brush <- input$plot_brush
        if (!is.null(brush)) {
          ranges$x <- c(brush$xmin, brush$xmax)
          ranges$y <- c(brush$ymin, brush$ymax)

        } else {
          ranges$x <- NULL
          ranges$y <- NULL
        }
      })

      ############ Printing the table to screen on table tab #########
       output$table = renderDataTable({
        final_table
      })

  })  # observeEvent ends here

} # Server fucntion ends here
3
  • Do your observers have to be nested? Have you tried moving the observer for plot_dbclick out of the observer for find? Commented Aug 29, 2018 at 3:53
  • If your observers have to be nested, then please post a minimal example that just focuses on two nested observers. Right now there are lots of lines of code in your example that may not relate to the problem. Commented Aug 29, 2018 at 3:55
  • Yes, the observeEvent have to be nested. because the second one is supposed to work only after the first one. And I'm not sure if that is the case for the code not to work, that's why I gave the whole code if someone tries to find other dependency errors. Commented Aug 29, 2018 at 4:12

1 Answer 1

1

you should look in to how the different types of observers and reactives really are working. It is a bit different from other functions in R. The nesting that you are tryning has no real effect. I have pulled your code apart removed all the nestings and it works quite nice.

ui <- fluidPage(

  titlePanel("Example"),

  sidebarLayout(
    sidebarPanel(

      textInput("numb", "Entre a id between G1-G19:"),
      actionButton("find", "Find")

    ),

    mainPanel(

      tabsetPanel(type = "tabs",
                  tabPanel("Interactive Plot", 
                           plotOutput("plot", click = "plot_click",
                                      height = 300,
                                      dblclick = "plot_dblclick",
                                      brush = brushOpts(
                                        id = "plot_brush",
                                        resetOnNew = TRUE
                                      )),
                           verbatimTextOutput("info")

                  ),

                  tabPanel("Table", dataTableOutput("table"))

      ) #tabstPanel ends here

    ) # mainPanel ends here
  ) 

)

  # Loading packages outside of server
  library(data.table)
  library(maptools)
  library(maps)
  library(ggmap)
  library(ggplot2)
  library(plyr)
server <- function(input, output) {

  ranges <- reactiveValues(x = NULL, y = NULL)

  mp_table <- eventReactive(input$find, { # "Find" button event

    area_density <- read.csv("~/Downloads/density.csv", sep = ",", row.names = 1)
    cordinates <- read.csv("~/Downloads/cordinate.csv", sep = ",")

    input_id <- input$numb
    area_density_t <- t(area_density[input_id,])
    area_density_t_df <- as.data.frame(area_density_t)
    area_density_t_df_data <- setDT(area_density_t_df, keep.rownames = TRUE)[]
    colnames(area_density_t_df_data)[1] <- "id"

    final_table <- merge(x=area_density_t_df_data, y=cordinates, by= "id", all=TRUE)
    colnames(final_table)[2] <- "density"

    final_table
  })

    ############## Map ####################
  mp <- reactive({
    final_table <- mp_table()
    top3 <- head(arrange(final_table, desc(final_table$density)) , n = 3)
    last3 <- tail(arrange(final_table, desc(final_table$density)) , n = 3)
    # Generating Map
    mapWorld <- borders("world", colour="gray50", fill="gray50") # create a layer of borders
    mp <- ggplot() +   mapWorld
    mp <- mp+ geom_point(aes(x=final_table$longitude, y=final_table$latitude) ,color="blue", size=3)+
      coord_cartesian(xlim = ranges$x, ylim = ranges$y, expand = FALSE) 
    mp <- mp+ geom_point(aes(x=top3$longitude, y=top3$latitude) ,color="red", size=3)
    mp <- mp+ geom_point(aes(x=last3$longitude, y=last3$latitude) ,color="green", size=3)
    mp

  })  
  # Printing Map to screen
  output$plot <- renderPlot({
    mp()
  })

  ################# Interactive Plot (Map) modifications #################

  # Plot Click
  output$info <- renderText({
    paste("\nLongitude=", input$plot_click$x, 
          "\nLatitude=", input$plot_click$y
    )
  })

  # Zoom
  # When a double-click happens, check if there's a brush on the plot.
  # If so, zoom to the brush bounds; if not, reset the zoom.
  observeEvent(input$plot_dblclick, {
    brush <- input$plot_brush
    if (!is.null(brush)) {
      ranges$x <- c(brush$xmin, brush$xmax)
      ranges$y <- c(brush$ymin, brush$ymax)

    } else {
      ranges$x <- NULL
      ranges$y <- NULL
    }
  })

  ############ Printing the table to screen on table tab #########
  output$table = renderDataTable({
   mp_table()
  })

} # Server fucntion ends here

shinyApp(ui,server)

Hope this helps!!

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your reply. I'll surely look over what you said. And I tried this. Still doesn't give the desired output. Basically, I want to zoom over the map with double -click after brush. Can you please help me with that. I saw an example on shiny site itself. But couldn't able to accommodate that properly to my code.
I removed a browser() call that I used for debugging maybe that was the problem. For me the zoom works!
Hey! that helped. Thanks :) Though I didn't understand the use of eventReactive. Anyway, I need to read about different types of observers and reactives thoroughly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.