【发布时间】: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