Something I have noticed when introducing folks to coding is that they often get overwhelmed pretty quickly. Coding takes a lot of practice, and they are worried about having to "remember" everything.
Let me share a "secret" about people who code every day...
Let me share a "secret" about people who code every day...
For the record, I like coding. I do it a lot, and the more you code, the easier it is to remember all the little things. But here is an example of code I use in LITERALLY EVERY SCRIPT I write, but theres no way I could ever write it out if you asked me to.
At the start of scripts, you often have to set your working directory to tell R where to look (should have specified we are talking about #Rstats here). I used to use:
setwd("File path goes here")
Copying/typing file paths is annoying.
setwd("File path goes here")
Copying/typing file paths is annoying.
But one day someone introduced me to:
setwd(dirname(rstudioapi::getActiveDocumentContext()$path))
Which sets the working directory automatically to wherever your R script is saved. Much more efficient, makes like so much easier.
setwd(dirname(rstudioapi::getActiveDocumentContext()$path))
Which sets the working directory automatically to wherever your R script is saved. Much more efficient, makes like so much easier.
But obviously that one is much more painful to type in, let alone remember. So heres the "secret", literally everytime I start a new script in R, I open up an old one, copy that line of code, and paste it into my own script.
So much of coding is just copying and pasting, and I think we need to emphasize this for new coders. Its super intimidating at first, but folks new to this need to understand just how people can seamlessly bust out new code without referring to old code
If you are new to coding, build up your library of chunks of code that you know work. New scripts are so often pieced together by copying and pasting from old code, that in the beginning there is no need to remember exactly how to type things out.
This is also why commenting your code is so important.
Anyways, I'm going to go have a bagel.