lecy / neighborhood_change_phx

https://lecy.github.io/neighborhood_change_phx/
1 stars 3 forks source link

Running R Markdown with relative data links #10

Open aehende1 opened 5 years ago

aehende1 commented 5 years ago

I created a single R Markdown file with relative data links that can be found at neighborhood_change_phx/data/Phx_data.rmd. This markdown contains all of the code necessary to download, merge, and rename the variables for all of the maps. However, when I go to test the code in R Studio on my machine, it will not run because of the relative data link.

Ex: setwd( ".." ) NHGIS_data <- read.csv("../data/NHGIS_geog_data.csv")

Error in file(file, "rt") : cannot open the connection In addition: Warning message: In file(file, "rt") : cannot open file '../data/NHGIS_geog_data.csv': No such file or directory

Is there a workaround for using relative data references as opposed to absolute references but still successfully running an R Markdown on my computer? Do I need to set a different wd()?

lecy commented 5 years ago

If the data is in the same folder as the RMD file you should be able to read it as:

NHGIS_data <- read.csv("NHGIS_geog_data.csv")

You would use the "../folder/" convention if you need to read data from another folder in the directory. The ".." will go up one directory level, taking you to the main folder, then the "/folder/" will take you into the named folder.

If the data is in the same directory as the RMD file you can reference it directly.

If it's in a subdirectory you would just use "folder-name/file-name.csv".

aehende1 commented 5 years ago

The data is in the same folder as the RMD, so I updated the code as you suggested but received the same error.

lecy commented 5 years ago

Did you remove this? setwd( ".." )

lecy commented 5 years ago

Before the read statement try getwd() to see where the directory is currently set.

aehende1 commented 5 years ago

The working director is set to the correct folder where both the data and the .rmd are saved. I removed the setwd(".."). The data appears to read in successfully now and the objects appear in the Global Environment correctly. However, when I try to run the code using the data it will not run and I receive the following error:

Error in rename(NHGIS_Data, White_1990 = CM1AA1990, White_2000 = CM1AA2000, : object 'NHGIS_Data' not found

lecy commented 5 years ago

This is why caps should be avoided in object names :-)

NHGIS_data <- read.csv("NHGIS_geog_data.csv")
NHGIS_Data <- rename(NHGIS_Data ...
aehende1 commented 5 years ago

Lesson learned. Thank you!!