 If you've watched any of my previous episodes of Code Club, you know that I absolutely love working in R and making figures using R. RStudio is a great platform for using R, and in this episode, I'll share with you how we can go about installing R, RStudio, and R packages. Hey, folks, I'm Pat Schloss, and this is Code Club. In future episodes of Code Club, we're really going to dig into critiquing figures, making them better, and all around learning about R so we can make our analyses easier and more reproducible. Well, before we can dig into that, I'd like to share with you some of the data that I'm going to be using in these future episodes so that if you're interested, you can follow along with me using the exact same data that I'm using. In addition, for this to work, you need R in RStudio installed. So we'll do that in today's episode. And also, a lot of what we're going to be using comes to us from the tidyverse package, and I'll show you how to install the tidyverse package as well. So first things first, why do we need R in RStudio? Good question. So when we run a command at a prompt in RStudio, or at our command line, we are running R. RStudio is a package, a software package or program that makes it easier to run R. It gives us all sorts of bells and whistles that allows us to edit code in the same environment that we run the code. It makes it easier to install packages. It makes it easier to see plots being generated. So I think of RStudio as running on top of R. You can't run RStudio without R. So hopefully that makes a little bit of sense. So you might ask, why would I ever want to run R apart from RStudio? Well, sometimes, if you're working on a high performance computing cluster, you don't have this graphical user interface like a program like RStudio is, right? So you might have to run R from the command prompt. So it pays to have a little bit of familiarity with running R from the command prompt. In today's episode, I'm going to show you how to install R in RStudio, both in a Mac environment as well as in a Windows environment. So look at the chapters down below in the timeline of this video if you want to jump ahead to Windows, because I'm going to talk about Mac first. And then we'll come back after we've installed the software so I can show you how to get the data, where to put it and how to set up a project in RStudio, and why you might want to do that. So first up, we want to install R in RStudio. I already have it installed on my Mac, but if we go to r-project.org, so that's r-project.org. And over on the left, you'll see download CRAN. So that is the link we want here on the left is a links, all the links for the different sites that have copies of R in RStudio. Here are also the USA links. I don't know if it really matters if you pick the link closest to you. I find that this one, the cloud one, it's maintained by RStudio generally is the most up to date of all the different versions. So this is what I would encourage you to use. So clicking on that link, you'll see there are three different versions that you can download. So I'm going to go ahead and do macOSX because this is the Mac version. And I'm then going to click on this first version, R-4.0.4. That will allow that to download. This will take a couple seconds to download. Once we've downloaded, it will have this package on our desktop. Depending on how you have things configured, it might also put it in your downloads directory. I like to work out of my desktop, whatever. So you double click on that. It will open up this installation dialogue. And I'm basically going to hit continue and agree all the way through. You might have to give it your password. And then it will install. Great. And it tells us that our installation was successful. I'll go ahead and close that. I can go ahead and delete that package because I don't need it anymore. Now what I want to do is show you how we can open up our terminal. So if I go ahead to finder applications and then utilities, you'll see down here that there's a terminal app. So if you double click on that, that will open up our terminal window. I use this terminal app so often that I just have it as a key fixture in my doc. So I'm here at my prompt and I can now type capital R, R, and voila, this will start R. So remember what this looks like because when we open our studio, we'll see the same thing. But again, you can run our commands from here. So two plus two, four, right? Wonderful. So I'm going to go ahead and quit this. Quit out of R and I'll minimize that. The next thing we want to install is our studio. So I'm going to come back up here and do our studio.com. And you'll see a link here for download. Click on that. We want the free version desktop for our studio with the open source free version. There are commercial versions. So if you're working for a for profit, I think you're supposed to use this $1,000 a year RStudio desktop pro. Basically, it gets you more support. Anyway, the free version works great. So I'm going to go ahead and click that download tab. Again, like we saw before, we see the instructions install R. We've already done that. Now we're ready to download our studio desktop. Click on that. And again, this will take a moment or two to download. So that then downloads a disk image. We double click. And we see that we now have this RStudio app. And all we need to do is copy it over into the applications directory. I already have a version. So I'll say I said replace. Type in my password of clumsy fingers. All right. And so that, of course, then is copying our studio over into my applications directory. Again, this is something I use a lot. And so I like having it in my doc. I get this warning from Apple saying it's a downloaded app. Yeah, I don't want to use it. I trust these people. So here is our RStudio window. Right. And so as you see on the left, this looks a lot like what we had over here from the terminal, right. And so again, we are running our from within our studio. If I want to create a script, I can go to new file R script. And this will open up another panel in the top left here, or I can do things like two plus two. And I can then click run. And that will then run that down here in the console. So again, that's one of the nice features of our studio is that it puts it all everything we're doing with R in one place. One other thing I want to show you is if we go up to our studio preferences, this opens up this preferences window. And there's a few things that I want to make sure that you have set to ensure maximal reproducibility. So basically you want everything in the R sessions workspace and history, you do not want anything there checked. Also, save workspace to dot our data on exit, you want that set to never. Okay, so this should be never, and these should all be unchecked. Basically, what our tries to do to help you is to save everything from a previous session, and then relaunch it when you relaunch R. So that might sound like a good idea. But the problem is that if you say defined a variable X in a previous session, and now you open it with all that old session information in a new session of R, X is still going to be defined, and you might not remember that, right? And so we want our scripts to run as though we were starting from scratch, as if you were to give me an R script, I want that to work. I want that script to contain all the information I need, I need to actually run and do the analysis that you're giving me in the script. So again, that's why we always want to have everything here unchecked. And we want save workspace to our data on exit set to never. Okay, so those are really important. And again, we'll do wonders for improving the reproducibility of your analysis. There's other things over here that you can mess with. Some things that you might be interested in changing might be this code tab to change how things edit, display, save, completion, diagnostics, things like that, your appearance, you might, I like white background, I know people hate white background, but you can change all these backgrounds to whatever you want to get a different appearance Dracula, who that's scary. I had modern. Actually, no, I had textmate, which is the default. So again, use whatever you want. It doesn't matter for the analysis, it matters for your peace of mind and what you like to look at. In packages, you can also set the primary crane repository. If you ever want to install a package, you'll want to do it through crane through this. And so if you'd say don't want to use that cloud one that we use the zero repository from our studio, then you'll want to change that here. Okay. So for the most part, that's really all you need to do. I'm going to go ahead and do apply, and then okay. And we're in good shape. The last thing I want to do is show you how you can install a package. And so the package that we're going to use is called tidyverse. So you might already have tidyverse installed if you've already been using our so to check that you can type over here on the right. And yours again, might look a little bit different than mine. But here, if I go ahead and maximize it, if I do tidyverse, you'll see that it pulls up the description that I already have installed. If you don't see tidyverse in this name column, and again, let me make this a little bit bigger so you can see it. If you don't have tidyverse here, you want to install it so you can click this install button here. And and then right here, in packages, you can type tidyverse. Okay. And it'll help you because it knows what packages are already in crayon. And then you can click install. And so this will run down here in the bottom left corner. And hopefully, everything works smoothly. You might get some questions about installing from source or things like that. Go ahead and click why that's fine. So to say yes, you might also get errors along the way. It seems a little idiosyncratic, what error messages you might get. So if you get an error saying like some package is missing, so like RCPP is missing, or can't be installed, go ahead and repeat the steps where over here then if it said that like RCPP, over here, you type RCPP, you could see that and again, assuming it's not installed or not installed correctly, you could then do install and then RCPP to install the RCPP package if that was the package that was throwing errors. I installed fine. So I'm going to hit cancel there. One last thing I can do to make sure everything installed correctly with tidyverse is click this square to the left of the name tidyverse. You'll see that this calls the command down in my console library tidyverse. And if you get this output, you've got everything set. And we're in good shape now for moving forward using RStudio on a Mac. All right, folks, I'm here in Windows with you. I'm actually still in my Mac, but I'm running Windows 10 through the parallels program that allows me to run Windows on my Mac. I'm not a Windows user, but in my experience, the features for R and RStudio on a Windows computer are the same as in a Mac and also on a Linux computer. I don't have a Linux computer at my disposal and I suspect most people aren't regularly running stuff on a Windows on a Linux type environment like with a graphical interface like this. So I think we should be good. I'll go ahead and open up the browser here. Where we're going to want to go is r-project.org. So we're here at the r-project.org website. On the left, you'll see this downloads button. Under that, you'll see a link for CRAN. So if you click on the CRAN, CRAN is the comprehensive R archive network. And so that is the place where you can download copies of R and that will ultimately also be getting packages to run features in R, right? So later we'll install the tidyverse and we'll get that through CRAN as well. On the left side of this screen are a bunch of links to different mirrors or places where you can download R and the packages from. So in my experience, yeah, there are these CRAN mirrors that might be close to me, but at the same time, like I think the University of Michigan has one, yeah, right here is one from Michigan. In my experience, this first one is usually the winner, right? Sometimes the other mirrors aren't up to date as much as this first one. And so I prefer to go to the zero cloud, which is maintained by our studio. All right. So we'll go ahead and click on that link for our project.org. We then see the ability to download for Windows, which is the environment we're on here. And we will get binaries for base distribution. So I think this is what we want. So we'll go ahead and do install R for the first time. Download R 4.0.4 Windows. So again, if you're watching this a few months or years in the future, it might be a different version of R. In general, I want to have the most recent version of R that I can get. There aren't huge changes across versions, but there's enough changes that I like to have the latest. So if I minimize this window, I can go to my file explorer window. And I think it downloaded it to my downloads. Yeah. And so there's my R hyphen 4.04 win, which I double click on that. This now asks me, do I want to allow this app to make changes to your device? Yes. And we'll go through and do English, the license, and I'm basically going to just agree to everything here. I'll use the defaults and that and yep. Again, just just do what just do what it asked you to do. And life should be pretty good for most purposes. This takes a few moments to install. And then we'll be in good shape. Great. So it installed, I can click finish. And we're good to go. If I look at my flag down here, I wonder if if I go down to the Rs. Here we go. There's a directory here for our new. And then you can click on, I think we want this R x 64. And this opens us up into a console where we can use R. So this environment in Windows especially kind of sucks. If you're running R in, say like a Linux console, it would look just like this. Or if you're to run it on a Mac, this is generally what we get. And again, from this window, you can do things like two plus two and get four. So that's cool. And works pretty well. So I'll go ahead and exit out of our there. I don't want to save a workspace image. What I do want to do is get our studio so we can have a better environment with which to work in R. So we will do our studio.com. And we will then click download here. And we want to choose our version. And so our studio is free for most applications, right? So if you're in the commercial world and you want extra support, then you can pay about $1000 a year. They also have servers and all sorts of other stuff. But like I said, for everything that we really want to do, we're going to use the free version. So I'll click download. It detects that I'm running this on Windows. You'll notice also that it required me to install our first which we already did. So we will then download our studio for Windows. Great. So that downloaded. And again, in my downloads, I have our studio can double click on that. Yes, I'd want it to run my computer for me. So yes, next, and we'll complete it. And looks like we're good. We can then we can then come down to our flag and see that we recently installed our studio. This should open our studio for us here. All right. So we're here in our studio. And you'll notice that this window looks a lot like what we had before in that R console. And we're in good shape. And we can do things like open up an R script, which will put up a window in the upper left corner here, where we can type R code. And then if our cursor is on that line, we can click Run. And it then runs it down here in our R console. We also have other panels over here for displaying information about the environment we're working in, history of previous commands, we've done other things. Also, we can change the size of these windows. And so we can see kind of a file manager window here, tab for plots, packages, help, and so forth. We'll come back to this packages tab in a minute. So first, I want to look at the settings. And so we'll go font tools, global options. And looking at this window, the general tab, I basically want everything here unchecked under the R session. So under this section, under workspace, and under history, I want all of those unchecked. And I also want save workspace to dot our data on exit set to never. So the problem with those default settings is that if I now if I quit our studio and come back in, it's going to reload all the old history and all the old variables and objects that I've been working with. That can cause things to go haywire when we're trying to work towards reproducibility. In my mind, we want all of our R scripts to be self contained. So that if I start it with a fresh script, fresh version of R with no prior history, it should run. Now, why would you want that to happen? Well, if I'm working on something and I want to share it with you, you want to know that you have everything in that script that you need to generate the figure, do the analysis that I'm doing. And so again, that's why we want to set all of these defaults to not be checked. And we do not want to save on exit. So that's good. Some of the other things that you might look at in here include things like looking at the code tab allows you to change things like the tabs, the display, all sorts of other things. Appearance might be something you want to change to. I've got the modern text mate, I believe you might also want more of a dark background like Dracula here gives you a different look. This is not going to affect how your code runs, but it's going to affect you in that you want to be working in an environment that is comfortable to you. And so you want to use settings and aesthetics that make you feel comfortable and make you happy to work there. Because if you're not happy, well, then you're not going to want to work in there very much. So again, the default here is text me. So if I scroll to the bottom here, I can click apply. Yes, I so we'll want to restart. So we'll go ahead and restart our studio. I don't need to save that. This will relaunch our studio for us. Go ahead and make that maximized. And we're right back here. Okay. So the one last thing I want to do with you before we go back and talk to those Mac folks is that there's a package we need to install. And so a lot of what we're going to be working in is a package called tidyverse tidyverse. And you'll see that this is empty here. So it's not installed. Sometimes if it was installed, it would show up in here. I'll show you what that looks like once we've installed tidyverse. So we can click on this install button here. And then in this packages window, we can type tidyverse. You'll see that it it helps you to type that in. And so we can then click install. And this might take a moment or two to install tidyverse is actually a package made up of a bunch of other packages. And so instead of trying to install a bunch of other packages, it's easy just install tidyverse and voila, you get everything. Well, I shouldn't say just because sometimes problems happen during the installation process, where it might complain that a certain package couldn't be installed. If that happens to you, then you'll want to repeat this process that we did for tidyverse with that package. So if it complains that, you know, RCPP didn't get installed properly, then you'll want to go back through this process, click on install. And then instead of tidyverse, type in RCPP, get that to install well, and then repeat to reinstall tidyverse, and then hopefully everything works. So that took a little while for it to install to make sure everything worked well. Go ahead and click this square to the left of tidyverse. This will run the library tidyverse command in the lower left corner. Also gives us this output from running that library command, and everything looks good here. Hopefully you got our our studio and the tidyverse packages installed well, whether using a Mac or Windows. I'm sorry, I don't have instructions for Linux users. If you need those, let me know. And maybe I'll make another video for people using Linux. It should basically be the same. Anyway, if you run into any problems, go ahead and please put a comment below this video. And I'll do my best to see if I can help you diagnose what what the problem might be. So we need some data to work with through these videos. So what I will do now is return to Safari. And if I go github.com Riffamonus forward slash raw data. So if you look down below in the notes, you might have to expand that window a little bit. I'll provide a link to get to the R download the R studio download, as well as this raw data download. If you click on releases over here on the right side of the screen, you'll click releases. And again, depending on when you're watching this video and getting the data, there might be newer releases. The latest release is 0.2. So go ahead, make sure you're there, you can then click on source code, get the zip version. That's fine. If you click on that, that will download it. Again, perhaps to your downloads directory, perhaps to your desktop. So mine went to my desktop. The other thing to note about this is that it has a hyphen 0.2 on it. So again, if you're on Mac or Windows, you should be able to right click on this to get a menu and then click rename. And what you'll want to do is remove that hyphen 0.2. So that's called raw data. I'm going to make another directory that I will call code club code underscore club. And I will drag raw data into that. And so now if you open that up, we have a directory for raw data. And so life is good, right? And in there is everything we want. So the last thing I'd like to do with you is to create a project within our studio for these code club episodes. Again, if you're a Mac or Windows, it doesn't matter. If you go file new project that opens this new project wizard, create project, and we're going to do it from an existing directory. So go ahead and click on that. And we now need to use the browse function here to browse and navigate to wherever we have that code club directory. So mine's on my desktop code club. And this is where I want to stop, you don't want to go into raw data. So I'm in my code club directory. And right there is raw data, which is good. And I'm going to click open. And so the path should look something like desktop code club, right? So the last directory is the name of the directory that your raw data directory is within. Okay, so again, it's important to kind of get all this right. Because if you're trying to use my code, then you need to have make sure the directory names are consistent. All right, we can then do create project. This will effectively relaunch our studio. And if you look at the right bottom right corner now, you'll see that we have something different in our files directory, we have raw data, which is where all our data are, as well as a file called code underscore club dot our project. The other thing you'll notice is in the upper left corner, we have the path to the directory we're in. This is our current working directory, two ways that we have to get into our studio. So the first is if I go to code club, and I double click on that icon, I have this file, code club dot our project that if I double click on that, this will open up our studio for me in the correct correct working directory that I want to be working in getting in the correct working directory is is very important. Because if we tell our to open up a file, it needs to we need to know that we're in the right place to kind of give it directions to then open that file, right? So I'll relaunch our studio to show you a second approach to get into the correct directory. So you'll notice in the upper left corner here, I'm in my home directory, I'm not in the correct working directory for my code club work. So if I do file open project, this will open up again, another dialogue window, and I can navigate to my desktop to code club to my code club dot our project file can click open. This will then switch to that project directory. And voila, we see that we have our correct working directory here in the console. If you're ever wondering what working directory you're in from within our, you can always type get wd open close parentheses. And this will give you the path, the absolute path as it's called to your current working directory. And so we are in the right place. Wonderful. So I hope that if you've never installed our studio or in our package before, I really hope that you found today's episode informative. And that helped you to perhaps get over any barriers that you had previously to installing these important tools. Also, I really hope you got that data downloaded. Because as I go forward in future episodes of code club, I'm going to keep coming back to that data. I might update that data as we go forward to add other data data sets that will allow me to illustrate other important parts of our now, if you find this stuff interesting, if perhaps you've been saying, I really need to learn our but I've been really struggling. I also teach workshops built around our analyzing data either using data from microbial ecology, or more general sources of data that have kind of a broader level of interest. Check out the notes below for all the important links for today's episode, as well as links where you can find out more information about upcoming workshops. All right, please tell your friends about code club and we'll see you next time for another episode.