【发布时间】:2020-08-15 23:10:16
【问题描述】:
我正在编写一个闪亮的应用程序,其中包含一个绘制的旭日形图。
提供适当格式的数据框后,我必须单击旭日形图以“向下钻取”。
是否可以模仿这个鼠标“点击”事件来控制来自用户输入的“向下钻取”,例如 selectInput() ?
如何链接 selectInput() 以便它也可以控制闪亮的旭日形?也许某种类型的观察事件?感谢您的帮助。
这是一个代表:
library(shiny)
library(plotly)
library(DT)
d <- data.frame(
ids = c(
"North America", "Europe", "Australia", "North America - Football", "Soccer",
"North America - Rugby", "Europe - Football", "Rugby",
"Europe - American Football","Australia - Football", "Association",
"Australian Rules", "Autstralia - American Football", "Australia - Rugby",
"Rugby League", "Rugby Union"
),
labels = c(
"North<br>America", "Europe", "Australia", "Football", "Soccer", "Rugby",
"Football", "Rugby", "American<br>Football", "Football", "Association",
"Australian<br>Rules", "American<br>Football", "Rugby", "Rugby<br>League",
"Rugby<br>Union"
),
parents = c(
"", "", "", "North America", "North America", "North America", "Europe",
"Europe", "Europe","Australia", "Australia - Football", "Australia - Football",
"Australia - Football", "Australia - Football", "Australia - Rugby",
"Australia - Rugby"
),
stringsAsFactors = FALSE
)
ui <- fluidPage(
mainPanel(
# would like to be able to override or mimic mouse click even with this user input
selectInput(
"make_selection", label = h5("Make selection:"),
choices = c("all" = " ", setNames(nm = d$ids)),
selectize = TRUE,
selected = "all"
),
plotlyOutput("p"),
textOutput("mytext")
)
)
server <- function(input, output, session) {
output$p <- renderPlotly({
plot_ly(d, ids = ~ids, labels = ~labels, parents = ~parents, customdata = ~ids,
level = input$make_selection, type = 'sunburst',
source = "mysource")
})
hoverClick <- reactive({
currentEventData <- unlist(event_data(event = "plotly_click", source = "mysource", priority = "event"))
})
output$mytext <- renderText({
hoverClick()
})
observe({
x <- input$make_selection
# Can use character(0) to remove all choices
if (is.null(hoverClick())){
x <- "all"
} else {
x <- as.character(hoverClick()[3])
}
updateSelectInput(session, "make_selection",
selected = x
# can I add something here so that it just updates the selector without actually
# triggering a selector event? (Otherwise both plotly and the selector are trying to
# choose the level and it is very jerky)
)
})
}
shinyApp(ui = ui, server = server)
【问题讨论】:
-
添加了对 selectInput() 的更新
标签: javascript r shiny plotly htmlwidgets