Interactive graphing using mouse clicks

I am doing a project in which I use a brilliant server and connect R to mongodb to get results from the database and dynamically display.

However, I am facing the following problem. First I get the results from db and do the plot. After this graph is completed, I want the user to make two clicks on the graph, on the basis of which he should take two values ​​as xlim and build an enlarged version of the previous graph. However, I cannot do this successfully.

Here is the code I wrote.

ui.R

library(shiny)
shinyUI(fluidPage(
    titlePanel("LOAD AND PERFORMANCE DASHBOARD"),

    sidebarLayout(
            sidebarPanel(
                    fluidRow(
                            selectInput("select", label = h3("Select type of testing"), 
                                        choices = list("Performance Testing"=1, "Capacity Testing"=2)),
                            radioButtons("radio", label = h3("Select parameter to plot"),
                                         choices = list("Disk" = 1, "Flit" = 2,"CPU" = 3,"Egress" =4,
                                                        "Memory" = 5))  
                    )),
            mainPanel(
                    plotOutput("plot",clickId="plot_click"),
                    textOutput("text1"),
                    plotOutput("plot2")
                    )
    )
))

server.R

library(shiny)
library(rmongodb)
cursor <- vector()
shinyServer(function(input, output) {

    initialize <- reactive({
            mongo = mongo.create(host = "localhost")
    })

    calculate <- reactive({
            if(input$radio==1)
                    xvalue <- mongo.distinct(mongo,ns = "mydb.vload", "disk")
            else if(input$radio==2)
                    xvalue <- mongo.distinct(mongo,ns = "mydb.vload", "flit")
            else if(input$radio==3)
                    xvalue <- mongo.distinct(mongo,ns = "mydb.vload", "cpu")
            else if(input$radio==4)
                    xvalue <- mongo.distinct(mongo,ns = "mydb.vload", "egress")
            else if(input$radio==5)
                    xvalue <- mongo.distinct(mongo,ns = "mydb.vload", "memory")
    })

    output$plot <- renderPlot({
            initialize()
            value <- calculate()
            plot(value,xlab="Time",ylab="% Consumed")
            lines(value)
            cursor <- value
    })       

    output$text1 <- renderText({ 
            paste("You have selected",input$plot_click$x)
    })

    output$plot2 <- renderPlot({
            plot(cursor[cursor<input$plot_click$x && cursor>first_click ],xlab="Time",ylab="% Consumed")                lines(cursor)
            first_click <- input$plot_click$x
    })           

})

Thank you in advance:)

+4
source share
1 answer

, , , ( ). registerEvent, Shiny 0.11, CRAN .

, click1 range. click1 , ; range x . , .

library(shiny)

ui <- fluidPage(
  h1("Plot click demo"),
  plotOutput("plot", clickId = "plot_click"),
  actionButton("reset", "Reset zoom")
)

server <- function(input, output, session) {
  v <- reactiveValues(
    click1 = NULL,  # Represents the first mouse click, if any
    range = NULL    # After two clicks, this stores the range of x
  )

  # Handle clicks on the plot
  observeEvent(input$plot_click, {
    if (is.null(v$click1)) {
      # We don't have a first click, so this is the first click
      v$click1 <- input$plot_click
    } else {
      # We already had a first click, so this is the second click.
      # Make a range from the previous click and this one.
      v$range <- range(v$click1$x, input$plot_click$x)
      # And clear the first click so the next click starts a new
      # range.
      v$click1 <- NULL
    }
  })

  observeEvent(input$reset, {
    # Reset both the range and the first click, if any.
    v$range <- NULL
    v$click1 <- NULL
  })

  output$plot <- renderPlot({
    plot(cars, xlim = v$range)
    if (!is.null(v$click1$x))
      abline(v = v$click1$x)
  })
}

shinyApp(ui, server)
+11

All Articles