在绘制数据被更改时,在R中绘制更新而不重新创建小部件 [英] Update plotly in R without recreating widget when plotted data is altered

查看:78
本文介绍了在绘制数据被更改时,在R中绘制更新而不重新创建小部件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当一个情节对象由R在闪亮中创建(或仅在R中)时,窗口小部件将完全重新创建。对于小数据集,这不是问题,但我正在处理包含数千个散点的图,这使得在我的shinyapp中重建一个图需要10-20秒。

Everytime a plotly object is created by R in shiny, (or just in R), the widget is recreated completely. For small data sets this is not a problem, but i'm working with plots that contain thousands of scatter points, making it take 10-20 seconds to recreate a plot in my shinyapp.

我正在寻找一种通过javascript解决方案更新数据的方法,该解决方案不会触发要重建的小部件,而只是替换它的数据。

I'm looking for a way to update the data through a javascript solution that doesn't trigger the widget to be rebuild, but simply replaces it's data.

这是一个虚拟应用程序,有2个小数据集,应用程序可以在这两个数据集之间切换。
在虚拟应用程序中,它通过重新创建小部件来实现。由于数据点有限,这里相当快,但不适合海量数据集。

Here is a dummy app with 2 small data sets between which the app can switch. In the dummy app it does it by recreating the widget. Quite fast here due to the limited datapoints, but not ideal for massive data sets.

如果有人知道如何实现这一目标,那将是我的应用程序的重大改进。

If anyone knows how to accomplish this, it would be a major improvement for my app.

要澄清:
这样的答案如下:在这里输入链接描述对我来说不起作用。关键是,在我的应用程序数据在构建绘图之后多次更改,因此我无法预加载数据框列表。

TO CLARIFY: An answer like this one here: enter link description here won't do the trick for me. The point is, in my app data is changed many times AFTER the plot has been build so I can't pre-load a list of data frames.

我觉得解决方案必须是一个javascript解决方案,可以抓取数据来覆盖当前绘制的数据,但不知道如何或是否可以这样做。

I have the feeling the solution would have to be a javascript solution that can grab the data to overwrite the currently plotted data, but not sure how or whether this can be done.

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

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

  plotlyOutput("Plot1")
)


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

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

  output$Plot1 <-  renderPlotly({plot_ly(data = dataSource(), x = dataSource()[,1], 
                                         y =dataSource()[,2], mode = 'markers', type = 'scatter')})
}

shinyApp(ui, server)


推荐答案

看看这些可能对您的案例有用的资源:

Take a look at these resources that might be useful to your case:


  1. Plotly R book by Carson Sievert 第3,4和4章6

  2. Plotly代理功能说明

  1. Plotly R book by Carson Sievert chapters 3, 4 & 6
  2. Plotly proxy function explanation

这是让你入门的代码。你需要做一些工作来调整轴标签,但这不应该那么困难。

This is the code to get you started. You have a bit of work to adjust the axis labels but this should not be that difficult.

希望这会有所帮助!

代码:

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

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

            plotlyOutput("Plot1")
    )


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

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

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

            observeEvent(input$dataset, {
                    f <- list(
                            family = "Courier New, monospace",
                            size = 18,
                            color = "#7f7f7f"
                    )
                    x <- list(
                            title = "x Axis",
                            titlefont = f, 
                            range = c(0, 1000)
                    )
                    y <- list(
                            title = "y Axis",
                            titlefont = f,
                            range = c(0, 100)
                    )
                    plotlyProxy("Plot1", session) %>%
                            plotlyProxyInvoke("addTraces", list(x = dataSource()[,1], 
                                                                y = dataSource()[,2],
                                                                type = 'scatter',
                                                                mode = 'markers')) %>% 
                            plotlyProxyInvoke("deleteTraces", list(as.integer(0))) %>% 
                            plotlyProxyInvoke("relayout", list(xaxis = x, yaxis = y))
            })



    }

    shinyApp(ui, server)

这篇关于在绘制数据被更改时,在R中绘制更新而不重新创建小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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