Add plotly traces dynamically on shiny

北城以北 提交于 2019-12-04 14:57:47

Here is a solution avoiding plotlyProxy() by filtering your data.frame before passing it to 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)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!