Emily
Emily

Reputation: 67

SelectInput to change multiple elements in Shiny

Is there a way to have a selectInput change two elements of a plot? For example below, I created a reactive block to manipulate the data I want to plot in geom_point for each selectInput choice and it works perfectly. However, I also want the color of the points to change, a different color for each choice that is automatic, the user need not choose one themselves. So for one input$case points I want what is written in geom_point "orangered2". But if they choose the other input$case option, I would like the points in geom_point to be "gold".

Maybe an if statement but I am not sure where to nest that if so.

I posted a snippet of my UI and server bits.

UI snippet from a tab

tabPanel("Analysis",
                  sidebarLayout(
                      sidebarPanel(width = 4,
                             selectInput(inputId = "case", 
                                         label="Locations",
                                         choices = c("TRUE", "FALSE")))

Server snippet

server <- function(input, output){
data_use <- reactive({
        real_final[real_final$case %in% input$case,]
    })
    output$bathy <- renderPlot({
        autoplot.bathy(shelf, geom=c("raster", "contour")) +
            scale_fill_gradientn(name = "meters above\nsea level", colours = c("plum2", "steelblue4","steelblue3", "steelblue2", "steelblue1"),
                                 breaks = c(-6000,0),
                                 limits = c(-6000,0),
                                 labels = c("-6000m","0m"), 
                                 na.value = "black") +
            geom_point(data = data_use(), aes(long, lat), color = "orangered2", pch = ".") +
            xlab("Longitude") +
            ylab("Latitude") +
            ggtitle("Seal Locations") +
            theme(axis.text.x=element_text(size=6),
                  axis.text.y=element_text(size=6),
                  axis.title=element_text(size=10,face="bold"))

Upvotes: 1

Views: 378

Answers (1)

St&#233;phane Laurent
St&#233;phane Laurent

Reputation: 84519

An option is to return a list with a reactive conductor:

data_and_color <- reactive({
  list(
    data = real_final[real_final$case %in% input$case,],
    color = ifelse(input$case == "TRUE", "gold", "orangered2")
  )
})

Then in the renderPlot:

x <- data_and_color()
ggplot(data = x$data, ......)
color = x$color

Upvotes: 2

Related Questions