 Hey folks, I'm Pat Schloss. Over the past couple of months, I have been trying to do a deep dive into what I consider the most critical components of base R that anyone using R should learn sooner rather than later. Yes, we have great tools like the functions and packages that are part of the tidyverse, but when it comes down to it, there are other parts of R, the base R, the things that come installed with R that are really important to know. Now this episode isn't really about using base R. It's about maybe using R and the base of R. Now what do I mean by that? Well, in all of the episodes of Code Club that are part of this series and probably nearly all of my episodes, we have done everything in RStudio. RStudio is something that's called an integrated development environment, an IDE or ID. I think I've heard people pronounce that. What is RStudio? Well, RStudio runs on top of R. So when you install RStudio, you also have to have R installed. In fact, when you open up RStudio, you'll get this window on the left side, this console window, which is R, right? Everything else that's in here as part of RStudio is extra. This is stuff that RStudio brings to R to make your environment, to make your time in R more attractive and easier, right? So we have like a finder window that allows you to see what does your project look like. You have information about the environment, what variables are loaded, what's the history of different commands you can use get within RStudio. It makes things easy, right? I can open up a text editor and type things in here and have them run down below in my console, right? It just makes everything easy. So RStudio is wonderful. Why would we ever not want to work in RStudio? Well, there's actually a couple of different reasons why we wouldn't want to work in RStudio. So the first is that it's graphical. There are oftentimes where I am working on a project that requires a lot of computational firepower. And so I have to do that up on high performance computer cluster up on that HPC. Everything is done through a terminal. I don't have a graphical interface. And so I often run R from that command line interface without the benefit of using RStudio. A second reason why I might not want to use RStudio and I'd rather work in the terminal is if I'm trying to automate things. So if I have a project and I've got this workflow of different scripts that I need to run to process my data, then I don't want to do that within RStudio because that's going to require a lot of input from me after on this command and then this command. And I'm doing kind of a lot of manual entering of commands in a terminal. I can work with a workflow manager like make or snake make to automate the running of all my different scripts and to keep track of the dependencies for those different scripts. That's just something that's not so easy to do within RStudio. So for those two reasons, the lack of a graphical interface and the need to automate things, oftentimes while I will develop a lot of my code here in RStudio, I ultimately end up running it over in a terminal. And so in this episode, I'm going to show you how we can work within the terminal and what things are different, what things are the same. And you know how you can recreate an environment that's a lot like what we have here in RStudio, but makes use of the great features of running things from the command line. So as you know, I have a Mac. This is my terminal window, the command line interface, you might have a percent sign instead of a dollar sign. But this should generally look like what you have if you have a Mac, or if you're on a Linux computer, or if you've logged into a Linux computer on your high performance computer. In a moment, I'll show you what things might look like if you're on a Windows computer. So again, if you're on a Mac or Linux, you'll have this tilde and then a dollar sign or tilde percent sign that tilde indicates you're in the home directory. So my distances project is on my desktop. So I can do CD desktop, distances, and then I can do LS. And then I can see the directories and files that are in my distances directory. And then I can type R and voila, there is my R session. This looks a lot like what we might expect to see in the left side here of our RStudio session, right? This is the R component of RStudio. Now let me show you what things would look like on Windows. So I have Windows running on a parallels emulation on my Mac. So things might look a little bit different than what you're used to if you're running Windows for real. But again, down here in type here to search, I can do cmd or type in command prompt to pull up that app. Again, I'm in my home directory on the Windows side of the computer. I can do CD desktop, CD distances. And then if I do dir, I can see the contents of that directory. And I can then type R and that will again launch R here in my terminal. Now for Windows users, I did have to install R on my path. If you Google for Windows path, add, you will find instructions for how you can add R to your path, so that at the command prompt, all you have to do is type R and voila, it should work. We'll come back to the Mac environment for the rest of the episode, but know that things should be the same between Mac, Linux, and Windows from here on out. If I want to quit out of R, I can do Q, open, close parentheses. Again, that brings me out. Sometimes depending on how you have R configured, it might ask if you want to save the current workstation, click no, because you don't want to save anything between different sessions. That gets a little bit too far into the weeds of configuring R that I want to go in this episode. If you want more guidance on that, by all means leave a comment down below this episode and we can follow up with that later. Now another way that we can run R from the command prompt is by using a function called R script or an app called R script. So we can do R script, and then I can give the path to the R script that I want it to actually run. And so I could do code forward slash analysis dot R. This will then run all the code that is in my analysis dot R script. So what is that? Well, one other thing that we need to make our useful on the command line is a text editor. And so one text editor we might use would be nano. So we could do code forward slash analysis. So I'll go ahead and add a line to this to write something out. So we could then do write TSV. And the object so x will be dist tbl. And then the output file will be dist dot TSV. And so I'll go ahead and save this and exit out. And again, I can rerun our script code analysis dot R. It does all that. And now if I do it now less, I see sure enough, I have this TSV. And in fact, if I had done nano on this TSV, I can then see that output. So if I only want to work within the terminal, this will work. I won't say it works great, because nano is a bit of a pain in the rear. Other alternatives to using nano as a text editor within the terminal would be to learn Vim or Emacs. I personally prefer to use a text editor called atom. So Adam will run on any operating system. And so I can do Adam and then period, this will then open my text editor with my project directory on the left side, and then all of my code all my text here as well. And you can see I've got this TSV. I've got my code window I can open up analysis dot R. And so one of the nice things about this is that I can come back to my terminal, I can launch R, and then I can kind of go back and forth, right? So I have a really nice environment to work in here as a text editor. There's a variety of features I've added to Adam to make it easier to write our code here in the text editor. So for example, I can highlight these lines, I can hit command enter, and then it will run those in my browser. So I can also do this up on a HPC, right? So there are a variety of tools that you can add to these different text editors to make it easy to work on your local computer as a terminal while interacting with code and with the computers up on the HPC. A variety of other text editors that are out there that I know people find useful is BB edit, I used BB edit or its predecessor text wrangler for a long time. People in my lab have really grown to like virtual studio code. It's put out by Microsoft, it's completely free, and it does a really nice job of working with kind of this remote connection of using a high performance computer. Another option I know people in my lab have enjoyed using is Sublime. I think you have to pay a little bit extra to get the good features, but people really like it. But really if you want something free, I think Adam is very popular, virtual studio code is very popular. Again, what we're trying to do is replace the text editor functionality that we enjoy with RStudio, but in a case where we perhaps can't be running our studio to develop our code, say if we're working with a remote computer like on an HPC, one of the things you've perhaps grown to like about working in our studio is the integration with get and GitHub. Well, Adam, it was originally produced by the people that develop get and GitHub. And so one of the things you'll notice over here in my project tab is that I have different colors of files now, right? So orange or this orange is yellowish color indicates that something that's already part of the repository has been changed, whereas green is something that's new. If it's in black like this, read me, then it's already committed and nothing's been changed. There are other things that if it's grayed out, then this is something that get is not actually tracking. You'll notice down here at the bottom we have get with the two. This will open a graphical interface within Adam for working with get and GitHub to make your commits and different types of things there. I typically if I'm working in this environment would be doing my get messages at the command prompt rather than within Adam here. But again, people find both to be really useful. Any of these different text editors will have either add ons or built in features that allow you to integrate directly with get and GitHub so that you can use version control while you're up on that high performance computer. And again, is just really nice feature. I want to illustrate a couple other things that I think are important to realize when you're working from the command line. So one of the things that's nice about working in our studio is again, the bottom right corner of our studio is often where you might see plots being displayed for you. And so how do we display plots here in the terminal? Well, perhaps we could do something like disk tibble, and I could then pipe that to ggplot. I could do AES x equals f3d0, y equals f3d1. Again, these are meaningless plots. And then I could do plus geome point. So that runs the code in R and opens up a separate window that allows me to see the plot being represented. Again, if you're working on an HPC where you don't have that graphical interface, you're not going to be able to see this figure. Ultimately, that's why I like to use things like gg save, where I can then say test dot PDF, and that will then save the image. And if I quit out of R, I can then see that I've got test dot PDFs where I could do open test dot PDF. And there you go. One of the other things that we like about working within our studio is that it's easy to get help, right? And so how would we get help outside of our studio? Well, the question mark is a powerful tool from base R. See, there we got some base R in. So if you put in the question mark, and then the name of a function, that will then give you the help page for that function. So if I do question mark g sub, then this is going to give me a help page for all of the things related to g sub grep grep all sub, these are base R versions of functions from the string R package, right? And so you can hit the space bar to kind of tab through different elements of this help page, right? And so again, this would be the same thing that you would find over in our studio, if you went to the help tab and typed g sub, right? So this is the same page here on the lower right corner that I would find over here in the terminal. One of the other things that we really like about working within our studio is that it's easy to install and update packages. Well, you can do the same thing, of course, within R from the command line by typing install dot packages. And then in quotes, let's put the name of a package, I'm going to put the here package. So this opens a dialogue window asking me which Cran mirror I want to install from, I'm going to go ahead and do zero cloud. And I'll click okay. And so this will then install the here package. If you watch the last episode where I talked about paths, the here package is a helpful package for kind of making using paths a little bit easier. So again, this is how we can install packages from within R at the command prompt, we could also do update packages. And let's put in ggplot to and then that function will update my ggplot to package if it needs to be updated. And I think it was already up to date. So nothing really happened there. Finally, I want to let you know that if you are a little bit leery of working from the command line or using these text editors, I would encourage you to perhaps reach out to your systems administrator and see if they could install our studio server for you and for your other colleagues at your institution. Our studio server is a browser based implementation of our studio. And so while it perhaps doesn't help all that much with automation, it does give you that nice integrated development environment that IDE up on your HPC. I find it a little bit frustrating because I like to use a lot of keystrokes to do simple things. And a lot of those keystrokes for our studio overlap with keystrokes for my browser. And so that just causes headaches for me, right? If nothing else, I hope that this episode helps you to have a greater appreciation for all the great things that our studio allows you to do. At the same time, I hope you also appreciate that our studio isn't the only game in town for running R. You can create your own makeshift IDE environment for working within R. You need a terminal, which we've shown here. You need a text editor. And then you need to work with your file system to output files, output figures, and then more easily see them on your computer, especially if you're working on a remote computer where you don't have that easy access. Again, this all takes practice in getting comfortable and used to the different environments that you're working in. I would encourage you to try these different systems. Perhaps it's easier to work with them on your local computer before you work with them on an HPC, because it's easier to see what's going on on your local computer. Practice with this, and we'll see you next time for another episode of Code Club.