CartaTerra

← Guides & help

R on the map

8 min read

R gives you each layer on your map as an sf data frame, the standard way R keeps a table with shapes. You can filter, measure, buffer and chart with sf and base R, and save a result as a new layer. R runs in your browser (webR, R 4.6 built for the web), walled off from the page, so your code can't see your account or the rest of CartaTerra. It opens from the side rail into the bottom dock, and is part of Pro (see plans).

Where to find R

  1. Sign in with Pro. Data tools on the map covers the rail and bottom dock.
  2. Add the layers you want to work with. Put them on your map first, from Add layer or with Upload.
  3. Press R on the side rail, in the data tools section (Tables, SQL, Python, R). If you've hidden it under + › Show on rail, turn it back on there.

R opens in the bottom dock, as a tab beside Tables, SQL and Python. Opening it stacks with tabs already there. Press R again while that tab is showing to close it, or use the small × on the tab. The − at the right end of the tab row minimizes the whole dock and keeps your code and results, and the R rail button brings it back. Map settings stays the button directly above + and opens the full settings; it is not in this dock.

R loads on your first run

R isn't part of the page. The first time you press Run, your browser loads R itself, about 16 MB, plus only the packages your code uses:

While it loads, the panel shows a bar and how far along it is, such as "Loading R… 12 of 42 MB". Your browser keeps what it loaded on this device, so the next run, even after a reload, starts without loading it again. If loading fails, you'll see "Couldn't load R. Check your connection and try again."

Other packages. R gets its packages from the webR package repository. sf, jsonlite and the packages sf needs are kept on your device like R itself. If your code calls library() for another package, R fetches it first and the panel shows "Installing packages…". Only packages built for webR work, and these aren't kept between visits.

Your layers: layers[["name"]]

The Examples chips under the box fill in code for the layer you picked: First rows, Buffer 1 mile, Area in sq mi (for area layers) and Bar chart (when the layer has a field to count). Nothing runs until you press Run. Your code is kept in this browser, so it's still there next time.

Show results: print, show and charts

A run that shows nothing says "Done. Nothing to show: use show(df), print() or add_layer()."

Save a result as a layer: add_layer

add_layer(x, "name") offers an sf data frame as a new layer. It shows under Results with its feature count and a Save as layer button. Saving adds the layer to your map and opens it in Tables.

Once it's a layer, Export… at the top of Tables (or Export map… in the map menu) saves it as Shapefile, GeoPackage, CSV, Excel, KML/KMZ, GeoJSON or GPX.

Worked examples

Examples 1 and 2 use a layer of your own called Pins. Examples 3 to 5 use built-in layers: add National Park Service Sites, Hospitals and States (Census) to your map first.

1. Look at a layer.

g <- layers[["pins"]]
cat(nrow(g), "features\n")
show(head(g, 20), "Pins")

2. Buffer by a real distance. Distances in longitude and latitude are in degrees, so project first. EPSG:5070 is an equal-area projection for the lower 48 states, in meters:

g <- layers[["pins"]]
b <- st_transform(st_buffer(st_transform(g, 5070), 1609.344), 4326)
add_layer(b, "Pins 1 mile buffer")

3. Area in square miles, largest first.

g <- layers[["national_park_service_sites"]]
g$sq_mi <- round(as.numeric(st_area(st_transform(g, 5070))) / 2589988.11, 2)
top <- st_drop_geometry(g)[order(-g$sq_mi), c("name", "sq_mi")]
show(top, "Largest park units")

4. Count points inside each area, and map it. Hospitals per state:

pts <- layers[["hospitals"]]
areas <- layers[["states_census"]]
areas$hospitals <- lengths(st_intersects(areas, pts))
add_layer(areas, "Hospitals per state")

Save it, then use Style to color the states by hospitals.

5. A bar chart of a field. The 15 states with the most hospitals:

n <- head(sort(table(layers[["hospitals"]]$state), decreasing = TRUE), 15)
par(mar = c(6, 4, 2, 1))
barplot(n, las = 2, main = "Hospitals by state")

Common errors

Related: Python on the map · SQL querying · Data tools on the map · Upload and export map files · All guides