Introduction

Gesopatial Data Carpentry for Urbanism with R

This package is meant to help you learn how to apply geospatial analysis in the urban context using RStudio as an open-source and powerful software for automation, rescalability and reproducibility.

The online materials of this instruction package is meant to help you working with R from scratch on your own, up to an elementary stage where you can perform first steps of major analytical methods and tools on a spatial context for your professional/academic work or for your own learning.

The package is a combination of online structured teachings from the Carpentries Incubator and recorded video materials from the previous workshop that was held in-person at TU Delft in 2024.

Episode 1: Introduction to R and RStudio

Videos Credits: Claudiu Forgaci (Introduction), Javier San MillánTejedor, Aleksandra Wilczynska

In this episode you’ll learn about the GDCU and its aim and objectives with our instructors giving detailed insightful context of the upcoming materials.

Overview

What is R?

Furthermore, the R and Rstudio as your working toolset is introduced to have a better understanding of the capabilities of working with R through your projects.

Setting up Rstudio

Check the GDCU Carpentries website to learn about the installation steps and R package requirements regarding your OS.

Check the summary and setup

Project Management in Rstudio

To start your work in the Rstudio environment, creating and handling a project package is crucial. You have to go over your project folder for file handling, data storing and management, or simply organizing your workflow. Check the video below how the instructors introduce the main components of every R project folder and how to organize them appropriately.

Organizing the working directory

Create R Project

Now, it’s time to create your project folder. This could be done in several ways. Spend some time creating your project by clicking and going, or type a command using an R script all the way along.

The first video explains to you how to create an R project.

The second video introduces the ways of interaction with R. You can also check the detailed instruction of how to interact with R in the Carpentries.

Create Sub-folders

To create sub-folders within your project folder, there are multiple ways to expand and organize your project folders.

Create a script

The key component to structuring your analytical workflow is to write the script. You need to create an R script file inside your project’s folder.

Create a script: Pros & Cons

Here’s a short discussion of what are the uses and difficulties of creating a script in the Rstudio environment.

Running the script

Now that you have your file and written script, you’re ready to run the code and operate your first lines of command. Learn more about the console details and Escaping.

R Packages

Intro

The R script can handle general operations for us. However, if we want to assign more complex and modular functions, we would need a set of tools that can fulfill the specific tasks for which they are developed. These toolsets are called “Package”. You need to install the package and run its libraries to activate them.

Cheatsheets

It is totally normal not to know the package names, specific functions, or general syntax by heart. We’ve made a cheatsheet for you to take a quick look at whenever you need to write down a specific function or package in your script. Take a look at the cheatsheet files or save them on your device in case of needing them.

Attention

Your auto-completion may not operate the same way as the one in the instruction video. The auto-completion options in your environment may vary based on the installed packages in your Rstudio.

Installing Tidyverse

Now it’s time to install one of the most commonly used packages in R, Tidyverse.

Think about the installation command; you’d need to write a specific line of code and run it in order to install a package. Heads up! once your install a package, you wouldn’t need to run its code anytime after you reopen your project.

Code commenting

A question

Now that you’ve installed your package, you don’t need to run that line again. But what if you need to keep its script? Let’s say you’d need to share your script file with a colleague who may not have the same packages installed on their device, so you need to keep the installation codes inside your script file.

A way to prevent your package installation codes from being run is to turn them into a comment. By doing that, you can keep your code lines inside your file without running them effectively.

Some tips

There are some general ideas you may like to know about commenting in code. Not only does the “#” sign make your code line unexecutable, but also you can literally write your comments on each line of code in case you need to provide information or trace your steps later while you’re reviewing your code or sharing it with somebody else.

Handling Paths

There’s an important step to address your folders’ locations inside your script. In R, you wouldn’t need to copy-paste a complete path directory, as long as you’ve saved the files inside the main project folder. Here you’ll learn how to configure your directories.

A small note

You may notice some differences in your script compared to the video. It is because of the Copilot add-on that suggests further code lines. That is not necessary in your current progress.

Variables and assignments

We introduced the packages and sets of specialized operators and functions. Speaking of which, along with simple operations that are already demonstrated, you can also define variables and assign them to new values using the “<-” key combination.

Check out the step-by-step description of assigning values in the Carpentries.

Discussion

Let’s say you want to assign a variable to another existing variable in your script. What do you think will happen? Check your response with the video below.

Download files

You’ve made it so far. That’s quite a progress!

Now, this is the last step of your preparation. You need to download the files you need to work with. Take the notes from the video and the Carpentries’ callout.

The error happened, right? What do you think caused the error? Take some time to think about it and play the next video while the instructors are trying to fix the error.

Warning and error messages

The warning messages are pretty common to appear when running your code. In general, not all of them are serious problems that interrupt your code running, but you may need to be aware of them.

Although the warnings are not critical, the error messages on your R console are the high-priority problems that indicate your code is not operating properly and the tasks cannot be completed.

Think about how to fix the error in the script and compare your evaluation with the following video.

Downloaded file path

The error is fixed. Once you download your files, you’d better check where they’re stored. This is crucial for any next step you take working with your data.