r - R Shiny:重新布局情节注释
问题描述
如果用户单击闪亮应用程序中的按钮,我想要一个情节图来更改注释。我不知道为什么这不起作用:
library(shiny)
library(plotly)
d <- data.frame(x = c(1,2,3), y = c(9,99,999))
ui <- fluidPage(
plotlyOutput("plot"),
actionButton("button", "toggle visibility"))
server <- function(input, output) {
output$plot <- renderPlotly({
plot_ly(d)%>%
add_lines(y=d$y, x= d$x)%>%
layout(annotations = list(x = 2, y= 99 , text = "hi"))})
observeEvent(input$button, {
plotlyProxy("plot", session= shiny::getDefaultReactiveDomain()) %>%
plotlyProxyInvoke("relayout", list(annotations= list(x = 2, y= 99 ,
text = "ho")))})}
shinyApp(ui, server)
解决方案
这不是relayout
在plotly
. 请参阅下面的示例,使用relayout
.
我更喜欢为此目的使用本机闪亮按钮,因为它提供了更大的灵活性。以下是实现高音切换的方法。
shiny
方法
library(shiny)
library(plotly)
d <- data.frame(x = c(1,2,3), y = c(9,99,999))
ui <- fluidPage(
plotlyOutput("plot"),
actionButton("button", "toggle visibility"))
server <- function(input, output) {
output$plot <- renderPlotly({
p <- plot_ly(d)%>%
add_lines(y=d$y, x= d$x)
if (is.null(input$button) | (input$button%%2 == 0)) {
p <- p %>% layout(annotations = list(x = 2, y= 99 , text = "hi"))
} else {
p <- p %>% layout(annotations = list(x = 2, y= 99 , text = "ho"))
}
p
})
}
shinyApp(ui, server)
relayout
在这种情况下,虽然它确实需要一个额外的按钮,但使该功能工作起来很简单。
plotly
relayout
方法
library(shiny)
library(plotly)
d <- data.frame(x = c(1,2,3), y = c(9,99,999))
ui <- fluidPage(
plotlyOutput("plot")
)
server <- function(input, output) {
output$plot <- renderPlotly({
updatemenus <- list(
list(
active = -1,
type = 'buttons',
buttons = list(
list(
label = "hi",
method = "relayout",
args = list(list(annotations = list(list(x = 2, y= 99 , text = "hi"))))),
list(
label = "ho",
method = "relayout",
args = list(list(annotations = list(list(x = 2, y= 99 , text = "ho")))))
)
)
)
p <- plot_ly(d) %>%
add_lines(y=d$y, x= d$x) %>%
layout(updatemenus = updatemenus)
p
})
}
shinyApp(ui, server)
推荐阅读
- mongodb - 当应用程序连接到这些多个副本集时,为什么每个 mongodb 副本集都应该有一个不同的 replSetName 值?
- java - 如何从未来的结果中获取 Callable
- appium - 防止appium桌面客户端强制停止应用
- java - 断言集合中的每个字符串都包含子字符串的最佳方法?
- python-3.x - reshape 的输入是一个具有 788175 个值的张量,但请求的形状有 1050900
- kotlin - 错误:(1, 41) Kotlin:符号在模块“jdk.internal.opt”中声明,它不导出包“jdk.i”
- javascript - 如何使用 IntelliJ 实时模板创建“this.someMethod = this.someMethod.bind(this)”的快捷方式?
- javascript - 如何在 vue 的引导表中添加列事件?
- c# - 在 C# 中的谷歌驱动器中下载时无法更新进度条
- authorization - 计算
一个 或者