Using the Methods Service in R; a tidy workflow
Efficient use of methods using functions and tibbles
In this post, I’ll briefly discuss how I’ve used a number of tools to efficiently handles inputs and outputs from the methods service in R. The example that I will go through uses the 4 methods that are part of the Urban Forest pipeline. The process that I have put together has been packaged and is used in a demonstration shiny app. This is not currently deployed anywhere, but is available in the UN Global Platform Gitlab. I will mention at this point that this is still experimental and more for demonstration purposes than for large scale production. If you find this workflow useful, please let us know.
Storing way_id coorinates in list columns
List columns are convenient ways of storing lists as an element in a row of a tibble. In the example below, I am calling the HighwayScrapeR method, the output of which is a list of lists.
I can make this into a tibble using the following code:
What the snippet above is doing is a little confusing at first. In the
result list, each way_id is an element in the list. Each of these elements has 3 elements within it, one for longitude, one for latitude and one for heading. In this function, we using lapply over the
result list which maps the unlist function to each element within each way_id element. At this point, we have a list with an element for each way_id and a matrix in each element.These elements are then changed into tibbles.
do.call ensures that rbind is applied to each of these tibbles, making the final input_tibble and finally a column is added for way_id.
Lists of functions
For ease of comprehension, I am using function for anything that I am writing in my local R environment and method for any method service method that I am using.
First I define a call-creation function to create an algorthmia object and set some options. The client has already been defined in this case.
This is just for ease, as I can then apply this function across a number of methods.
Next, I define a function that allows me to create a list of functions:
The methods that I need are passed in as a character vector. These are converted to a list and then the call-creator function is called on each element of that list.