image: thumbnail.jpg
May 2023 Webinar: Version Control and RStudio
Thursday 25th May 2023
With Ryan Johnson, Data Science Advisor at Posit
About the webinar:
In this 1-hour training, we’ll discuss what are version control and git, setting up Git, RStudio Projects + Version Control, RStudio IDE git integrations, and Git-backed deployment to Posit Connect.
Transcript#
This transcript was generated automatically and may contain errors.
So I'd like to welcome everybody this afternoon as it is for us here in the UK. Ryan Johnson's going to come and talk to us at the NHSR community for our webinars. We're really excited. And I'm not going to talk too much because I really want to hear what you've got to say today, Ryan. So welcome, everybody.
Great. Thank you for the intro. I'm gonna go ahead and start sharing my screen and we'll jump right into things. And so just confirm you can see my screen. Okay.
I wasn't. Thank you. Yes, I can.
Perfect. All righty. So we have another training for everyone today for the NHSR group here. And it's going to be all about version control, primarily using Git within the RStudio IDE. This is going to be very much an introductory level into version control and how to use it within the RStudio IDE. So for those that I have not met, my name is Ryan Johnson. I work here at Posit, the company formerly known as RStudio, and I'm a data science advisor. And my goal is to make sure that teams that are using our open source tools like the open source RStudio IDE and our professional tools as well, they're getting the most out of them.
All right. So I like to come in here and just provide some trainings on how to get the most out of our open source and our professional tools as well. We have a pretty good sized group today, which is great to see. So I'd encourage folks if you have questions as we go through to add those to the Zoom chat. And we should have a few minutes at the end to address any questions. But if I don't get to all of them today, I'll be sure to capture those questions and try to follow up in an email. I also like to keep these sessions very casual. So if at any point you do have like a pressing question, if you really want to unmute yourself and scream into the microphone and interrupt me, I'm absolutely OK with that. We'll keep it casual, keep it fun.
We'll keep it a nice, safe learning environment.
Audience poll
OK, one thing I like to do for all of my trainings is I like to get to know the audience a little bit. This is completely optional and it's also completely anonymous, but I do have a few questions for the group and we're going to use a tool called Slido for this. So if you want, go ahead to Slido.com and enter this key code you see in the bottom left hand corner. Or if you have your phone handy, you can scan that QR code you see on your screen. All right, so I'm going to go ahead and start this poll, the QR code still in the top left, and you can go again to Slido.com and enter that ID code.
And the first question I love to ask the group is, what is your job title? So it could be as generic as you want it, or you can get really specific as you want. Just go ahead and type out your job title, PhD student.
Cool. And we'll give folks a few extra seconds here to get logged in to Slido.
All right, lots of data scientists, that's typically the most popular job title for my audiences. That's cool.
All right, we'll give folks another five seconds or so.
Ooh, infectious disease, that's right in my wheelhouse.
Nice. All right, awesome. So keep that screen open, either on your computer or phone. I'm going to switch over to the next question.
And I'd love to know if you are a developer, so you write code on a daily basis, what is your programming language or languages of choice? So you can vote for multiple options here. And at the very bottom, there is an option for I don't write code. So if you do not write any code on a day to day basis, that's totally fine. Go ahead and let me know that again, just as a reminder.
All right, so we have 100% R users. I guess that's not surprising, given the audience, the R community here.
Python users, that's cool. Excel, awesome. SQL, all right, very cool. So mostly R users, that's good. Let's switch over to the next question.
So for the R, Python developers, I'd love to know what tools you use for your development. So what IDEs do you use? And you can use multiple IDEs on a daily basis. And go ahead and let me know that. If you're a SAS developer, for example, you may not see your IDE here, the SAS Enterprise. So just go ahead and select other if that applies to you. All right, it's awesome to see so many R users and RStudio users. Of course, I'm a little biased, but that's okay.
All right, some spider. All right, Jupyter for the Python developers. VS Code, awesome. All right, move on to the next question here. I think I just have two more questions.
So we've mostly been talking about open source tools and open source programming languages. But here at Posit, we do offer some professional tools. And I'm going to touch upon some of them today, not a lot. But I just want to get your familiarity with some of our professional tools. So whether you use Posit Cloud, Workbench, Connect, Package Manager. But if you don't use our professional tools, go ahead and let me know that as well. And it looks like that's the bulk of folks. Again, totally fine. This is just helping me understand who all is on the call today.
All right, last question for the group, which is going to be related to today's topic, all about version control. If you use version control today, let me know what platform you use on a daily basis. So GitHub tends to be the most popular. But if you use GitLab, Bitbucket, SourceForge, some other version control system. But also let me know if you don't use version control. Because that's really important for today and helping me understand the group. And again, if this applies to you, then you are absolutely in the right place.
OK, so as expected, it looks like GitHub is the most popular. And certainly a good proportion of the group not using version control today. And that's great. I mean, this is what today's session is going to be all about.
Introduction to version control and Git
All righty, so I'm going to go ahead and stop this poll. Thank you, everyone, for entertaining me for a little bit. Let's go ahead and dive into it. So today's going to be more or less like a show and tell. I'm going to kind of describe how to use version control within the RStudio IDE. If you have the RStudio IDE open and you have a GitHub account, you're certainly more than welcome to try to follow along. But I'd really just encourage folks to just sit back, grab some popcorn, relax, take it all in, and you can always kind of experiment right afterwards.
So what are we going to talk about today? We'll first kind of set the stage, make sure we have a good understanding of what exactly is version control and what is Git, which is going to be the tool we'll focus on today. I'm going to talk about, I'm going to refer to as the admin checklist. This is something I'm only going to cover from a very high level. Typically, when you use version control within your environment, it's going to be different from one group to the next group to the next group. So because of that, it's kind of hard to go over this admin checklist for every single group, kind of make it broad enough. But I'll certainly pass along some really valuable resources for setting up version control for your environment.
We'll then talk about RStudio projects, which is imperative for using version control with RStudio. We'll talk about some of the Git integrations, which I bet some of you are already very familiar with. And then at the very end, if we have time, we'll talk about a really cool functionality you can do with one of our professional tools that leverages Git. It's called Git Back Deployment.
Again, I'll forward these slides over to everyone. So if you want to take a deeper dive into version control, here are some helpful links. Some things we'll talk about as well, but most of what I gleaned for today's presentations comes from these resources.
All right, so let's first talk about version control and Git. So first, version control. What is it? So it's a practice of tracking and managing changes to software code. Now, I don't know everyone's background. It sounds like most folks are data scientists, but if you're like me, the idea of version control and you're just getting started was like, oh, that's for the hard-hitting software engineers, people doing really powerful stuff, way beyond the practice of a data scientist. That's not the case. Version control is super important for data scientists as well.
Version control is super important for data scientists as well.
And we're going to talk all about it today. And to use version control, you typically have to use a version control system or a VCS. And Git tends to be the most popular VCS out there. There's other ones you may have heard of like SVN, but really, Git is the most popular. And you use Git to manage an evolution of a set of files. So your R scripts, your Shiny apps, your Plumber APIs, your R Markdown, your core code documents, all of your source code altogether. You track that, and all of those files together is known as a repository.
And so you can take your repository, and if you want, you can host it on a repository hosting service. That sounds like most folks are leveraging GitHub today, but there's a variety of options for you to choose from, many of which use Git, such as Bitbucket and GitLab. There's also SourceForge, but really anywhere else you can host a repository and track changes of these source files, that can serve as your repository hosting service.
Now, how do you actually use Git? Git is actually a piece of software. So I would say most computers nowadays have Git pre-installed on it when you buy the computer. But if you don't, you will actually have to physically install it, just like you would install the open source RStudio IDE. It's a piece of software. And it's typically, when it was first designed, and even to this day, it's most commonly leveraged from within the terminal or command line. And again, if you're like me, a data scientist that really didn't have much, you know, computer science background coming into data science, the idea of using Git from the terminal like this or like the command line was extremely daunting. And I was like, I'm not going to bother learning this.
But because of that, most popular IDEs today, including the RStudio IDE, has some type of integrated support for version control. And we're going to talk all about RStudio in this Git tab you see right here. Some of these files you can see being tracked with version control. So this is where we're going to spend today looking at the version control integration with the RStudio IDE. But if you use something like VS Code, which I know some folks are using, that also has great integrations with Git.
Admin checklist for setting up Git
Now, in order to use Git with your RStudio IDE, either your open source version of the RStudio IDE or maybe within Posit Workbench or some other environment like Posit Cloud, you need to make sure that Git can talk to your repository hosting service, right? So Git is the piece of software in your computer and it needs to talk to that repository hosting service such as GitHub. And that's going to require some configuration and what I'm calling here this admin checklist. So we're just going to cover a few things and then I'll show you some resources that can help you kind of check all these boxes.
So the first thing, obviously, you're going to need an account on GitHub or some other type of repository hosting service. It's always a good idea that if you're just starting out with version control and really as you continue on your journey with version control, you want to make sure you're using the most recent versions of R and RStudio as well because we're adding new features to our Git integrations frequently. Again, Git is a piece of software so you have to make sure you can install Git and then RStudio needs to know where you install Git on your system, right? So in addition to that, it also needs to know who you are. So for example, if you're writing some shiny code, a shiny application, and you want to track changes and push it to some GitHub repository, RStudio needs to know who you are, like who's actually making this push? Can they make this push? So that's an important configuration.
And once you've done all that, it's always a good idea to confirm that you can push changes to GitHub or some other repository hosting service and also pull those changes. So that's going to make sure that, you know, you configured Git correctly, but it's also going to make sure that within your environment at NHS that you have the kind of network privileges, so to speak, in order to make that connection between GitHub and your local environment.
So what are some things that can help out with this Git configuration? I would wager that at least some folks on this line have probably stumbled upon this resource, right? This was written by Jenny Brine, one of our software engineers here at Posit, and it is an extremely fun and really easy way to get started with Git that's kind of tailored to our developers, right? Even if you're a Python developer, you should be able to find some really helpful resources here, but this is a completely free online book, so let me just go ahead and grab this link so everyone has it here in the chat.
And I'm going to reference this online resource a few times today, but specifically when you are getting started with Git and you're trying to set up Git to make sure it knows who you are and also connecting it to GitHub and to RStudio IDE, this is going to be a very valuable resource. So make sure you always have this one bookmarked as you start your journey with version control.
Now, also within the RStudio IDE, there are some helpful features. So you do have to make sure that the RStudio IDE can find Git that's installed on your computer or your server. And so there is an option within Tools, Global Options. Right down here, you'll see Git SDN. You can actually provide the path, so where you installed Git on your system just to make sure that RStudio can leverage Git and the integrations built inside.
And then also, there is a package, which I'm also going to wager, given the high proportion of R developers on the line, you've probably stumbled upon this at some point. It's called the Use This Package. And just kind of a side note, I still think this has the best tech sticker. I will die on that hill. I just love this sticker. The Use This Package has a variety of functionalities. It's really important for package development,
but it also does include a lot of helper functions for setting up Git in your environment. All right, so taking a project, for example, and integrating it with a GitHub repository.
So make sure you have this package handy as you get going with your version control journey.
All right, so we're going to actually switch over here in a second to kind of like a live demo of how to use version control within the RStudio IDE. But there's one really important thing that everyone needs to know.
In order to use version control features in the RStudio IDE, you absolutely 100% must use RStudio Projects.
In order to use version control features in the RStudio IDE, you absolutely 100% must use RStudio Projects.
So if you don't use RStudio Projects, we will walk through the process of creating one. But you actually have a few options when you create a project. So you can create a brand new directory. So think of this as you go into your computer, you go to your desktop and you right click your mouse and you select new directory. That's more or less what happens when you create a new directory, RStudio Project.
Maybe you've already created that folder and it has some data, some scripts in it, and you just want to initialize that kind of directory within RStudio Project. You can do that as well. But there's also an option right here at the bottom to create a new RStudio Project from version control. So from a GitHub repository. And that's actually the process we're going to go through today and show you.
But a few more notes about RStudio Projects because they're really important for this talk. And maybe just out of curiosity, I know Zoom is probably not your platform of choice, but if folks leverage RStudio Projects today, I'd love to know if you do. So you have a few options. There is an emoji feature with Zoom. So if you can give me an emoji, like a thumbs up or smiley face or whatever, and just let me know if you currently use RStudio Projects in your day-to-day workflows. And if you don't, no problem. I'm just kind of curious to see if folks are leveraging it today.
All right, I see a few emojis come through. One thumbs down. All right, cool. So this looks like a few folks are familiar with it, but we'll spend some more time on it.
What is an RStudio Project?
So what is an RStudio Project? Again, this is going to be a directory on your computer, either your local computer or somewhere on your server if you're using Posit Workbench, for example. And within this working directory, all of your code lives, right? So your R scripts, your R Markdown documents, your Shiny apps, all of your source code. It also includes all of your data, your results, your tables, your figures. And also it's optional, but you can have all the packages associated with this project actually within the working directory itself.
Really the take-home message with RStudio Projects is that it isolates your project. So you can work on one project and not worry about it impacting some other project on your system, which could be a huge nightmare.
Now for today's session, we're really just going to be focusing on the code part of your RStudio Projects and kind of managing the evolution of your code within the various RStudio Projects.
Connecting an RStudio Project to GitHub
So some folks are using RStudio Projects, some folks are not, and that's totally fine. And we're going to go through the workflow of actually creating an RStudio Project and tying it to a GitHub repository. And when you make that connection, you actually have some choices in terms of your order of operations.
So for example, you could create a brand new RStudio Project, but you actually start on GitHub. So you create the repository and then you pull it into an RStudio Project. So kind of starting over here on the right and following this arrow along the bottom. That's actually where we're going to kind of focus our demo for today.
But you do have a few other options. Maybe you already have an RStudio Project on your system and then kind of down the line, like, okay, maybe now I should set it up with a GitHub repository. You can either push all the contents from this RStudio Project into a new GitHub repository, or you can actually go on GitHub first and then just kind of make that connection between the two, all right? Totally any option, order of operations is totally fine. But just for today's session, we're going to start with this first one.
So these are going to be the steps we're going to walk through today. There's a bunch of them, and we're just going to kind of break it down bit by bit. So let's just really focus in on this first two, all right? And again, if you have RStudio open, you have a GitHub account, feel free to try to follow along. But for the most part, just sit back, relax, and we'll kind of do this one together.
Live demo: creating a GitHub repository
So what I'm going to do is I'm going to start on GitHub. I'm going to create a brand new repository. So I'll show you that workflow. And then we're going to take that repository and we're going to pull it into an RStudio project within the RStudio IDE. So let's go ahead and get started.
I'm first going to start on GitHub. So this is my GitHub account, right? You can see my bald head right there. And we're going to go ahead and create a brand new repository. So here I can click on repositories. And you can see I have a bunch of them that are incredibly out of date. I should probably go clean them up. But let's go ahead and create a brand new one.
So create new repository. You can choose a template if you want. I'm going to choose owner because I'm part of a few different organizations. So I'll select my name. And we're just going to call this test repo. And let's give it a description. I'm going to say test repo for NHS R talk with a happy face.
We're going to make sure this is a public repository, right? Just so that anyone could pull it in if they wanted to. If you want to make it private, you typically need to create or have a professional account for GitHub. But if you have a free account, you can always just make any public repository.
I'm also going to initialize this repository with a readme file, which is always just a good idea. The readme is just going to be information about your repository. You know, what is the purpose? What kind of files are inside of it? So we'll make this readme. And we'll just leave everything else as kind of default for right now. And I'm going to hit create repository down here at the bottom.
Okay, so here is our brand new repository, and it only contains one file, this readme.md, which is the syntax for markdown. And it's actually showing that readme.md right down here. You can see here's the name of the repository. And here's that description that I added. Again, that's the only thing currently in the repository. It's for the most part blank, besides this one readme file.
Pulling the repository into RStudio
Okay, so we've created this repository. Now we're going to go ahead and switch over to the RStudio IDE. And I'm going to do this in the context of Posit Workbench, right? I'm going to spin up an RStudio session, but this everything will hold true if you're using the open source version of RStudio or using Posit Cloud, for example, and I know there's a few users there. So I'm going to click on Posit Workbench.
All right, you can see over here on the right hand side, here's some of the projects I've been working on. I'm going to go ahead and spin up a brand new session. One of the benefits of Posit Workbench is I can choose various IDEs, and so I know there's a few VS Code users as well, and send some Jupyter for the Python developers. But again, we're going to live right here within RStudio. So I'm going to click on this. We'll leave all these parameters as default for right now. We're not too worried about those for today's session, and we'll kick off this RStudio session.
So let's give it a few seconds to boot up. And then we'll jump right into it. Now, let me make my screen a little bit bigger, because that's super tiny.
All righty, so here is the RStudio IDE. Now, it sounds like most folks, if not everyone on the call, is pretty familiar with this IDE. But just to quickly give you a lay of the land, over here on the left hand side, we have our active R console, where I can write some really complex R code, like two plus two. On the right hand side, the top right corner, by default, it's opened up to this environment tab. But when we do the Git integrations with RStudio, it's actually going to be up in this quadrant, this top right quadrant. And you'll notice right now, there actually is no Git integration. That's because I'm not in an RStudio project. So again, I want to drive home that point that you need to be within an RStudio project in order to leverage those Git integrations.
And then down here in the bottom right, by default, it's going to open up your file directory. So, let's go ahead and create a brand new RStudio project. So, there's a few different ways you can do this within the RStudio IDE. You can go to file, new project. That's probably the most intuitive way. There's also a button for this. So, right here underneath the edit, you'll see this little kind of opaque box with the R inside of it and a little plus icon. That's another way to create a project. So, that will do the same thing.
But for me personally, I actually like to work over here in the top right hand corner of my screen. You'll see currently it says project none. That's another great indicator to let you know that you're not within a project. So, let's go ahead and change that. I'm going to click on this box and we'll select new project. And once we do that, we'll get a pop-up menu here. That same pop-up menu I was showing you before. We can create a brand new project in a new directory, an existing directory, or we can pull in a project from version control. And that's what we're going to do today. So, let me go ahead and click on this option.
It's going to ask which version control system do you want to use? And before this demo, we are using Git, but we do also support subversion. So, I'll select Git. And now we need to provide the repository URL. So, let me switch back to my GitHub repository. I'm going to select this green code button and I'm going to copy the URL for my repository.
I'm back into RStudio and I'm going to paste it in. And I'll hit tab. It's going to auto-populate what it thinks the repository name is. I'll just leave it as test repo. And then you want to place this directory, this RStudio project, in whatever location you want. I'm just going to go ahead and place it inside of this directory called posit projects that I created previously. And that's pretty much it. We'll hit create project.
It's going to pull in that readme file from the GitHub repository. And anytime you create an RStudio project, it's always going to open up a new RStudio session. So, we'll just give this a few seconds to boot up.
All right. So, here we are within a fresh RStudio session. We still have our console over here in the top on the left-hand side. You'll see in the file directory, we have a few new files. So, we have this thing called a gitignore. We're not going to talk too much about it today, but just know that RStudio added this file for me. And then you're also going to see this rproj file. Every time you create a project, this rproj file will be in that working directory. So, kind of the topmost directory of your project. And you can always go back to that top level directory by clicking this little box icon in the top right. This is kind of a cool feature that I only learned about like a year ago. So, I always like to kind of show it off.
All right. Now, two important things to note currently. Here in the very top, you're going to see test repo. So, if you're within an RStudio project, you should see the name of your project in the top. That's going to be another great indicator to let you know you're within an RStudio project. And now, if you look at your environment tab, so this quadrant up here in the top right, we scroll over, you'll now see this Git tab right here. All right. And this is going to be that Git integration we're going to focus on today.
And currently we're seeing two files in this directory. This .gitignore and this rproj file. So, again, these were two new files that RStudio added for me. So, currently they're not being tracked by version control, which is represented by these question marks. Basically, Git sees these files and it's like, do I track these? Do I not track these? I don't know what to do with them. Hence why you're going to see those question marks.
All right. But for right now, that's going to be our focus. So, we've gone back to our slides. We've created that GitHub repository and we've pulled it in as an RStudio project. So, let's go ahead and step through a few additional kind of features you can do with the RStudio IDE and the Git integration. And then we'll actually do some more data science-y stuff.
Creating branches in RStudio
So, the next thing I want to demonstrate is how to create a branch. Now, we're not going to focus a lot today on like, you know, when to create a branch, why you want to create a branch. Just know that it is an important tool with version control. Let me just actually go to the slides here and kind of talk a little bit more about branches.
So, what exactly are branches? Well, they allow you to take a detour from the mainstream of development and do work without changing that mainstream. So, if you look at this diagram in the top right, think of these like black dots as like your main development. So, maybe you're working on a Shiny application and this is your production version of your Shiny application. Typically, you don't want to make any changes directly to that polished production application. And what would be nice is if you can just kind of branch off to the side, try out some new features and hey, if it works out great, eventually you can merge it back into that mainstream. So, this is why branches are super important. So, we're just going to show you in this demo how to create a branch using the RStudio IDE.
Well, they allow you to take a detour from the mainstream of development and do work without changing that mainstream.
So, let me come back to the RStudio IDE. If we look over here in the Git tab, we have this additional toolbar and we're going to spend some time talking about all these various buttons. But to start, you're going to see over here on the right hand side, this little pink kind of blocky button, this is the way to create a new branch. And right next to it, it's going to let you know what current branch you're on. So, that main production branch, those black dots I was showing you before, typically it's going to be called main. If you're using like an older repository, an older version, it may be called master. But that's typically, this is the syntax for that main production branch.
So, let's go ahead and demonstrate how to create a new branch. I'm going to click on this button. You're going to get a pop-up menu and I'm just going to call this test branch one, all right? Don't worry too much about remote. We're not going to focus on that today. But that's pretty much all you need to do is just give that branch a new name, hit create. And it's automatically going to run the git commands in the background, all right? So, that's one of the great things about the RStudio IDE and a lot of the other IDEs is that automatically takes care of that talking to git through the terminal for you.
Now, again, I talked about previously making sure that git can talk to GitHub. And this is where you're going to set up those credentials, all right? It may be a little bit different in your environment, but for our demo environment, we can't save these credentials. So, I do have to enter them again.
So, I'm just going to go ahead and do this really quick. I'm going to give my GitHub username.
And then I'm going to supply my password. Don't worry, it just won't show up on the screen or anything. So, go to my password manager, get my GitHub token, copy it.
And I'll paste it right here and hit okay.
You should see some logs right here just letting you know everything that's going on in the background. I'll hit close.
And now if we look right here, you can see I'm now currently on test branch one. So, that's quickly just a very short demonstration of how to create branches. And again, I just highly encourage folks to explore branches more and how they can be used in your workflows. And also just know that a lot of times like branching and merging, sometimes your group may have kind of predefined practices and best practices for how you want to manage branches and merging those branches into production.
Making and committing changes in RStudio
All right. So, coming back here, you've created a branch. Now I'm going to go ahead on that branch, I'm going to make a change to one of the files that we want to track from source code. And we're just going to focus in on that readme file.
So, I'm going to make some changes and I'm going to show you how you can see those changes within that Git tab. We're going to commit them and we're going to push them. All right. So, don't worry right now if you don't know what those mean, we're going to walk you through those steps.
Okay. So, here we are within the RStudio ID, currently on that test branch one that we created. And in my file directory in the bottom right, you can see that readme file. Again, this was created on GitHub and I pulled it into this project. So, I'm going to go ahead and click on it.
And here's the current state of my RStudio, of that readme file. We have the name of the repository and we see the description right here. If I come back to my repository, it's exactly what you're seeing right here.
Now, let's go ahead and make a change within the RStudio IDE. So, I'm going to add a new line and I'm just going to write some text saying, this change was made in the RStudio IDE.
And I'll give another super smiley face. Okay. That's it.
Now, what I'm going to do, you'll see right here, this readme file, the name of it is in this red text. That's letting you know that you currently haven't saved this file. So, when you're ready, you can click on this floppy disk icon to save it. And once you do that, I want you to keep your eyes over here on this Git window. So, I'm going to hit save.
And as soon as I do that, Git notices that this file now has been updated. There is a change in this file and Git has noticed that change.
So, this readme file appears in the Git tab with this little M next to it. If I hover my mouse over and just hold it there, it's going to let you know that this file has been modified.
So, we've made a change to this readme file. If you're happy with those changes, ultimately, what you want to do is get those changes up into the GitHub repository.
Before we do that, we have to commit it. So, let's just take, again, a review of where we currently are. We have this readme file that has been modified and we have these two files that RStudio added that are not currently being tracked.
So, in order to commit those changes to GitHub, we first have to do something called staging it. And I always think of it kind of like a play, like a theater, right? So, in order to, before the show goes on, you basically need to place everything onto a stage, right, to prepare it for the show.
So, in order to stage things using the RStudio Git integrations, you're going to see this column over here in these boxes. So, any file you want to eventually commit and push to GitHub, we're first going to stage it. So, I'm going to want to track that Git ignore. I want to track this test repo rproj file. So, you can see now it changes to A, which means it's added.
And I also want to stage the readme file, those changes that we made. And once we do that, we're going to click on this diff icon. And so, I actually need to stop sharing for a second because I need to share my whole window because we're going to get a new pop-up window. So, just give me one sec here.
And advanced portion of screen.
Okay. So, can you give me like a thumbs up? Just let me know that you can still see it. Okay, perfect.
All right. So, I've staged all these files. Now, I'm going to click on this diff icon, right? And basically, you can click diff, you can click commit. They're going to bring up the same window. So, I'm going to click diff.
And diff just stands for differences, right? Shorthand for differences.
Over here on the left-hand side, you can see the three files that we have staged. And you can select those files. And in the main window down here in the bottom, you can actually see what has been changed.
Any lines that were added or any text that was added is going to show up in green. So, you can see here in the previous state of my readme file. And I've added a new line on three. And then on four, I added some text letting me know that I've made some changes to the ID.
And maybe what would be helpful to show is I'm actually going to make a deletion, all right? So, let me actually delete this line right here. So, you can see what that looks like. I'll hit save right here.
All right, let me go ahead and uncheck this and restage it. And now let me click on the diff.
So, this might provide a little bit more information. So, you can see now I've deleted a line, which is shown in red. And I've added two lines, a blank line and then a text line.
So, that looks good. This is a change I want to make. You can see the other files as well. Again, we're not going to worry about them. But they're all in green because these are brand new files to version control.
Once you're happy with this, everything is staged and ready for the show to start, we can now commit it, all right? And this is going to commit it to my local repository on RStudio.
The commit message is really an art form. This is where you need to be very explicit, like kind of being intuitive. So, when you go back and read these commit messages, you know what happened. But you also want to keep them very short and concise. You don't want to write an entire paragraph.
The commit message is really an art form. This is where you need to be very explicit, like kind of being intuitive. So, when you go back and read these commit messages, you know what happened. But you also want to keep them very short and concise. You don't want to write an entire paragraph.
And so, a common first commit for a project will be the init commit, right? The initial commit. So, that's what we're just going to do for right now. So, I'm going to go ahead. This all looks good. I'm ready to commit this to version control. So, I'll hit commit.
It's going to run those git commands in the background using this git commit. You don't have to worry about it. It'll print some logs. If there's any error messages that occur, these log messages can be a little bit helpful to troubleshoot what's going on.
So, we'll close this. And now you can see my git tab is empty, right? Everything is up to date locally within RStudio. But you're also going to see this little information icon letting you know that your branch, so this test branch one, is ahead of basically the GitHub repository by one commit. So, I've made one commit locally here within RStudio.
And it's saying that, hey, you're one step ahead here. Do you want to push these up to GitHub? And that's what we're going to do next.
Pushing and pulling changes with GitHub
So, right here, you're going to see these two arrows, pull and push. Let's go ahead and show you how to push changes to GitHub. It's pretty darn easy. All you have to do is push that button and it's done.
Okay, you can see now it's pushed up to that GitHub repository. I'm going to switch over to GitHub. I'm going to refresh my page here.
And I also need to make sure I'm on the right branch. So, you'll see currently I'm on main. But if you recall, we did all those changes to our test branch right down here.
So, this is the current state of that test branch on my GitHub repository. You can see that gitignore is now present, that rproj file is now present. And here is my current state of my readme, which you can see down here. This change was made in the RStudio IDE.
So, this is how you make changes locally within RStudio and then push them up to GitHub.
Now, let's actually flip it in reverse. And this is actually great for collaborative work because you can have multiple people working on a single GitHub repository. And maybe you come in in the morning and there's been some changes within the GitHub repository, and you want to pull in those changes before you start your work. So, that's what we're going to do next.
But in order to do that, we first need to make some changes to this readme on GitHub. And you can do that using this little pencil icon you see right here. So, I'm going to click on this.
I'm going to add two new lines. And I'm going to say this change was made in GitHub. Three smiley faces. And we'll hit commit change.
So, it's going to be very similar to the RStudio IDE when you want to provide that kind of informative commit message. And GitHub is actually providing a pretty intuitive one. So, update readme. That sounds good. We'll commit it directly to this test branch.
And there we go. So, I'll come back to my repository. Now, here's the current state of my readme file. It has these two lines, right? One change was made in RStudio. One was made in GitHub.
If I come back to my RStudio IDE, I'm still on that test branch. This is the current state of my local readme file. You can see I just had this one line. So, I want to pull in those changes from that GitHub repository. And to do that, you probably guessed it. We're going to click pull. All you have to do is push it.
And it's already done for me. And now you can see here is the current state of that readme file in my local environment, which matches the GitHub repository.
So, that was quite a few steps that we just kind of plowed through here. We created a branch. We made changes to a file, that readme file. We pushed it to GitHub. And then we pulled it from GitHub.
Creating and publishing a Shiny app
So, now let's do something that's a little bit more data science interesting, right? We're actually going to create a piece of content, such as a Shiny application. And I'm going to show you a really cool feature to publish content to a tool called PositConnect. Again, if you don't have PositConnect, that's totally fine. This is really just kind of highlighting some cool Git integrations with some of our professional tools.
So, let's go ahead and just create a Shiny application. So, I'm going to come back here to my RStudio IDE. I'm going to close out of this readme file. And I'm going to create a Shiny app. To do that, there's actually a built-in Shiny application to the RStudio IDE. So, I can click on this dropdown in the top left and select Shiny web app.
We'll just give this application a random name. I'll say NHS Shiny. And I'll hit Create.
And now you can see in the bottom right, we have this NHS Shiny directory. If I click on it, it has a single file, app.r, which is what you're seeing over here in the top left corner. All right, so this is a Shiny application. If you're not familiar with Shiny, it's a way to create interactive web applications using only R code.
All right, so it's using the Shiny package. And we can run this application locally within RStudio. So, I hit Run App. We should get this little viewer tab that opens up.
And here is a running or a kind of rendered Shiny application. We have this histogram over here on the right. And I can change the number of bins by sliding this bar to the left or sliding it to the right. And you can see how Shiny reacts to these input changes as I slide this bar left and right.
Pretty simple Shiny application, but it does demonstrate the power of Shiny to explore your data interactively.
But let's say you want to share the Shiny application with the world. There are a few different ways to do that. But one of the ways is a tool called Posit Connect. All right, so if I come down to this little flow diagram here, you can basically take content you've created like Shiny applications and push it from RStudio and then host it on Connect to make it really easy to share.
There's actually a few different ways you can publish content to Connect. And I'm going to show you two of them.
The first way, so I'm going to close this application, is to publish it directly from within the RStudio IDE. And you can do that with this little blue icon you see on the top of your screen. That is our publishing button. If I was to click on this, I could choose what app I want to publish. So it's app.rfile. It's what you see over here on the left. And I can publish it to Posit Connect.
Now here at Posit, we call our demo environment Colorado. I really have no idea why. But if I wanted to publish it to Connect, I just have to define that server, give it a name, hit publish, boom, I'm done.
But I want to show you a different workflow where you can take this application, host it on GitHub, and you can actually publish directly from GitHub repository. This is a really good workflow if you have multiple people working on a single application and you just want to pull in the most recent version from that GitHub repository into Connect.
So to do that, we have one additional step we need to go through. And we basically need to provide Posit Connect with the information about my environment, like what R version am I using? So I'm using 4.2. What packages am I using? What versions of those packages? Connect needs to know a little bit more about my environment so that I can deploy this application properly.
So to do that, we have to create what's known as a manifest file. Now it sounds kind of scary, but it's actually pretty darn easy.
And that's with the help of a package called rsConnect. And inside of this package, there's a function called writeManifest. And the only thing you need to do is provide it with the app directory. So my app directory is going to be this tnhs-shiny. And that's it.
Once I hit enter, it only takes a few seconds. Keep your eyes down here. And it creates this manifest.json.
And again, this is all information about my environment for this application. So you do need to create this manifest file in the same directory as the content you want to deploy. If I click on it, just to kind of show you what it looks like, it's not the prettiest thing to look at, but they're pretty easy to understand.
So you can see up here in the very top, you can see some information, like what version of R am I using? What packages I'm using, such as R6. Scroll down, Rcpp. It's also going to include information, like what version of those packages. So this is all just really important information about my environment.
If anyone on the line is familiar with another open source package called renv, this is very similar to a lock file, an Rnv lock file. It just provides a little bit more information.
So this is the only two things you need. Your piece of content, our Shiny app right here, and you need that manifest file basically right next to it. So let's go ahead through the process of taking these changes locally within RStudio and pushing it up to GitHub, all right? So you'll see in the Git tab, where I now have this new directory that's not currently being tracked. Again, Git is like, okay, I've detected this directory, but I don't know what to do with it. Should I track it or should I not track it? Let's go ahead and track it.
We first need to stage it. And to see just two files inside of it, app.r, manifest.json. We want to make sure we stage both of those. And we can go ahead and hit this commit button.
We'll get this pop-up menu. Both files are brand new, which is why everything is green. And let's go ahead and give it a commit message. Again, try to be informative here. Create Shiny app. And we'll hit commit.
I'll close out of this. We'll get that same information window, letting us know that we're one commit ahead locally than with the GitHub. So let's go ahead and push those changes to GitHub.
And that's it. We'll come back to my GitHub repository. I'll refresh my page. I'm still in this test branch. And now you can see I have this nhs-shiny directory. And inside of that directory, I have my app and my manifest file. And this is everything you need in order to publish directly from GitHub.
Deploying to Posit Connect from GitHub
So let me come into Posit Connect. Again, this is just our demo environment here at Posit. And at the very top of the screen, you're going to see this button called publish. And then the very first option is import from Git. So I'm going to select this option. And the first thing we need to provide is that GitHub repository. So let me come back here, come back to this code, copy my repository URL, and then paste it right here.
I'll hit next. And what it's going to do right now is to ask you what branch do you want to publish from? And this is another really cool workflow because you can actually publish the same piece of content but multiple versions of it depending on what branch you want to deploy from. So we can select main or we can select that new branch that we created. So we're going to select test branch one because that's where everything is currently residing. We'll hit next.
It's going to look for deployable directories. Really, it's searching for this manifest file. And if it finds it, it's going to say, OK, I have the information needed to publish the content in this directory. So it's in this directory. And all we have to do is give it a title. So I'll call it NHS Shiny. And we'll deploy the content.
And so what's happening right now is Connect is reading that manifest file and it's replicating my environment. So what our version packages, all those versions, once that's prepared, it'll then deploy that Shiny application directly to Posit Connect.
All right, so we'll just give this a few seconds to boot up. But as a publisher, so I'm the publisher, the owner of this piece of content, I have some additional controls right here in the top, this gear icon. And most importantly, I have access controls. All right, so here's that Shiny application that we created. And it behaves just like it did when it was running within Posit Workbench.
Sharing and access controls
Now I'm going to go through the process of showing you how easy it is to share this content with everyone here on the line once it's hosted on Posit Connect. So again, I have control over access. I can be very specific. I can make it so that I'm the only one that can view it. And that's currently how it's set. I could add maybe your customer success manager, Jacqueline Janus. And I can, again, I have very tight control.
I can make it a little less strict. I can say all users log in required. Basically, if you can log into Posit Connect, you can view this content. So I'd pretty much be all Posit employees. Or I can say anyone, no login required. And that basically means if you have access to this URL you see at the top of your screen, you have access to this content. You'll also notice this URL is pretty ugly. It's got like random letters and numbers. We can make it look a little prettier. Down here, content URL. I can say NHS Shiny and hit save.
So I'm going to go ahead and snag this URL you see at the bottom, come into the chat. And I'm going to paste it and share this application with everyone on the line. And what's really important here is that you don't, as a consumer of this application, you don't have to know anything about Shiny, version control, Posit Connect, R. You can just treat this application as a website. So it's really good for getting these insights into the hands of people that may not be an R developer, for example.
You can just treat this application as a website. So it's really good for getting these insights into the hands of people that may not be an R developer, for example.
Automatic redeployment on git changes
Now, the only other thing I'll mention here as it pertains to version control. So as you recall, this application is being deployed directly from this GitHub repository. If I come in and I make a change to this Shiny application and push it to GitHub repository, Posit Connect can automatically detect that change and rebuild the content for you. All right, so you don't have to worry about constantly redeploying it every time you make a change to the Shiny application. Posit Connect can automatically detect if there's been a change to that GitHub repository and rebuild it, which can be a really nice feature, kind of this continuous integration and continuous deployment.
Q&A
All right, so that's pretty much it. I know we're pretty much, we have like five, six, seven or so questions or minutes left. So I know this session can be a little bit of a whirlwind, especially if you're brand new to version control, but I just wanna highly encourage folks to play around with it. If you get stumped, there's a lot of great resources out there, including this Get Started with R GitHub. And I just, you know, read through this. It's a really fantastic resource, but I do want to leave a little bit of time for questions. So if you do have any, you can feel free to unmute yourself or you can just pop those questions into the chat. I'll try my best to kind of run through some of these.
So Jake asked one question, will R tell you if you are behind a commit from GitHub? So it doesn't necessarily have a, you know, information window, which lets you know if your commit is ahead of GitHub. So it doesn't actually have a way to let you know if you're behind. Kind of short answer there. There are ways you can do that with kind of directly talking with Git.
Another thing I should mention here, if I go back to the RStudio IDE, maybe we'll screen a little bigger here. There's a lot of great integrations with this Git tab in the top right corner. All right, but in some cases, you may need to dive a little deeper into Git and version control. And RStudio allows you to do that. So if you do want to interact with Git directly, you can click on the terminal tab in the bottom left-hand corner, and this will give you a prompt to your terminal, right? And so, you know, I can text to see like, which Git version am I using? I can run things like Git status, you know, give me some more information. So there are ways to kind of interact with Git in a raw form directly within the RStudio IDE in case you can't get everything you need to out of the Git integrations up here.
Good question though, Orville, thank you. Very nice.
Jake, what would you suggest is the best way to start a GitHub project when you have an R project that has already started? Push all files at once or push them where you created them? Yeah, I would definitely say push all files at once. Push all files at once.
And this book right here, let me see if I can find the chapter really quick. Yeah, right. Let me see if I can make this a little bit bigger. But you'll see this early GitHub wins, right? And it's going to say, get started with GitHub. And that workflow we talked about today, you know, we started with a brand new project and creating a GitHub repository first. But Jake, as you mentioned, you may have an existing project, right? And so if that's your kind of scenario, read through these two chapters, 16 and 17, and they'll kind of give you some best practices for getting going there. But I always suggest, you know, any piece of source code, right? Your R scripts, your Shiny apps, any source code that you want tracked, I would certainly say, you know, commit them all at once. But there may be some use cases where you may not want to do that. That's just my preference.
As you get to the terminal, I don't seem to have one. Yeah, so. Coming back here to RStudio, this terminal tab, and James, you may have to let me know, are you using RStudio within, like a desktop application or using it with Imposit Cloud? What's your environment there? Because that may also dictate if you have access to this terminal. Because I don't believe you do have a, right, with a desktop app. So that's interesting.
There might just be a global option a global options, a tool that you need to enable the terminal. I'll try to dig that up for you. But you should be able to access the terminal from the RStudio IDE, the desktop application.
There's another question about connecting to GitLab. The workflow we went through today, we used GitHub as just an example. All right, if you want to connect to push changes to a GitLab repository, you just have to provide that URL for the GitLab repository instead. There really would be no changes within the RStudio IDE.
It's a good question, Jake. I actually have personally, I don't have a lot of experience with OneDrive. So I may have to do some digging there to see if I can find someone who has a little more experience with that. Let me just talk. Just launching, is it because, because I have this in the organization that I work with, my documents is OneDrive and I can use Git. If that's where the question was coming from, yes, it's fine. It just sees it like it's, I think actually it was the same question or comment earlier about projects.
The person was saying that they use them, but what's the benefit? And the benefit is if you use our projects, you don't have to worry about pathways and strings and OneDrive. It just goes, I'm here. And yeah, projects gets around our network drive layouts in the NHS and in public sector. So if that's where the question is coming from, totally, if it's something different, just could you just clarify and we can see if we can help. Oh yeah, so yeah, amazing. Yeah, projects.
All right, we got about one minute. Any last burning questions?
All right, cool. I'll probably be sending over what I'll just actually do in the chat as well. So I'll just pop in my email. This session, I think, depending on your background in R, you might have gotten a lot out of it. If you're brand new to R, I'd also love to know kind of what your feedback is. You know, was it too much content? Did I yell too in depth? I'm always trying to figure out ways to make these trainings a little bit better. So if you want to shoot me an email with some thoughts, more than happy to do that. I'll also pass along an anonymous survey if you'd rather do that more anonymously as well.
But really appreciate you all jumping on. Hopefully, today's session was helpful and certainly excited for any future trainings with NHS. Thanks, everybody. Well, I'd like to say thank you just to wrap this up for your time. I think you've got so much in an hour and there was a comment about how much has been learned even though you've used Git. And I can say the same. You know, there's just things in our studio like that button that you showed. I didn't know there was a project button. Yeah, there you go. I mean, it's right next to the other button I use all the time. So we don't always see these things. So it's really great. You always learn something if you, even if you've been using something for a long time. So thank you so much. And hopefully, we'll see you again. And I will stop. Thanks, everybody.