【问题标题】:Prevent plotly selected traces from resetting when changing the variable to be plotted in R Shiny在更改要在 R Shiny 中绘制的变量时,防止绘图选择的迹线重置
【发布时间】:2023-02-16 05:53:48
【问题描述】:

我正在尝试制作一个闪亮的应用程序,它由一个侧边栏面板和一个图组成。在面板中,我有单选按钮来选择应该绘制哪个 ID。我还有多个变量,用户可以使用 plotly legend 关闭和打开这些变量。

我希望应用程序首次打开时该图为空。为此,我在情节中使用visible = "legendonly"。但是,当在侧边栏面板中更改 ID 时,我想保留用户已经激活的痕迹(通过在图例中单击它们);但是,由于每次都会重新生成 plotly,因此它再次使用 visible = "legendonly" 选项,这会导致 plot 重置。

当在侧边栏面板中选择不同的选项时,有没有办法保留痕迹(仅已选择的痕迹)?

请参阅下面的可重现示例;请注意,我让这个例子在本地运行。您需要将数据和包分别加载到您的 R 会话中。数据可以在问题的底部找到。

library(shiny)
library(plotly)
library(lubridate)

### Read mdata into your R session
# UI 

uix <- shinyUI(pageWithSidebar(
  headerPanel("Data"),
  sidebarPanel(
    radioButtons('vars', 'ID', 
                 c("1", "2")),
    helpText('Select an ID.')
  ),
  mainPanel(
    h4("Plot"),
    plotlyOutput("myPlot")
  )
)
)
# SERVER 

serverx <- function(input, output) {
 
  #load("Data/mdata.RData") #comment out this part and load data locally
  
  # a large table, reative to input$show_vars
  output$uteTable = renderDataTable({
    ute[, input$show_vars, drop = FALSE]
  })
  
  output$myPlot = renderPlotly(
    {
      p <- plot_ly() %>% 
        layout(title = "Title", xaxis = list(tickformat = "%b %Y", title = "Date"),
               yaxis = list(title = "Y"))
      
      ## Add the IDs selected in input$vars
      for (item in input$vars) {
        mdata %>% 
          mutate(Date = make_date(Year, Month, 15)) %>% 
          filter(ID == item) -> foo
        
        p <- add_lines(p, data = foo, x = ~Date, y = ~Value, color = ~Variable, visible = "legendonly",
                       evaluate = TRUE)
        
        p <- p %>% layout(showlegend = TRUE,
                          legend = list(orientation = "v",   # show entries horizontally
                                        xanchor = "center",  # use center of legend as anchor
                                        x = 100, y=1))        
      }
      print(p)
    })
}
shinyApp(uix, serverx)

创建于 2020-06-12 reprex package (v0.3.0)

问题:ID == 2 是否可以保留Var1 trace?

主意:我认为如果我可以在应用程序部署后立即将 visible = 'legendonly 更改为 TRUE 是可能的,因此它仅适用于情节的第一个示例。可能,我还需要将evaluate 更改为FALSE

数据:

mdata <- structure(list(Year = c(2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 
2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 
2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 
2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 
2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 
2015L, 2015L, 2015L, 2015L, 2015L, 2015L), Month = c(1L, 1L, 
1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L, 5L, 
5L, 5L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 9L, 9L, 
9L, 9L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 
12L), Variable = c("Var1", "Var1", "Var2", "Var2", "Var1", "Var1", 
"Var2", "Var2", "Var1", "Var1", "Var2", "Var2", "Var1", "Var1", 
"Var2", "Var2", "Var1", "Var1", "Var2", "Var2", "Var1", "Var1", 
"Var2", "Var2", "Var1", "Var1", "Var2", "Var2", "Var1", "Var1", 
"Var2", "Var2", "Var1", "Var1", "Var2", "Var2", "Var1", "Var1", 
"Var2", "Var2", "Var1", "Var1", "Var2", "Var2", "Var1", "Var1", 
"Var2", "Var2"), ID = c(1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 
2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 
1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2), Value = c(187.797761979167, 
6.34656438541666, 202.288468333333, 9.2249309375, 130.620451458333, 
4.61060465625, 169.033213020833, 7.5226940625, 290.015582677083, 
10.8697671666667, 178.527960520833, 7.6340359375, 234.53493728125, 
8.32400878125, 173.827054583333, 7.54521947916667, 164.359205635417, 
5.55496292708333, 151.75458625, 6.361610625, 190.124467760417, 
6.45046077083333, 191.377006770833, 8.04720916666667, 170.714612604167, 
5.98860073958333, 210.827157916667, 9.46311385416667, 145.784868927083, 
5.16647911458333, 159.9545675, 6.7466725, 147.442681895833, 5.43921594791667, 
153.057018958333, 6.39029208333333, 165.6476956875, 5.63139815625, 
197.179256875, 8.73210604166667, 148.1879651875, 5.58784840625, 
176.859451354167, 7.65670020833333, 186.215496677083, 7.12404453125, 
219.104379791667, 9.39468864583333)), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -48L), groups = structure(list(
    Year = 2015L, .rows = list(1:48)), row.names = c(NA, -1L), class = c("tbl_df", 
"tbl", "data.frame"), .drop = TRUE))

【问题讨论】:

    标签: r shiny plotly r-plotly shinyapps


    【解决方案1】:

    以下使用 plotlyProxy 替换现有绘图对象(和轨迹)的数据,因此避免重新渲染绘图。这种方法比重新渲染更快。

    library(shiny)
    library(plotly)
    library(lubridate)
    
    # UI
    uix <- shinyUI(pageWithSidebar(
      headerPanel("Data"),
      sidebarPanel(
        radioButtons('myID', 'ID', 
                     c("1", "2")),
        helpText('Select an ID.')
      ),
      mainPanel(
        h4("Plot"),
        plotlyOutput("myPlot")
      )
    )
    )
    
    # SERVER
    serverx <- function(input, output, session) {
    
      output$myPlot = renderPlotly({
        p <- plot_ly() %>% 
          layout(title = "Title", xaxis = list(tickformat = "%b %Y", title = "Date"),
                 yaxis = list(title = "Y"))
        
        mdata %>% 
          mutate(Date = make_date(Year, Month, 15)) %>% 
          filter(ID == 1) -> IDData
        
        p <- add_lines(p, data = IDData, x = ~Date, y = ~Value, 
                                         color = ~Variable, visible = "legendonly")
        
        p <- p %>% layout(showlegend = TRUE,
                          legend = list(orientation = "v",   # show entries horizontally
                                        xanchor = "center",  # use center of legend as anchor
                                        x = 100, y=1))        
        p
      })
      
      
      myPlotProxy <- plotlyProxy("myPlot", session)
      
      observe({
        mdata %>%
          mutate(Date = make_date(Year, Month, 15)) %>%
          filter(ID == input$myID) -> IDData
        
        req(IDData)
        uniqueVars <- unique(IDData$Variable)
        
        for(i in seq_along(uniqueVars)){
          IDData %>% filter(Variable == uniqueVars[i]) -> VarData
          plotlyProxyInvoke(myPlotProxy, "restyle", list(x = list(VarData$Date), 
                                                         y = list(VarData$Value)), list(i-1))
        }
      })
      
    }
    
    shinyApp(uix, serverx)
    

    有关更多信息,请参阅plotly book、plotly 的function referencethis answer 中的“17.3.1 部分情节更新”一章。

    数据:

    ### Read mdata into your R session
    mdata <- structure(list(Year = c(2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 
    2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 
    2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 
    2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 
    2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 
    2015L, 2015L, 2015L, 2015L, 2015L, 2015L), Month = c(1L, 1L, 
    1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L, 5L, 
    5L, 5L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 9L, 9L, 
    9L, 9L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 
    12L), Variable = c("Var1", "Var1", "Var2", "Var2", "Var1", "Var1", 
    "Var2", "Var2", "Var1", "Var1", "Var2", "Var2", "Var1", "Var1", 
    "Var2", "Var2", "Var1", "Var1", "Var2", "Var2", "Var1", "Var1", 
    "Var2", "Var2", "Var1", "Var1", "Var2", "Var2", "Var1", "Var1", 
    "Var2", "Var2", "Var1", "Var1", "Var2", "Var2", "Var1", "Var1", 
    "Var2", "Var2", "Var1", "Var1", "Var2", "Var2", "Var1", "Var1", 
    "Var2", "Var2"), ID = c(1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 
    2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 
    1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2), Value = c(187.797761979167, 
    6.34656438541666, 202.288468333333, 9.2249309375, 130.620451458333, 
    4.61060465625, 169.033213020833, 7.5226940625, 290.015582677083, 
    10.8697671666667, 178.527960520833, 7.6340359375, 234.53493728125, 
    8.32400878125, 173.827054583333, 7.54521947916667, 164.359205635417, 
    5.55496292708333, 151.75458625, 6.361610625, 190.124467760417, 
    6.45046077083333, 191.377006770833, 8.04720916666667, 170.714612604167, 
    5.98860073958333, 210.827157916667, 9.46311385416667, 145.784868927083, 
    5.16647911458333, 159.9545675, 6.7466725, 147.442681895833, 5.43921594791667, 
    153.057018958333, 6.39029208333333, 165.6476956875, 5.63139815625, 
    197.179256875, 8.73210604166667, 148.1879651875, 5.58784840625, 
    176.859451354167, 7.65670020833333, 186.215496677083, 7.12404453125, 
    219.104379791667, 9.39468864583333)), class = c("grouped_df", 
    "tbl_df", "tbl", "data.frame"), row.names = c(NA, -48L), groups = structure(list(
        Year = 2015L, .rows = list(1:48)), row.names = c(NA, -1L), class = c("tbl_df", 
    "tbl", "data.frame"), .drop = TRUE))
    

    【讨论】:

    • @kraggle 是的,同样可以通过单个plotlyProxyInvoke 调用完成,如图here 所示。
    • @kraggle 如果您有新问题,请将其原样发布 - 不要将其作为答案发布在这里。您可以使用 visible = FALSEvisible="legendonly" 来隐藏跟踪。
    • 迹线按照它们在图中出现的顺序从 0 开始编号。有关更多详细信息:提出一个单独的问题。
    【解决方案2】:

    我能想到的是添加一个复选框来选择要绘制的变量,而不是在图例中关闭和打开它们。使用此方法,而不是使用 visible = legendonly,我保留了未选中默认值的复选框。此外,当用户更改 ID 时,变量保持不变,因此会为下一个 ID 绘制。见下文;

    library(shiny)
    library(plotly)
    library(lubridate)
    
    ### Read mdata into your R session
    
    # UI 
    
    uix <- shinyUI(pageWithSidebar(
      headerPanel("Data"),
      sidebarPanel(
        radioButtons('vars', 'ID', 
                     c("1", "2")),
        checkboxGroupInput('varp', 'Variable',
                           c("Var1", "Var2")),
        helpText('Select an ID and Variables to be plotted.')
      ),
      mainPanel(
        h4("Plot"),
        plotlyOutput("myPlot")
      )
    )
    )
    
    # SERVER 
    
    serverx <- function(input, output) {
    
      #load("Data/mdata.RData") #comment out this part and load data locally
    
      # a large table, reative to input$show_vars
      output$uteTable = renderDataTable({
        ute[, input$show_vars, drop = FALSE]
      })
    
      output$myPlot = renderPlotly(
        {
          p <- plot_ly() %>% 
            layout(title = "Title", xaxis = list(tickformat = "%b %Y", title = "Date"),
                   yaxis = list(title = "Y"))
    
          ## Add the IDs selected in input$vars
          for (item in input$vars) {
            mdata %>% 
              mutate(Date = make_date(Year, Month, 15)) %>% 
              filter(ID == item,
                     Variable %in% input$varp)-> foo
    
            p <- add_lines(p, data = foo, x = ~Date, y = ~Value, color = ~Variable, evaluate = TRUE)
    
            p <- p %>% layout(showlegend = TRUE,
                              legend = list(orientation = "v",   # show entries horizontally
                                            xanchor = "center",  # use center of legend as anchor
                                            x = 100, y=1))        
          }
          print(p)
        })
    }
    
    shinyApp(uix, serverx)
    

    【讨论】:

    • 问题(自我)现在为您解答了吗?
    • @TonioLiebrand 不,正如我在赏金横幅中解释的那样,我认为这是一种解决方法。
    猜你喜欢
    • 2023-04-06
    • 2016-09-28
    • 1970-01-01
    • 1970-01-01
    • 2021-04-29
    • 1970-01-01
    • 1970-01-01
    • 2022-01-12
    • 2016-08-16
    相关资源
    最近更新 更多