So now you do all your analytical work in RStudio. You create reports about your job in RStudio, either as PDF documents or HTML files. You even produce slides in RStudio.
Excluding asking RStudio to pay your bills, what more could you expect from this IDE?
Perhaps producing websites to share your work on the World Wide Web.
Well, RStudio actually can do it!
This recipe will show you how to produce and maintain a blog directly from RStudio.
We will see how to produce a website composed of R Markdown files and structured in the following recipe.
To perform some of the activities in this recipe, we will employ the wget
utility, which is available both for Unix and Windows OS.
We will use it to download and save HTML files from the Web.
You can find information on wget
installation for Unix OS at http://www.cyberciti.biz/faq/howto-freebsd-installing-gnu-wget-command-port/.
For Windows OS, I suggest that you visit the page by Richard Baxter at https://builtvisible.com/download-your-website-with-wget/.
In this recipe, we will interact with an online GitHub repository using a command-line session. Within RStudio, it is also possible to obtain same results linking your project to the online repository, and handling commits and pushing through the apposite Git pane in the top-right corner.
If you wish to know more about this, I suggest that you go through the Using GitHub with RStudio recipe in Chapter 5, Power Programming with R.
OK, this is easy. Just create a new empty RStudio project.
github.io
repository.Some years ago, GitHub started offering a free website-hosting service, perfectly integrated with its Git repository hosting service. This service is named GitHub Pages and has got its own website at https://pages.github.com.
For our purpose, we just have to create an empty repository (don’t worry, we are going to fill it later) that has the same name as your GitHub user account, for instance, andreacirilloac/github.io
.
Navigate to the following link (assuming you are already logged in to GitHub) to create a new repository, github.com/new
:
RStudio comes with a proper menu control to open a shell/terminal session. Just go and hit it:
You now need to remotely authenticate to GitHub from your remote desktop, run the following command to accomplish this task (substituting andreacirilloac
with your GitHub account name):
git config --global user.name “AndreaCirilloAC”
git config --global user.email “[email protected]”
In your online repository, you will find the URL of your repository. Just go and copy it while I wait for you here:
Now you can make a copy of your online repository on your local machine, that is, you can clone your repository, paste the previously copied HTTPS address in the following command, and run it on the shell/terminal session:
git clone https://github.com/AndreaCirilloAC/andreacirilloac.github.io.git
The people at RStudio provided a website template to help us get started with this really nice way of blogging. Taking advantage of their kindness just requires us to download the template using wget
within our terminal/shell
session:
wget /rstudio/rmarkdown-website/raw/master/_navbar.html
This will add the template to our current directory, which is also our RStudio project directory.
.gitignore
file and add the .rproj
file.
.gitignore
files are used to let the Git system know which files shouldn’t be tracked within Git.
We can specify within this file whatever we want. However, I would recommend that you add your .rproj
file by writing your_rstudio_project_name.rproj
within your .gitignore
file, which should be placed within the RStudio project.
makefile
blog.
makefile
is a file used during website building, containing relevant metadata such as the kind of output to produce.
We can easily download it using wget
, once again due to the courtesy of the RStudio team, from https://github.com/rstudio/rmarkdown-website/blob/master/Makefile.
output.yaml
file.In this step, we will create a .yaml
file, containing data on the output we want to produce from the Markdown code, which is, in our case, an HTML file.
Creating this file can be done using the touch
command on the terminal/session
if you are on a Unix OS:
touch _output.yaml
If you are on a Windows OS, you should instead use the following command:
New-Item -ItemType file output.yaml
.yaml
file:We are now going to add content to our .yaml
file by specifying the required kind of the output and other parameters.
In both Unix and Windows OS, we can do this using the echo
command and the pipe operator:
echo “html_document: > self_contained: false > lib_dir: libs > “ > _output.yaml
index.rmd
file.The index.rmd
file will represent your blog home, so you can add to it all the content you would like to help your reader get oriented within the website.
Refer to the Using one markup language for all types of documents – rmarkdown recipe to see how to fill and style your document.
about_me.rmd
file.This blog usually contains an about me page to introduce the website author(s).
first_pos.rmd
file.Push your new file to the online repository first by adding all modified files to a new commit. Then, commit and finally push from the terminal session:
git add -a git commit git push
Before actually navigating to your freshly built website, you should wait a few minutes to let the GitHub service read and build your website.
After that, your website will be up and running, and you should see it at http://your_account_name.github.io
.