1

I plotted some plots based on my data, and I use shiny to graph a map and plots to let user select which plot will display, but I got error message:

Warning: Error in <reactive:graphInput>: duplicate 'switch' default value:'"New South...'and'"Victoria"...'
  [No stack trace available] 

Here are my codes:

region_name <- c("New South Wales","Victoria","Queensland","South Australia","Western Australia","Tasmania",
                 "Northern Territory","Australian Capital Territory")

ui <- fluidPage(
  titlePanel("Agriculture"),
  tmapOutput("water_map"),
  selectInput("water_graph","Choose a region to graph:",
              choices = region_name),
  plotOutput("selected_water")
)

## xxx_water_plot is made by ggplot2, and it display correctly without shiny

server <- function(input,output,session){
  output$water_map <- renderTmap(water_map)
  nsw_water_show <- reactive(nsw_water_plot)
  vic_water_show <- reactive(vic_water_plot)
  qld_water_show <- reactive(qld_water_plot)
  sald_water_show <- reactive(sa_water_plot)
  wa_water_show <- reactive(wa_water_plot)
  tas_water_show <- reactive(tas_water_plot)
  nt_water_show <- reactive(nt_water_plot)
  act_water_show <- reactive(act_water_plot)
  graphInput <- reactive({
    switch(input$water_graph,
           "New South Wales" <- nsw_water_show(),
           "Victoria" <- vic_water_show(),
           "Queensland" <- qld_water_show(),
           "South Australia" <- sa_water_show(),
           "Western Australia" <- wa_water_show(),
           "Tasmania" <- tas_water_show(),
           "Northern Territory" <- nt_water_show(),
           "Australia Capital Territory" <- act_water_show()
           )
  })
  output$selected_water <- renderPlot({
    graphInput()
    
  })
}

I don't know what the problem is, the tmap displays correctly, and the selection is in the correct position as I thought, but the plot doesn't show up. I don't understand the error message, there should not be any duplicate values in selection. Can anyone help me? Thanks.

Mologan
  • 37
  • 4

1 Answers1

1

Try it with = instead of <- inside your switch.

switch(input$water_graph,
           "New South Wales" = nsw_water_show(),
           "Victoria" = vic_water_show(),

etc.

<- is an assignment operator, so it assigns and then finds a default value for both the first two, instead of reading each line as a switch element.

Aqeel Padaria
  • 231
  • 3
  • 7
  • It works perfectly! Thank you so much for solving that issue! I thought `=` and `<-` are actually the same thing before, now I figure out the difference. – Mologan Jun 04 '21 at 09:55
  • [This](https://stackoverflow.com/questions/1741820/what-are-the-differences-between-and-assignment-operators-in-r) is a good link that talks about the difference. The gist is that `<-` is an assignment operator, plain and simple. `=` can be one too, but is actually also a way to pass named arguments. For switch, we want to pass arguments, but `<-` doesn't do that, only `=` does. – Aqeel Padaria Jun 04 '21 at 12:34