简体   繁体   English

闪亮的反应性-动态更改绘图数据行

[英]Shiny reactivity -change plot data row dynamically

I know renderPlot produces plot that can be shown on Shiny plotOutput function. 我知道renderPlot产生可以在Shiny plotOutput函数上显示的图。 I also know autoinvalidate() helps to calculate data reactively. 我也知道autoinvalidate()有助于以反应方式计算数据。

I am displaying a radar chart (in fact can be any chart) using the below codes: 我正在使用以下代码显示雷达图(实际上可以是任何图表):

output$plot2 <- renderPlot({
      autoInvalidate()
        p2<<-ggradar(mtcars_radar[i,])
    })

What I dont know is how to change the value of i from 1 to 300 during every event of autoinvalidate(). 我不知道如何在每次autoinvalidate()事件期间将i的值从1更改为300。 Or is there anyway I can change the row of data in plot so that the plot is dynamically animating every sec with a new row of data. 还是无论如何,我都可以更改绘图中的数据行,以便绘图以每秒新的数据行动态地进行动画处理。 Can anyone help me plz? 有人可以帮我吗?

The full code is here: 完整的代码在这里:

library(shiny)
library(ggplot2)
 mtcars %>%
 rownames_to_column( var = "group" ) %>%
 mutate_at(vars(-group),funs(rescale)) %>%
 tail(4) %>% select(1:10) -> mtcars_radar

ui <- fluidPage(
  sidebarPanel(

    actionButton("button", "Go!")
  ),
  # Show the plot
  mainPanel(
    plotOutput("plot2")
  )
)

server <- function(input, output) {
  library(ggplot2)
  library(ggradar)
  suppressPackageStartupMessages(library(dplyr))
  library(scales)

  autoInvalidate <- reactiveTimer(2000)
  plot2 <- NULL


  output$plot2 <- renderPlot({
    ggradar(mtcars_radar[1,])
  })
  observeEvent(input$button,{

     output$plot2 <- renderPlot({
      autoInvalidate()
        p2<<-ggradar(mtcars_radar[i,])
        p2
    })
  })

}

# Run the application 
shinyApp(ui = ui, server = server)

Any help please? 有什么帮助吗?

This is where you need a reactive value that stores the row index and changes every second. 在这里,您需要一个反应性值来存储行索引并每秒更改一次。 I do not have the library ggradar , so I will just print out the current row index value instead. 我没有库ggradar ,所以我只会打印出当前行索引值。 I also used invalidateLater instead of reactiveTimer as suggested by Shiny documentation . 我也用invalidateLater代替reactiveTimer所建议闪亮的文档

library(shiny)

ui <- fluidPage(
  verbatimTextOutput("debug")
)

server <- function(input, output) {

  row_idx_max <- 15
  row_idx <- reactiveVal(0)

  observe({
    isolate(row_idx(row_idx() + 1))
    cur_row_idx <- isolate(row_idx())
    if (cur_row_idx < row_idx_max) {
      invalidateLater(1000)
    }
  })

  output$debug <- renderPrint({
    row_idx()
  })
}

shinyApp(ui, server)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM