Сброс осей при объединении сюжетных анимаций в блестящие с помощью plotlyProxyInvoke - PullRequest
1 голос
/ 10 июля 2019

Я пытаюсь создать анимированный сюжет, в котором вводятся новые трассы, анимируются трассы и затем масштабируются оси.У меня проблемы с тем, чтобы заставить эти вещи работать вместе.Представить ниже.Это работает за исключением случаев, когда у меня есть и Animate Traces, и Rescale Axis, тогда масштабирование оси сбрасывается на каждой итерации.

Использование прокси-интерфейса в Plotly / Shiny для динамического изменения данных

https://community.plot.ly/t/how-to-efficiently-restyle-update-modify-plot-containing-frames/5553

https://plot.ly/javascript/plotlyjs-function-reference/

https://plot.ly/javascript/animations/

Довольно сложно следовать документации Plotly.Я не мог получить addFrames, relayout, рестайлинг, реакцию или обновление, чтобы работать на меня.Мне больше всего повезло с анимацией.Я был бы очень признателен за любую помощь, я боролся с этим уже две недели.

# plotly_add_anim13.R
library(shiny)
library(plotly)
library(dplyr)
library(purrr)

ui <- fluidPage(
    checkboxInput("add", "Add Trace", TRUE),
    checkboxInput("animate", "Animate Traces", FALSE),
    checkboxInput("rescale", "Rescale Axis", FALSE),
    plotlyOutput("plot")
)

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

    my <- reactiveValues(
        fnumber = NA, # frame number
        frame = NA, # frame data list
        ntraces = NA, # number of traces
        xrange = NA # xaxis range
        )

    speed = 1000 # redraw interval in milliseconds

    output$plot <- renderPlotly({
        isolate({
            cat("renderPlotly\n")
            my$fnumber <- 1
            my$ntraces <- 2
            f <- as.character(my$fnumber)
            x <- runif(2)
            y <- rep(runif(1), 2)
            t <- c("A", "B")
            ids0 <- paste0(my$ntraces-2, letters[1:2])
            ids1 <- paste0(my$ntraces-1, letters[1:2])
            my$xrange <- c(0,1)
            # https://community.plot.ly/t/how-to-efficiently-restyle-update-modify-plot-containing-frames/5553
            my$frame <- list(
                            name = f,
                            data = list(
                                list(x=x, y=y, frame=f, ids=ids0, type="scatter", mode="lines", showlegend=FALSE),
                                list(x=x, y=y, frame=f, ids=ids1, type="scatter", mode="text", text=t, showlegend=FALSE)
                            ),
                            traces = as.list(as.integer(c(my$ntraces-2, my$ntraces-1))),
                            layout = list(xaxis=list(range=my$xrange, zeroline=FALSE),
                                          yaxis=list(range=c(0,1), tickmode="array", tickvals=seq(0,1,0.2), ticktext=seq(0,1,0.2)))
                        )
            p <- plot_ly()
            p <- do.call(add_trace, prepend(my$frame$data[[1]], list(p)))
            p <- do.call(add_trace, prepend(my$frame$data[[2]], list(p)))
            p <- do.call(layout, prepend(my$frame$layout, list(p)))
            p <- animation_opts(p, frame=speed, transition=speed)
            p
        })
    })

    proxy <- plotlyProxy("plot", session=session)

    # https://shiny.rstudio.com/reference/shiny/0.14/reactiveTimer.html
    autoInvalidate <- reactiveTimer(speed*2)

    observeEvent(autoInvalidate(), {
        # req(NULL)
        # https://stackoverflow.com/questions/50620360/using-proxy-interface-in-plotly-shiny-to-dynamically-change-data
        # https://community.plot.ly/t/how-to-efficiently-restyle-update-modify-plot-containing-frames/5553
        # https://plot.ly/javascript/animations/#frame-groups-and-animation-modes
        # https://plot.ly/javascript/animations/
        if (input$add){
            cat("add trace\n")
            my$fnumber <- my$fnumber + 1
            my$ntraces <- my$ntraces + 2
            f <- as.character(my$fnumber)
            x <- runif(2)
            y <- rep(runif(1), 2)
            t <- c("A", "B")
            ids0 <- paste0(my$ntraces-2, letters[1:2])
            ids1 <- paste0(my$ntraces-1, letters[1:2])
            my$frame$name <- f
            my$frame$data[[my$ntraces-1]] <- list(x=x, y=y, frame=f, ids=ids0, type="scatter", mode="lines", showlegend=FALSE)
            my$frame$data[[my$ntraces-0]] <- list(x=x, y=y, frame=f, ids=ids1, type="scatter", mode="text", text=t, showlegend=FALSE)
            my$frame$traces <- as.list(as.integer(1:my$ntraces - 1))
            plotlyProxyInvoke(proxy, "addTraces",
                              list(
                                my$frame$data[[my$ntraces-1]],
                                my$frame$data[[my$ntraces-0]]
                                ))
            plotlyProxyInvoke(proxy, "animate",
                              # frameOrGroupNameOrFrameList
                              list(
                                name = my$frame$name,
                                data = my$frame$data,
                                traces = my$frame$traces
                              ),
                              # animationAttributes
                              list(
                                frame=list(duration=0),
                                transition=list(duration=0)
                              )
            )# animate

        }
        if (input$animate){
            cat("animate traces\n")
            my$fnumber <- my$fnumber + 1
            f <- as.character(my$fnumber)
            traces <- 1:my$ntraces - 1
            for (i in seq(0, my$ntraces-2, 2)){
                x <- runif(2)
                y <- rep(runif(1), 2)
                t <- c("A", "B")
                ids0 <- paste0(i, letters[1:2])
                ids1 <- paste0(i+1, letters[1:2])
                my$frame$data[[i+1]] <- list(x=x, y=y, frame=f, ids=ids0, type="scatter", mode="lines", showlegend=FALSE)
                my$frame$data[[i+2]] <- list(x=x, y=y, frame=f, ids=ids1, type="scatter", mode="text", text=t, showlegend=FALSE)
            }
            my$frame$name <- f
            plotlyProxyInvoke(proxy, "animate",
                              # frameOrGroupNameOrFrameList
                              list(
                                name = my$frame$name,
                                data = my$frame$data,
                                traces = my$frame$traces
                              ),
                              # animationAttributes
                              list(
                                frame=list(duration=speed),
                                transition=list(duration=speed)
                              )
            )# animate
        }
        if (input$rescale){
            cat("animate layout\n")
            my$fnumber <- my$fnumber + 1
            f <- as.character(my$fnumber)
            my$xrange <- runif(2)*0.1+c(-0.1,1)
            my$frame$name <- f
            my$frame$layout <- list(xaxis=list(range=my$xrange))
            plotlyProxyInvoke(proxy, "animate",
                              # frameOrGroupNameOrFrameList
                              list(
                                name = my$frame$name,
                                data = my$frame$data,
                                traces = my$frame$traces,
                                layout = my$frame$layout
                              ),
                              # animationAttributes
                              list(
                                frame=list(duration=speed),
                                transition=list(duration=speed)
                              )
            ) # animate
        }
    }) # observeEvent

}

shinyApp(ui, server)


Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...