简体   繁体   中英

Shiny hover show ONLY variable value

Here's a reprodcuible example, close to a Shiny App I'm working on:

library(ggplot2)
library(shiny)

ui <- basicPage(
  plotOutput("plot1", hover = "plot_hover"),
  verbatimTextOutput("info")
)

server <- function(input, output) {
  output$plot1 <- renderPlot({
  ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point()
})

output$info <- renderPrint({
nearPoints(mtcars, input$plot_hover, threshold = 10, maxpoints = 1,
           addDist = TRUE)[1]
})
}

shinyApp(ui, server)

Here's the app, with the hover functionality shown, the white cross is the mouse pointer: 在此处输入图片说明

I want to show only mpg value, no rowname. I tried this with nearPoints:

nearPoints(mtcars, input$plot_hover, threshold = 10, maxpoints = 1,addDist = TRUE)[1,1], which resulted in: 

在此处输入图片说明

What I want is just the mpg value, no rowname, no [ 1 ], nothing. How can this be done?

cat() can be used to do this.

the following code, should do the trick for you

  output$info <- renderPrint({
    val <-nearPoints(mtcars, input$plot_hover, threshold = 10, maxpoints = 1,addDist = TRUE)[1,1]

    cat(val)
  })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM