数据更改时使用plotlyProxy添加多个跟踪 [英] Use plotlyProxy to add multiple traces when data changes

查看:120
本文介绍了数据更改时使用plotlyProxy添加多个跟踪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

感谢此问题:

Thank to this question: SO-Q I have now understood how to remove traces. In this case, I simply remove 0:2, but I can change that to i.e. array(O:unique(factor(df$group))) to remove however many groups my model had created in a previous run.

但是我无法弄清楚的是如何添加多条迹线,在目标列中为每个因子添加1条,并用THECOLORS

What I haven't been able to figure out however, is how to add multiple traces, 1 for each factor in the target column, and color them by the colors in THECOLORS

library("shiny")
library("plotly")

rock[,2] <- sample(c('A', 'B', 'C'), 48, replace = T)
THECOLORS <- c('#383838', '#5b195b','#1A237E', '#000080', '#224D17', '#cccc00', '#b37400',  '#990000')

ui <- fluidPage(
  selectInput("dataset", "Choose a dataset:", choices = c("mtcars","rock")),

  plotlyOutput("Plot1")
)


server <- function(input, output, session) {


  dataSource <- reactive({switch(input$dataset,"rock" = rock,"mtcars" = mtcars)})

  output$Plot1 <-  renderPlotly({plot_ly(mtcars, x = ~mpg, y = ~hp, type = 'scatter', mode = 'markers', color = as.factor(mtcars$cyl), colors = THECOLORS) })

  observeEvent(input$dataset, {
    f <- list(
      family = "Courier New, monospace",
      size = 18,
      color = "#7f7f7f"
    )
    x <- list(
      title = "x Axis",
      titlefont = f, 
      range = c(0,(max(dataSource()[,1])+ 0.1*max(dataSource()[,1])))
    )
    y <- list(
      title = "y Axis",
      titlefont = f,
      range = c(0,(max(dataSource()[,4])+ 0.1*max(dataSource()[,4])))
    )
    plotlyProxy("Plot1", session) %>%
      plotlyProxyInvoke("deleteTraces",array(0:2)) %>% 
      # lapply(unique(dataSource()[,2], function(x) {  data <- dataSource()[which(dataSource()[,2] == x)],
      #                                   plotlyProxyInvoke("addTraces", 
      #                                     
      #                                     x = data()[,1],
      #                                     y = data()[,4],
      #                                     type = 'scatter',
      #                                     mode = 'markers')}) %>%

      plotlyProxyInvoke("relayout", list(xaxis = x, yaxis = y))
  })
}

shinyApp(ui, server)

推荐答案

基本上,当使用plotlyProxy时,而不是使用带有"addTraces"的plotlyProxyInvoke时,"addTraces"用于添加更多跟踪. 您必须创建一个列表列表,每个内部列表将包含每个跟踪的详细信息. 在您要添加许多跟踪的情况下,purrr软件包中的某些功能可能有助于创建定义跟踪的列表列表.

Basically when using plotlyProxy and than plotlyProxyInvoke with "addTraces", "addTraces" is used to add more traces. You have to create a list of lists and each inner list would contain the details of each trace. In your case with many traces to add maybe some of the functions from the purrr package could help in creating the list of lists defining the traces.

看看下面的代码.这是一个非常简化的示例,仅添加了两条跟踪,但是列表方法列表存在. 关于您对速度的评论,也许只有在需要时才可以加载数据,而在您的应用程序概念允许的情况下可以部分加载数据...

Take a look at the code below. It is a very simplified example, adding only two traces but the lists of list approach is there. Regarding your comment about the speed maybe you could load data only when needed and partially if your app concept allows for that...

代码:

    library("shiny")
    library("plotly")
    library(purrr)

    ui <- fluidPage(
            selectInput("dataset", "Choose a dataset:", choices = c("rock", "mtcars")),

            plotlyOutput("Plot1")
    )


    server <- function(input, output, session) {



            output$Plot1 <-  renderPlotly({plot_ly(data = rock, x = ~area, 
                                                   y =~peri, mode = 'markers', type = 'scatter')})

            observeEvent(input$dataset, {
                    if (input$dataset == "rock") {
                            f <- list(
                                    family = "Courier New, monospace",
                                    size = 18,
                                    color = "#7f7f7f"
                            )
                            x <- list(
                                    title = "Area",
                                    titlefont = f, 
                                    range = c(0, max(rock$area))
                            )
                            y <- list(
                                    title = "Peri/Perm",
                                    titlefont = f,
                                    range = c(0, max(rock$peri))
                            )    
                            plotlyProxyInvoke(plotlyProxy("Plot1", session), "addTraces", list(list( 
                                    x = rock$area,
                                    y = rock$peri,
                                    type = 'scatter',
                                    mode = 'markers',
                                    marker = list(size = 10,
                                                  color = 'rgba(255, 182, 193, .9)',
                                                  line = list(color = 'rgba(0, 255, 0, .3)',
                                                              width = 2))
                            ),
                            list( 
                                    x = rock$area,
                                    y = rock$perm,
                                    type = 'scatter',
                                    mode = 'markers',
                                    marker = list(size = 10,
                                                  color = 'rgba(255, 182, 193, .9)',
                                                  line = list(color = 'rgba(152, 0, 0, .8)',
                                                              width = 2))
                            ))
                            ) 
                            plotlyProxy("Plot1", session) %>%
                                    plotlyProxyInvoke("deleteTraces", list(as.integer(0))) %>%
                                    plotlyProxyInvoke("relayout", list(xaxis = x, yaxis = y))
                    } else {
                            f <- list(
                                    family = "Courier New, monospace",
                                    size = 18,
                                    color = "#7f7f7f"
                            )
                            x <- list(
                                    title = "hp",
                                    titlefont = f, 
                                    range = c(0, max(mtcars$hp))
                            )
                            y <- list(
                                    title = "mpg/cyl",
                                    titlefont = f,
                                    range = c(0, max(mtcars$mpg))
                            ) 
                            plotlyProxyInvoke(plotlyProxy("Plot1", session), "addTraces", list(list( 
                                    x = mtcars$hp,
                                    y = mtcars$mpg,
                                    type = 'scatter',
                                    mode = 'markers',
                                    marker = list(size = 10,
                                                  color = 'rgba(255, 182, 193, .9)',
                                                  line = list(color = 'rgba(0, 255, 0, .3)',
                                                              width = 2))
                            ),
                            list( 
                                    x = mtcars$hp,
                                    y = mtcars$cyl,
                                    type = 'scatter',
                                    mode = 'markers',
                                    marker = list(size = 10,
                                                  color = 'rgba(255, 182, 193, .9)',
                                                  line = list(color = 'rgba(152, 0, 0, .8)',
                                                              width = 2))
                            ))
                            )   
                            plotlyProxy("Plot1", session) %>%
                                    plotlyProxyInvoke("deleteTraces", list(as.integer(0))) %>%
                                    plotlyProxyInvoke("relayout", list(xaxis = x, yaxis = y))
                    }
            })
    }

    shinyApp(ui, server)

这篇关于数据更改时使用plotlyProxy添加多个跟踪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆