An app for plotting species distributions - green dots everywhere


#1

Hello, I was starting to build an app to map species distribution data. However, when I run the code it maps distribution data of…what? I have no clue, whenever I run the app in RStudio and wait for a short time a world map appears with green points on it and I have absolutely no idea what these points represent or where they may be stored at. I rewrote this code, restarted the computer, ran it in the R console, and even ran the code on a different computer, but still they are plotted. If anyone has a hint, please let me know.

Here is the code to paste into RStudio:

library(shiny)
library(maptools)
library(mapdata)
library(spocc)

ui<-fluidPage(
textInput(inputId = “spec”, label = “Species name”, placeholder = “enter species name here”),
plotOutput(outputId = “plotname_here”)
)

server<-function(input, output) {
output$plotname_here<- renderPlot({
distribution<-function(x, y){

  out<-occ(query = x, from=c("gbif"))
  df<-occ2df(out)
  map("world")
  points(df$longitude, df$latitude, pch=19, col=y, cex=0.5)
}

distribution(input$spec, “green”)
}

)
}

shinyApp(ui = ui, server = server)

However, if you type a species name into the text input (e.g Apis mellifera) the dots disappear and you get the actual distribution mapped. Clearing the type-in field makes them appear again.

Also, running only the function in a console does not by default result in these dots.

Best regards,
P


#2

since they start like that, and clearing it makes the dots reappear, it sounds to me like that is the query result when the text field is blank (so it is being triggered on first run and when cleared).

You might want to add some code checking the input before using it in a query.


#3

I added

validate(
need(input$spec != ‘’, ‘please enter a name for distribution mapping’)
)

to the code and now it works. It’s still a mystery to me what it plotted before, but anyway, it keeps the app from plotting it. Thanks!