2
votes

I want to draw a Plotly graph in the Shiny App in R. I want the the functionality in such a way that I want to plot a certain number of points (say 20) in a loop.

This is my code for the Server.R :-

xAxis = vector("numeric", as.numeric(input$Generations))
yAxis = vector("numeric", as.numeric(input$Generations))

graphDF = data.frame(cbind(xAxis, yAxis))

for(i in 1 : 5)
{    output$GA = renderPlotly({

  print(graphDF) # Testing 

  graphDF$yAxis[i] = i
  graphDF$xAxis[i] = i

  print(graphDF) # Testing

  # Plotly functionality
  p <- plot_ly(graphDF, x = graphDF$xAxis, y = graphDF$yAxis)

})
}

Any help would be most appreciated.

Kind Regards

1
You can have a look at thisSBista
Dear @SBista, are you sure this is so complex? I mean ... is there a simpler way to do that?Abdul Basit Khan
You can use googlevis package as suggested in this link.SBista
Any feedback for me?Mike Wise
Dear @MikeWise, I feel really bad to reply after this long. I actually got busy in writing the Report for this project since past week. I do understand it is not trivial and am most obliged for such a great help. You have give such an simple solution that even a newbie like me can easily understand that. Once again, thank you very much brother !Abdul Basit Khan

1 Answers

3
votes

This was more complicated than it looked. It looks like you want to iterate and create a series of plotly graphs, changing the data values as you go along.

Because the Generations slider re-initializes the vector to a new length, and each iteration changes the state of the data being plotted, you can't just cascade reactive functions. Storing the state in a reactiveValues is a good way to handle this.

The major changes were as follows:

  • Added a reactiveValues to store xAxis and yAxis
  • Added an observeEvent to reinitialize those values when its value change
  • Added an "Iteration range" slider to drive the iteration (easier than a reactive timer). Note that it has an animate parameter that (probably) creates a reactive timer on its own.
  • Modified the plotly call to make it more conventional and avoid warnings.

The code:

library(shiny)
library(plotly)

u <- fluidPage(
  titlePanel("Iterations of a plotly graph"),
  sidebarLayout(
    sidebarPanel(
      sliderInput("Generations","Number of Generations:",
                  min = 1,  max = 50,  value = 20),
      sliderInput("iter", "Iteration range:", 
                  value = 1,  min = 1,  max = 1000, step = 1,
                  animate=animationOptions(interval=800, loop=T)),
      p("To start click on the blue arrowhead")
    ),
    mainPanel(
      plotlyOutput("GA")
    )
))
s <- shinyServer(function(input,output){

  rv <- reactiveValues(xAxis=NULL,yAxis=NULL)

  observeEvent(input$Generations,{
    rv$xAxis=vector("numeric", as.numeric(input$Generations))
    rv$yAxis=vector("numeric", as.numeric(input$Generations))
  })
  output$GA = renderPlotly({
      rv$yAxis[input$iter] <- input$iter 
      rv$xAxis[input$iter] <- input$iter
      gdf <- data.frame(xAxis=rv$xAxis, yAxis=rv$yAxis)
      plot_ly(gdf, x = ~xAxis, y = ~yAxis, type="scatter",mode="markers")
    })
})
shinyApp(u,s)

Because it is dynamic, you have to run it to see how it really works, but here is a screen shot after several iterations:

enter image description here