0
votes

I know renderPlot produces plot that can be shown on Shiny plotOutput function. I also know autoinvalidate() helps to calculate data reactively.

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(). 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?

1

1 Answers

1
votes

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. I also used invalidateLater instead of reactiveTimer as suggested by Shiny documentation.

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)