简体   繁体   English

shiny 点击 plot 更新输入

[英]shiny click on plot update input

I have this very simple shiny app我有这个非常简单的 shiny 应用程序

When input changes, the graph changes accordingly当输入发生变化时,图形也会相应变化

When a point is selected within the graph the corresponding model is displayed on the right of the input text box当在图表中选择一个点时,相应的 model 将显示在输入文本框的右侧

I would like to see the selection to be displayed inside the text box我想看到要在文本框中显示的选择

Can anyone please point me in the right direction谁能指出我正确的方向

Thanks for any help谢谢你的帮助

  require(ggplot2)
  require(dplyr)
  require(Cairo)   
  require(dplyr)
  
    
  mtcars2 <- datasets::mtcars %>% rownames_to_column(var = 'model') 
  
  
  ui <- fluidPage(
    
      fluidRow(
        column(width = 3,
               selectInput(inputId = '.model', label = 'input model', choices = mtcars2$model,  selected = NULL)),
        br(),br(),
        column(width = 3,
               textOutput('click_1A'), label = 'selected model')
            ),
      fluidRow(
        column(width = 8,
             plotOutput("plot1", height = 350, brush = "plot_brush", click = "plot_click")),
      )
    )
  
  server <- function(input, output) {
    
    
    
    global <- reactiveValues(.model = NULL) 
   
    
    # scatter plot
    output$plot1 <- renderPlot({
      selected_model <- input$.model
      ggplot(mtcars2, aes(x=mpg,y=disp), color = 'red') + 
        geom_point(size = 3, col = 'red') + 
        geom_point(data = filter(mtcars2, model == selected_model), col = 'blue', size = 5) + 
        theme_bw() +
        theme(legend.position = 'none') 
      
      
    })
    
    
    # MODEL name 
     output$click_1A <- renderText({
       
       near_out <- nearPoints(mtcars2, input$plot_click, addDist = TRUE)
       global$.model <- near_out %>% 
         pull(model) 
     })     
         
    }
  shinyApp(ui, server)

Thanks @Ben谢谢@Ben

Here is the clean version of what was trying to achieve:这是试图实现的干净版本:

require(ggplot2)
require(tidyr)
require(tibble)
require(lubridate)
require(Cairo)
require(dplyr)
  
    
mtcars2 <- datasets::mtcars %>% rownames_to_column(var = 'model') 
  
  
  ui <- fluidPage(
    
      fluidRow(
        column(width = 3,
               selectInput(inputId = '.model', label = 'input model', choices = mtcars2$model,  selected = NULL)),
      ),
      fluidRow(
        column(width = 8,
             plotOutput("plot1", height = 350, brush = "plot_brush", click = "plot_click")),
      )
    )
  
  server <- function(input, output, session) {
    
    
    
    global <- reactiveValues(.model = NULL) 
   
    
    # scatter plot
    output$plot1 <- renderPlot({
      selected_model <- input$.model
      ggplot(mtcars2, aes(x=mpg,y=disp, label = model), color = 'red') + 
        geom_point(size = 3, col = 'red') + 
        geom_point(data = filter(mtcars2, model == selected_model), col = 'blue', size = 5) + 
        #geom_text() + 
        theme_bw() +
        theme(legend.position = 'none') 
      
      
    })
    
      observeEvent(
        eventExpr = input$plot_click, 
        handlerExpr = {
          selected_model <- nearPoints(mtcars2, input$plot_click, maxpoints = 1, addDist = F) %>% pull(model)
          updateSelectInput(session, inputId = ".model", choices = mtcars2$model, selected = selected_model)}
        ) 
    }
  shinyApp(ui, server)
  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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