【问题标题】:Shiny app that updates point shape on plot based on user input selection闪亮的应用程序,根据用户输入选择更新绘图上的点形状
【发布时间】:2021-08-01 05:30:43
【问题描述】:

我正在向闪亮的应用程序添加绘图点击功能。作为应用程序的一部分,用户可以在其中选择一个选项。

我希望每次用户更改输入时,图上点的形状都会发生变化。但只有新点发生了变化。已经在图上的点应该保持它们的形状。

下面是我的例子。我尝试在每次点击后重置输入,但没有成功,实际上我不需要重置,只需绘制图形上的形状即可响应输入。

library(shiny)
library(tidyverse)
library(shinyWidgets)



#UI
ui <- basicPage(
  
  tags$style(type="text/css", ".shiny-output-error { visibility: hidden; }"),
  
  uiOutput("input_event"),
  
  plotOutput("plot", click = "plot_click", width = "350px", height="700px"))
  
  )

#server
server <- function(input, output, session) {
  
  #get click inputs
  val <- reactiveValues(clickx = NULL, clicky = NULL)
  
  #bind clicks
  observe({
    
    input$plot_click
    
    isolate({
      val$clickx = c(val$clickx, input$plot_click$x)
      val$clicky = c(val$clicky, input$plot_click$y)     
    })
  }) 
  
  
  #create event inputs
  output$input_event <- renderUI({
  
    radioGroupButtons(
    inputId = "event",
    label = "Select Event", 
    choices = c("Event1", "Event2", "Event3"),
    selected = "Shot Made",
    individual = TRUE,
    checkIcon = list(yes = tags$i(class = "fa fa-circle", style = "color: steelblue"),
                      no = tags$i(class = "fa fa-circle-o", style = "color: steelblue"))
    )
    
  })
  
  
  observeEvent(input$plot_click, {
    
    shinyjs::reset("event")
    
  })
  
  
  #interactive plot
  output$plot <- renderPlot({
    
    shape <- if (input$event == "Event1") {19} else {18}
    
    # Set up a plot area with no plot
    plot(c(-25, 25), c(-50, 50), type = "n", axes = F , ylab = "", xlab = "")
    points(val$clickx, val$clicky, pch = shape)
    
    
  })
  
  
}

shinyApp(ui, server)

【问题讨论】:

    标签: r plot shiny shinyapps shiny-reactivity


    【解决方案1】:

    您可以将形状与单击x和y一起保存到reactiveValue中:

      val <- reactiveValues(clickx = NULL, clicky = NULL, shape = NULL)
    

    然后在每次点击时插入它。

      observeEvent(input$plot_click, {
          req(input$event)
          val$clickx = c(val$clickx, input$plot_click$x)
          val$clicky = c(val$clicky, input$plot_click$y)
          val$shape = c(val$shape, if (input$event == "Event1") 19 else 18)
      }) 
    

    我使用了observeEvent而不是observe和isolate,并且需要选择一个事件。

    最后,在情节中,使用保存的形状:

        points(val$clickx, val$clicky, pch = val$shape)
    

    【讨论】:

      猜你喜欢
      • 2018-04-30
      • 2020-10-06
      • 2019-02-08
      • 2017-06-07
      • 2018-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-20
      相关资源
      最近更新 更多