gpt4 book ai didi

shiny - 在 Shiny 上动态添加 plotly 痕迹

转载 作者:行者123 更新时间:2023-12-01 11:29:36 28 4
gpt4 key购买 nike

我正在构建一个应用程序,允许用户使用 selectInput 在绘图图上动态添加和删除跟踪。

我试图从 plotly 包中使用 plotlyProxy () 和 plotlyProxyInvoke () ,但无济于事。

以下是我的基本代码:

  library(shiny)
library(shinydashboard)
library(plotly)


ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(
sidebarMenu(
menuItem("Search", tabName = "Tabs", icon = icon("object-ungroup"))

)
),
dashboardBody(
tabItem(tabName = "Tabs",
fluidRow(
column(width=3,
box(
title="SELECT ",
solidHeader=TRUE,
collapsible=TRUE,
width=NULL,
selectInput(
inputId="Player",
selected = NULL, multiple = TRUE,
label=" Choose Player",
choices=c("Messi", "Suarez", "Ronaldo" )),
selectInput(
inputId="Delete",
selected = NULL, multiple = TRUE,
label=" Choose Player",
choices=c("Messi", "Suarez", "Ronaldo" )),
submitButton("Select")
)
),

column( width=9,
tabBox(
width="100%",
tabPanel("tab1",
plotlyOutput("Plot1")
)))))))

server <- function(input, output, session) {
output$Plot1 <- renderPlotly({

goals <- data.frame(Name = c("Messi", "Suarez", "Ronaldo", "Messi", "Suarez", "Ronaldo", "Messi", "Suarez", "Ronaldo" ),
Number= c(47, 35, 40, 49, 32, 31, 51, 49, 44 ),
Year = c("2018","2018","2018", "2017", "2017", "2017", "2016","2016","2016")
)

plot_ly(goals, x = ~Year, y = ~Number, type = 'scatter', mode = 'lines', color = ~input$Player )%>% layout(showlegend = TRUE)%>%
layout(title = 'Number of goals')
})

# plotly.addTraces
observeEvent(input$Player, {
plotlyProxy("Plot1", session) %>%
plotlyProxyInvoke("addTraces", list(x = ~Year,
y = ~Number,
type = 'scatter',
mode = 'lines'))
})

# plotly.deleteTraces
observeEvent(input$Delete, {
plotlyProxy("Plot1", session) %>%
plotlyProxyInvoke("deleteTraces")
})
}
shinyApp(ui, server)

有没有办法动态使用 plotlyProxyInvoke() 来添加和删除跟踪,而不必使用 addTrace() 对跟踪进行硬编码?

最佳答案

这是避免 plotlyProxy() 的解决方案在将 data.frame 传递给 plot_ly 之前过滤它:

library(shiny)
library(shinydashboard)
library(plotly)


ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(
sidebarMenu(
menuItem("Search", tabName = "Tabs", icon = icon("object-ungroup"))

)
),
dashboardBody(
tabItem(tabName = "Tabs",
fluidRow(
column(width=3,
box(
title="SELECT ",
solidHeader=TRUE,
collapsible=TRUE,
width=NULL,
selectizeInput(
inputId="Player",
selected = NULL, multiple = TRUE,
label=" Choose Player",
choices=c("Messi", "Suarez", "Ronaldo" ), options = list('plugins' = list('remove_button')))
)
),

column( width=9,
tabBox(
width="100%",
tabPanel("tab1",
plotlyOutput("Plot1")
)))))))

server <- function(input, output, session) {
output$Plot1 <- renderPlotly({

goals <- data.frame(Name = c("Messi", "Suarez", "Ronaldo", "Messi", "Suarez", "Ronaldo", "Messi", "Suarez", "Ronaldo" ),
Number= c(47, 35, 40, 49, 32, 31, 51, 49, 44 ),
Year = c("2018","2018","2018", "2017", "2017", "2017", "2016","2016","2016")
)

filteredGoals <- reactive({
goals[goals$Name %in% input$Player, ]
})

plot_ly(filteredGoals(), x = ~Year, y = ~Number, type = 'scatter', mode = 'lines', color = ~Name)%>% layout(showlegend = TRUE) %>%
layout(title = 'Number of goals')
})
}
shinyApp(ui, server)

关于shiny - 在 Shiny 上动态添加 plotly 痕迹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53822696/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com