闪亮的应用程序,根据用户输入选择更新绘图上的点形状
Shiny app that updates point shape on plot based on user input selection
我正在向闪亮的应用程序添加绘图点击功能。作为应用程序的一部分,有一个输入,用户可以在其中 select 一个选项。
我希望每次用户更改输入时,图上点的形状都会发生变化。但只有新点发生变化。已经在图上的点应保持其形状。
下面是我的例子。我尝试在每次点击后重置输入但它没有用,我实际上不需要重置,只需要绘图上的形状来响应输入。
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)
您可以将形状与单击 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)
我正在向闪亮的应用程序添加绘图点击功能。作为应用程序的一部分,有一个输入,用户可以在其中 select 一个选项。
我希望每次用户更改输入时,图上点的形状都会发生变化。但只有新点发生变化。已经在图上的点应保持其形状。
下面是我的例子。我尝试在每次点击后重置输入但它没有用,我实际上不需要重置,只需要绘图上的形状来响应输入。
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)
您可以将形状与单击 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)