1

Trying to create a shiny app that has a dropdown menu allowing you to select plots that are subelements of a list that is saved to the global environment.

The plots are located in the second subelement of each element in a list

e.g. list = ([[dataframe1, plot1], [dataframe2, plot2], etc])

The app I am trying to create is given by:

choices = paste0("list[[1]][[", 1:2, "]]")

ui <- shinyUI(fluidPage(selectInput("selectPlot", 
                                    "Choose desired country", 
                                     choices),
                        plotlyOutput("plot")))

server <- shinyServer(function(input,output){      
   output$plot <- renderPlotly({
     return(get(input$selectPlot))
   })
 })

shinyApp(ui,server)

However, no plots are being shown and I am receiving the following error: **Warning: Error in get: object 'anom[[1]][[1]]' not found*

If I save the plots individually to the environment then this approach works. However, I am trying to access the plots via this list that is already present!

Adding reproducible example:

ds1 <- data.frame(sample(1:10, 10), sample(11:20, 10))
ds2 <- data.frame(sample(1:10, 10), sample(11:20, 10))

p1 = plot_ly(x = ~ds1[[1]], y = ~ds1[[2]]) %>% add_markers() 
p2 = plot_ly(x = ~ds2[[1]], y = ~ds2[[2]]) %>% add_markers() 

l = list(list(ds1, p1), list(ds2, p2))

#want to access p1 and p2 from within the list to create drop down menu graph
choices.p = paste0("l[[1]][[", 1:2, "]]")

ui <- shinyUI(fluidPage(selectInput("selectPlot", 
                               "Choose desired country", 
                               choices.p), 
                               plotlyOutput("plot")))


server <- shinyServer(function(input,output){      
output$plot <- renderPlotly({
  return(get(input$selectPlot)) 
 })
})

shinyApp(ui,server)
MLavoie
  • 9,671
  • 41
  • 36
  • 56
Mike Tauber
  • 137
  • 1
  • 12
  • plotly! Here is a reproducible example - (https://stackoverflow.com/questions/45255613/generating-dropdown-menu-for-plotly-charts) - instead of saving the plots to the global enviroment - is there a way to access them from within a list that is already saved to the enviroment? – Mike Tauber Feb 15 '18 at 12:00
  • @MikeTauber Even in your case if you've got it saved in a list, isn't it? Without saving to global variable means, you just want it to be plotting only when required not saved, is that right? – amrrs Feb 15 '18 at 12:06
  • @MikeTauber Please check the answer and feedback – amrrs Feb 15 '18 at 13:59
  • @amrrs thanks for your comments. Much appreciated. I added a reproducible example - does this help explain the problem? – Mike Tauber Feb 15 '18 at 14:28

2 Answers2

1

Based on your MWE, this code doesn't save the plots anywhere but uses observeEvent to plot based on selection.

library(shiny)
library(plotly)



ui <-shinyUI(fluidPage(selectInput("selectPlot", "Choose desired plot", choices=paste0("p", 1:2)), plotlyOutput("plot")))

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

  observeEvent(input$selectPlot,{
    if(input$selectPlot %in% 'p1') output$plot <- renderPlotly(plot_ly(mtcars, x=~cyl, y=~gear))
    else output$plot <- renderPlotly(plot_ly(mtcars, x=~hp, y=~am))

  })


})

shinyApp(ui,server)
amrrs
  • 6,215
  • 2
  • 18
  • 27
  • Thanks for your response @amrrs, much appreciated. Unfortunately still no luck. If I am using your example above (this example https://stackoverflow.com/questions/45255613/generating-dropdown-menu-for-plotly-charts) - suppose that p1 and p2 are contained with a list - e.g. list = ((dataset1, p1), (dataset2, p2)). How would you access these subelements from the workspace so they show up in a drop down menu ? More specifically how do you 1) reference them within the UI 'choice' argument and 2) access them in the Shiny server using input$selectPlot ? Let me know if any of this is still unclear ! – Mike Tauber Feb 15 '18 at 14:04
0

Would something like this work for you:

p1 <- plot_ly(mtcars, x=~cyl, y=~gear)
p2 <- plot_ly(iris, x=~Sepal.Width, y=~Petal.Length, color = "red")

l = list(mtcars = list(mtcars, p1), iris = list(iris, p2))
choice_data <- names(l)

ui <- shinyUI(fluidPage(selectInput("selectPlot", 
                                    "Choose desired country", 
                                    choices = choice_data), textOutput("selected_var"), uiOutput("test")))


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

  output$test <- renderUI({
    l[[input$selectPlot]][2]

  })

})

shinyApp(ui,server)
MLavoie
  • 9,671
  • 41
  • 36
  • 56