R on the map
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
- Sign in with Pro. Data tools on the map covers the rail and bottom dock.
- Add the layers you want to work with. Put them on your map first, from Add layer or with Upload.
- 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:
- Code that doesn't use your layers or sf loads R alone, about 16 MB.
- Code that uses layers, an st_ function or library(sf) loads sf and the packages it needs too, about 42 MB in all.
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"]]
- layers holds one sf data frame per layer on your map, in longitude and latitude (EPSG:4326). Each layer has a short name, the same one SQL uses: lowercase, with spaces and symbols turned into underscores. "Toilets (public lands)" is layers[["toilets_public_lands"]] and "My Hikes" is layers[["my_hikes"]]. The full rules are in SQL querying.
- The sidebar name also works when only one layer has it. layers[["Toilets (public lands)"]] is that same layer. It works in a fresh tab and after you have opened SQL.
print(layers)lists the short name and, when it also works, the sidebar name. - The Layers chips insert the short name, so you don't have to work it out. The chip's own label stays the sidebar name. The short name is the one form that always means one layer: if two layers share a sidebar name, that name does not pick one of them, and the second short name ends in _2 (then _3), in the order the layers are listed, the same as in SQL. A sidebar name that is exactly another layer's short name is that other layer. Use each layer's own short name, which is what its chip inserts.
- Columns are your layer's fields, plus geometry. The app's own fields (such as _id) are left out.
- What's included. Layers with shapes and rows: your own, uploaded and analysis layers, and built-in layers that load in full. Layers from Add from a link and very large built-in layers that the map draws in tiles aren't included. Together the layers can have up to 50,000 features. If yours go over that, the ones that don't fit are left out, and the status line names them.
- A wrong name stops with "No layer named …" followed by the names you can use.
print(layers)lists them too.
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
- print() and cat() write to the console under Results.
- show(df, "title") shows a data frame as a table under Results, with the first 200 rows. The shape column is left out of the table.
- Charts. Draw with base R graphics, such as plot(), barplot() and hist(). Each chart your code makes shows as an image under Results, sized to that pane, up to 6 per run. Rotated x-axis labels stay fully visible.
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.
- There is no feature-count cap. A result over 8 MB is refused, and the panel says so. Filter or simplify a bigger one first.
- Shapes in another projection are converted back to longitude and latitude for you.
- add_layer needs an sf object. To turn a table with longitude and latitude columns into one, use
st_as_sf(df, coords = c("lon", "lat"), crs = 4326). - Pro. Saving a result is part of Pro. On another plan, Save as layer shows "Saving R results is part of Pro."
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
- "R stopped with an error (below)." The console shows R's error message. Check the call it names.
- "That took longer than a minute, so it was stopped." Each run gets a minute. Try a smaller layer or a simpler step. The next run starts a fresh R, so anything you set up before is gone.
- "No layer named …" Check the name, or tap its Layers chip (it inserts the short name). The sidebar name works only when one layer has it. If the layer isn't on your map yet, add it, then run again.
- "add_layer() needs an sf object" Turn your table into one with st_as_sf, or keep its geometry column.
- "That layer is over 8 MB." / "That layer has no shapes to put on the map." Filter or simplify the result, or keep its geometry column.
- "R opens from the rail or the bottom dock." Press R on the rail.
Related: Python on the map · SQL querying · Data tools on the map · Upload and export map files · All guides