-1

I wanted to make an R notebook containing a map, but I can't knit it to HTML. I am getting the following error:

Error: could not find function "plot_my_connection" 

I tried to find a package I could use, but I couldn't find anything. What edits are needed for my code to run, so I can knit to HTML?

The error happens on the following call

for(i in 1:nrow(all_pairs)) {
  plot_my_connection(
    all_pairs$long1[i],
    all_pairs$lat1[i],
    all_pairs$long2[i],
    all_pairs$lat2[i],
    col = "#99ccff",
    lwd = 1
  )
}
mhovd
  • 3,724
  • 2
  • 21
  • 47
iga
  • 1

1 Answers1

0

Did you first run and define the function?

I am assuming you want this

# A function to plot connections
plot_my_connection=function( dep_lon, dep_lat, arr_lon, arr_lat, ...){
    inter <- gcIntermediate(c(dep_lon, dep_lat), c(arr_lon, arr_lat), n=50, addStartEnd=TRUE, breakAtDateLine=F)             
    inter=data.frame(inter)
    diff_of_lon=abs(dep_lon) + abs(arr_lon)
    if(diff_of_lon > 180){
        lines(subset(inter, lon>=0), ...)
        lines(subset(inter, lon<0), ...)
    }else{
        lines(inter, ...)
        }
    }

From this resource https://www.r-graph-gallery.com/how-to-draw-connecting-routes-on-map-with-r-and-great-circles.html

Make sure to also load any relevant libraries, e.g. maps, gcIntermediate and geosphere.

mhovd
  • 3,724
  • 2
  • 21
  • 47
  • Thanks! The code you gave me indeed knits, but the map then lacks visible connections between cities. Maybe I could add them somehow anyways? – iga Jul 12 '21 at 23:04
  • That sounds like a different issue outside the scope of this question - it would be better if you opened a new question for that part. Be sure to include a minimal, reproducible example including code and data. – mhovd Jul 13 '21 at 08:09