首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当数据更改时,在shiny中以动画方式绘制绘图

当数据更改时,在shiny中以动画方式绘制绘图
EN

Stack Overflow用户
提问于 2019-06-17 09:47:00
回答 1查看 908关注 0票数 1

当数据发生变化时,我想要一个R-shiny-plotly图来平滑过渡。我不想要R-plotly提供的操作按钮方法。因此,我正在尝试通过plotlyProxyInvoke("animate", ...)按照链接的示例获取动画

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

我的应用程序运行了,但是绘图没有动画效果。我漏掉了什么。

代码语言:javascript
复制
library(shiny)
library(plotly)
library(dplyr)

ui <- fluidPage(
    actionButton("go", "Click to recalc"),
    plotlyOutput("plot")
)

gendata <- function(){
    ndata <- 10
    tibble(text=LETTERS[1:ndata], f=1, x=runif(ndata)) %>% mutate(r = rank(x))
}

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

    origdata <- gendata()
    if (FALSE){ # for offline testing
        print(head(origdata))
        my <- list(olddata = origdata, newdata = origdata)
    }

    my <- reactiveValues(
        olddata = origdata,
        newdata = origdata
        )

    output$plot <- renderPlotly({
        cat("renderPlotly\n")
        plot_ly() %>%
            add_trace(x=origdata$x, y=origdata$r, frame=origdata$f, width=0.8, type="bar", orientation="h", name="Rank") %>%
            add_trace(x=origdata$x + 0.02, y=origdata$r, frame=origdata$f, text=origdata$text, type="scatter", mode="text", showlegend=FALSE)
    })

    observeEvent(input$go, {
        req(my$newdata)
        cat("observeEvent input$go\n")
        my$olddata <- my$newdata # save old data
        my$newdata <- gendata() %>% # generate new data
            mutate(f=my$olddata$f+1)
        print(head(my$newdata))
        # https://plot.ly/javascript/plotlyjs-function-reference/#plotlyanimate
        plotlyProxy("plot", session=session, deferUntilFlush=FALSE) %>%
            plotlyProxyInvoke("animate",
                              # frameOrGroupNameOrFrameList
                              list(
                                list(
                                    traces = list(as.integer(0)),
                                    data = list(
                                        x = my$newdata$x,
                                        y = my$newdata$r,
                                        frame = my$newdata$f
                                    )
                                ),
                                list(
                                    traces = list(as.integer(1)),
                                    data = list(
                                        x = my$newdata$x + 0.02,
                                        y = my$newdata$r,
                                        text = my$newdata$text,
                                        frame = my$newdata$f
                                    )
                                )
                              ),
                              # animationAttributes
                              list(
                                transition = list(duration = 500, easing="linear"),
                                frame = list(duration = 500)
                              )
                              )# plotlyProxyInvoke
    })

}

shinyApp(ui, server)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-06-17 10:11:12

原来我只需要修改列表结构。另外,条形图目前还不能在情节中动画,所以我不得不用粗线条来伪装它。

代码语言:javascript
复制
library(shiny)
library(plotly)
library(dplyr)

ui <- fluidPage(
    actionButton("go", "Click to recalc"),
    plotlyOutput("plot")
)

gendata <- function(){
    ndata <- 10
    d <- tibble(text=LETTERS[1:ndata], f=1, x=runif(ndata)) %>% mutate(r = rank(x))
    rbind(mutate(d, x=-1), d, mutate(d, x=-1)) %>%
        arrange(text)
}

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

    origdata <- gendata()
    if (FALSE){ # for offline testing
        print(head(origdata))
        my <- list(olddata = origdata, newdata = origdata)
    }

    my <- reactiveValues(
        olddata = origdata,
        newdata = origdata
        )

    output$plot <- renderPlotly({
        cat("renderPlotly\n")
        plot_ly() %>%
            add_trace(x=origdata$x, y=origdata$r, frame=origdata$f, line=list(width=20, simplify=FALSE), type="scatter", opacity=0.5, mode="lines", name="Rank") %>%
            add_trace(x=origdata$x + 0.02, y=origdata$r, frame=origdata$f, text=origdata$text, type="scatter", mode="text", showlegend=FALSE) %>%
            layout(xaxis=list(range=list(0,1.1))) %>%
            animation_opts(frame=500, transition=500, redraw=FALSE)
    })

    observeEvent(input$go, {
        req(my$newdata)
        cat("observeEvent input$go\n")
        my$olddata <- my$newdata # save old data
        my$newdata <- gendata() %>% # generate new data
            mutate(f=my$olddata$f+1)
        print(head(my$newdata))
        # https://plot.ly/javascript/plotlyjs-function-reference/#plotlyanimate
        plotlyProxy("plot", session=session, deferUntilFlush=FALSE) %>%
            plotlyProxyInvoke("animate",
                              # frameOrGroupNameOrFrameList
                              list(
                                data = list(list(
                                                x = my$newdata$x,
                                                y = my$newdata$r,
                                                frame = my$newdata$f
                                            ),
                                            list(
                                                x = my$newdata$x + 0.02,
                                                y = my$newdata$r,
                                                text = my$newdata$text,
                                                frame = my$newdata$f
                                         )),
                                traces = list(as.integer(0), as.integer(1)),
                                layout = list()
                              ),
                              # animationAttributes
                              list()
                              )# plotlyProxyInvoke
    })

}

shinyApp(ui, server)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56623868

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档