R Notebooks are a ‘flavor’ of R markdown that combine plain text with R commands in code chunks. You can (and should!) edit them, and save it every few minutes like anything else!
Minimize the console. You won’t need it because the output will appear below each code chunk.
To view a copy of your notebook as HTML, just hit Save, then Preview.
Useful keyboard shortcuts:
View the working directory:
getwd()
[1] "D:/Workshops/R-Spatial/rspatial_mod/outputs/rspatial_data"
List the files in the current working directory. Note ‘.’ is shorthand for the current working directory.
list.files(".")
[1] "data" "gadm36_ZMB_2_sp.rds" "LICENSE"
[4] "nb.bak" "nb.css" "nb_geop_01.nb.html"
[7] "nb_geop_01.Rmd" "nb_geop_02.nb.html" "nb_geop_02.Rmd"
[10] "nb_spatial-qry.nb.html" "nb_spatial-qry.Rmd" "nb01_syntax-review.nb.html"
[13] "nb01_syntax-review.Rmd" "nb01_syntax-review_ans.nb.html" "nb01_syntax-review_ans.Rmd"
[16] "nb02_files-folders.nb.html" "nb02_files-folders.Rmd" "nb02_files-folders_ans.nb.html"
[19] "nb02_files-folders_ans.Rmd" "nb03a_tables101.nb.html" "nb03a_tables101.Rmd"
[22] "nb03a_tables101_ans.nb.html" "nb03a_tables101_ans.Rmd" "nb03b_plotting101.nb.html"
[25] "nb03b_plotting101.Rmd" "nb04_import-gis-data.nb.html" "nb04_import-gis-data.Rmd"
[28] "nb04_import-gis-data_ans.nb.html" "nb04_import-gis-data_ans.Rmd" "notebooks_2021-03-18.zip"
[31] "README.md" "scripts" "scripts.zip"
[34] "test_working_dir.R" "ZMB_msk_alt.grd" "ZMB_msk_alt.gri"
[37] "ZMB_msk_alt.vrt"
CHALLENGE. List all the files in your ‘Home’ directory. (Hint: the shortcut for the Home directory is ‘~’) Answer
List the Shapefiles in the ‘data’ directory.
list.files("./data", pattern = "*.shp")
[1] "sf_schools.shp" "veg37.shp" "veg37.shp.xml" "yose_boundary.shp" "yose_poi.shp"
[6] "yose_poi.shp.xml"
Write an expression that will check if a file named “sf_libraries.csv” exists in the data directory.
file.exists("./data/sf_libraries.csv")
[1] TRUE
Import the sf_libraries.csv file.
my_data <- read.csv("./data/sf_libraries.csv")
# View(my_data)
CHALLENGE: What type of R object does read.csv()
return?
Any R object can be saved to disk in a special native R format, which you can load back into R.
Create 1000 random values using the random number function of your choice (e.g., rnorm()
).
rnd_vals <- rnorm(1000) * 20
CHALLENGE: Create a histogram of the values.
Save your random numbers to your ‘Home’ directory using save()
:
save(rnd_vals, file="~/my_random_numbers.RData")
Congratulations, you have completed the Notebook!
To view your Notebook at HTML, save it (again), then click the ‘Preview’ button in the RStudio toolbar. If