Generate interactive plot from a list after selectInput

Hi,

I just start with R markdown, I search a solution to my issue on several topics but I could not find it. I have a dataset containing several observations of several species (referenced with Longitude and Latitude), and I try to make an interactive page (to share in html after with some people), in which we could select the species of interest through a menu, and obtain automatically the corresponding map of observations from the selectInput value. However, I could not find how to make the map appearing automatically after selecting the species in the menu.

I created a list (list_map) of the length of the number of species. Each list object is the map of each species, as a plotly object to get interactive maps. In the list, each map is named from the codename of the species.

Then, in my code, I did a menu to select the species of interest:

``` {r, echo = FALSE}
selectInput(inputId = "sp_choice", label = "Select a species",
              choices = names(list_map))```
```{r, echo = FALSE}
renderUI({
  HTML(paste("You selected", input$sp_choice))
})```

This works, and for example, I selected in the menu the species "UnOb", and now I would like to automatically plot the corresponding ggplotly map from list_map (which is the object list_map$"UnOb").

However, I can't find how to make the corresponding plot appearing automatically. I tried several codes but I think I am not fully understanding how all of these work...

For example, the code above seems to almost work, it opened another window once but then my computer did not stop charging something that never appeared...

ui <- fluidPage(
  selectInput(inputId = "sp_choice", label = "Select a species",
              choices = names(list_map)),
  
  renderUI({
    HTML(paste("You selected", input$sp_choice))
  }),
  
  plotOutput("plot")
)

server <- function(input, output, session) {
  
  output$plot <- renderPlot({
    print(list_map[names(list_map) %in% input$sp_choice])
  })
}

shinyApp(ui, server)

Thanks a lot in advance for your help!

Welcome to the community! Below is a working example using plotly plots (g1, g2, g3) created with the mtcars package. In your shiny app example, since you are working with plotly objects, you will want to use renderPlotly() and plotlyOutput().

library(shiny)
library(tidyverse)
library(plotly)

g1 = ggplotly(ggplot(mtcars, aes(x = mpg, y = cyl)) + geom_point() + labs(title = 'Map 1'))
g2 = ggplot(mtcars, aes(x = disp, y = cyl)) + geom_point() + labs(title = 'Map 2')
g3 = ggplot(mtcars, aes(x = cyl, y = mpg)) + geom_point() + labs(title = 'Map 3')


list_map = list('Plot 1' = g1, 
                'Plot 2' = g2, 
                'Plot 3' = g3
                )

ui <- fluidPage(
  selectInput(inputId = "sp_choice", 
              label = "Select a species",
              choices = names(list_map)
              ),
  br(),
  uiOutput('my_selection'),
  br(),
  plotlyOutput("plot")
)

server <- function(input, output, session) {
  
  output$my_selection = renderUI({
    HTML(paste("You selected", input$sp_choice))
  })
  
  output$plot <- renderPlotly({
    list_map[input$sp_choice][1][[1]]
  })
}

shinyApp(ui, server)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.