May 2023 Webinar: Version Control and RStudio
videoimage: thumbnail.jpg
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. 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.
Perfect. 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.
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.
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.
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.
All right, lots of data scientists, that's typically the most popular job title for my audiences.
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.
All right, so we have 100% R users. I guess that's not surprising, given the audience, the R community here.
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.
All right, it's awesome to see so many R users and RStudio users.
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.
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.
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.
What we'll cover today
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.
Version control and Git
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.
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.
Version control is super important for data scientists as well.
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.
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.
Admin checklist for setting up Git
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 you have to make. 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. 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.
RStudio Projects and version control
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.
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.
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.
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.
Demo: creating a GitHub repository and RStudio project
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.
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.
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. So I'm going to click on Posit Workbench.
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.
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.
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.
Creating branches
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. 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.
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.
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.
Staging, committing, and pushing changes
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 IDE, 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.
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. 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.
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 IDE.
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.
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. 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.
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.
Pulling changes from 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.
Git-backed deployment to Posit Connect
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 Posit Connect. Again, if you don't have Posit Connect, 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.
