2
votes

I am trying to make an interactive plot in shiny. When I click on the original plot, I want to show info about the point I clicked in a separate fluidRow (another table/plot). I flowed some example online to setup my UI in which the click input variable is called "plot_click". However, I cannot find this input in my server (when I type input$ in server, the input variable list doesn't have plot_click). The code is shown below:

shinyUI(fluidPage(


  # Application title
  titlePanel("Test Project"),

  sidebarLayout(
    sidebarPanel(
      selectInput("variable", "variable:", var_list),
      selectInput("analysis_function", "Function", analy_fun_list)      ),

    mainPanel(
      fluidRow(
        column(width = 4,
               plotOutput("plot", height=350, click = clickOpts(id="plot_click")  )    
               )
      ),
      fluidRow(
        column(width = 6,
               verbatimTextOutput("click_info"))
      )

    )
  )

))

And the server code calling the click input is below:

output$click_info <- renderPrint({
  nearPoints(unlist(graph_react()[4]), input$plot_click, addDist=TRUE)
})

To highlight, the variable "input$plot_click" in the last line cannot be found.

1
Need more code to figure out what you are doing. There is no hint as to how you came up with unlist(graph_react()[4]) for example. - Mike Wise
Thanks for your reply. The first argument of nearPoints is a dataframe. Here, this reactive function suppose to return this kind of dataframe. Because the function return 4 iterms as a list, I used unlist to retrieve the last one. The problem here is "input$" cannot find "plot_click" as input option. - Kenny

1 Answers

1
votes

Well, I made up a small example that is hopefully close to your, that shows you how it works.

  • Created some variables (x,y,z), and some analytic functions (sin,cos,exp), and gerated a grid of data with them
  • Made a plot from them based on the input variables selected in your ui function.
    • Then echoed the click info from that plot

This is the code:

library(shiny)
library(ggplot2)

var_list <- c("x","y","z")
analy_fun <- c("sin","cos","exp")

u <- shinyUI(fluidPage(
  titlePanel("Test ClickInfo"),
  sidebarLayout(
    sidebarPanel(
      selectInput("variable", "variable:", var_list),
      selectInput("analy_fun", "Analytic Function:", analy_fun)),
    mainPanel(
      fluidRow(
        column(width = 4,plotOutput("plot", height=350, click=clickOpts(id="plot_click"))
      ),
      fluidRow(
        column(width = 6, verbatimTextOutput("click_info"))
))))))
s <- function(input, output) {
  gendata <- reactive({
    df <- expand.grid(x=-4:4,y=-4:4,z=-4:4)
    df$sin <- sin(df$x+df$y+df$z)
    df$cos <- cos(df$x+df$y+df$z)
    df$exp <- exp(df$x+df$y+df$z)
    df
  })
  output$plot <- renderPlot({
    ggplot(gendata()) + geom_point(aes_string(x=input$variable,y=input$analy_fun))
  })

  output$click_info <- renderText({
    sprintf(" %s = %.2f \n %s = %.2f ",input$variable,  input$plot_click$x,
                                       input$analy_fun, input$plot_click$y )
  })
}
shinyApp(u, s)

And here is a screen shot of the resulting shiny app with click_info:

enter image description here