--- title: "Naming conventions in R" author: Abhijit Dasgupta --- ```{r setup, include=F, child = here::here('slides/templates/setup.Rmd')} ``` ```{r setup1, include=FALSE} library(pander) library(fontawesome) library(emo) library(countdown) ``` class: middle,center,inverse # Naming rules --- ## Why do we need names? Everything in R is an object that can be named Unless you name an object, it doesn't stay in the R environment You name an object using the assignment operator `<-` I read this as a left arrow For example ```{r, eval=F} spine_data <- readr::read_csv('data/Dataset_spine.csv') ``` Here, we read the CSV file into R, and then save it in the current R session by naming it `spine_data` --- ## Syntactic names Following these rules will always give you valid object names without the need for additional trouble. 1. Contains letters, numbers, `.` or `_` characters 1. Starts with a letter or `.` not followed by a number 1. Is not a reserved word () -- So `.2way` is not a syntactically valid name --- ## Going wild You can actually use **any** name for an object, as long as you encase it in backticks The name can then include spaces, special characters, start with a number, etc. Still, don't use reserved words. -- .pull-left[ You can do `` `First variable` `` `` `2 many bikes` `` ] .pull-right[ .heat[But should you?!!] ] --- ## Naming conventions There are several prevalent naming conventions in R + snake case, e.g., `my_variable` + lower camel case, e.g., `myVariable` + upper camel case, e.g., `MyVariable` + period separated, e.g., `my.variable` + lower case, e.g., `myvariable` Choose one and stick with it. It will save you some sanity. ----- > The **janitor** package has a nice function called `clean_names` which will transform the column names in a data.frame to follow a particular naming convention, with the default being snake case. --- background-image: url(../img/janitor_clean_names.png) background-size: contain --- ## Resources The following commands, typed in the R console, will get you to the corresponding help files. + `?make.names` + `?assign` --- class: middle,center,inverse # Brackets --- layout: true