【发布时间】:2018-05-22 22:08:16
【问题描述】:
我有一个交互式显示的闪亮文本,它从情节图中捕获点击事件。如果没有点击,则会显示默认文本,一旦点击某个点,就会显示其对应的值。
但是,我也有一个单选按钮来选择图中所描绘的内容。问题是,当我更改选定的单选按钮时,交互式显示的文本不再正确,因为情节发生了变化并且没有被捕获,如下面的简化示例所示。 因此,每当我在单选按钮中选择不同的选项时,我都希望重置 event_data(并因此显示默认文本)。
我知道有一些方法可以创建一个单独的“重置”按钮(例如,使用 shinyjs 包,请参阅 here),但我想知道是否有可能以某种方式将此重置功能与单选按钮耦合。
library(ggplot2)
library(shiny)
library(shinydashboard)
library(plotly)
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(),
dashboardBody(
fluidRow(
box(plotlyOutput("first"),
radioButtons("radbut", "Choose:", c("Sepal" = "sepal","Petal" =
"petal"))
),
box(textOutput("second"))
)
)
)
server <- function(input, output, session) {
output$first <- renderPlotly({
if (input$radbut == "sepal") {
gp <- ggplot(data = iris, aes(x = Sepal.Width, y = Sepal.Length)) +
geom_point()
} else {
gp <- ggplot(data = iris, aes(x = Petal.Width, y = Petal.Length)) +
geom_point()
}
ggplotly(gp, source = "select")
})
output$second <- renderText({
clicked <- event_data("plotly_click", source = "select")
if (is.null(clicked)) {
text = "Select a value"
} else {
text = paste("You clicked:", input$radbut,
clicked[[3]],",", clicked[[4]], sep = " ")
}
text
})
}
shinyApp(ui, server)
【问题讨论】: