/ * jQuery Plugin: Sticky Tabs * @author Aidan Lister aidan@php.net * adapted by Ruben Arslan to activate parent tabs too * http://www.aidanlister.com/2014/03/persisting-the-tab-state-in-bootstrap/ / (function($) { "use strict"; $.fn.rmarkdownStickyTabs = function() { var context = this; // Show the tab corresponding with the hash in the URL, or the first tab var showStuffFromHash = function() { var hash = window.location.hash; var selector = hash ? 'a[href="' + hash + '"]' : 'li.active > a'; var $selector = $(selector, context); if($selector.data('toggle') === "tab") { $selector.tab('show'); // walk up the ancestors of this element, show any hidden tabs $selector.parents('.section.tabset').each(function(i, elm) { var link = $('a[href="#' + $(elm).attr('id') + '"]'); if(link.data('toggle') === "tab") { link.tab("show"); } }); } };
// Set the correct tab when the page loads
showStuffFromHash(context);
// Set the correct tab when a user uses their back/forward button
$(window).on('hashchange', function() {
showStuffFromHash(context);
});
// Change the URL when tabs are clicked
$('a', context).on('click', function(e) {
history.pushState(null, null, this.href);
showStuffFromHash(context);
});
return this;
}; }(jQuery));
window.buildTabsets = function(tocID) {
// build a tabset from a section div with the .tabset class function buildTabset(tabset) {
// check for fade and pills options
var fade = tabset.hasClass("tabset-fade");
var pills = tabset.hasClass("tabset-pills");
var navClass = pills ? "nav-pills" : "nav-tabs";
// determine the heading level of the tabset and tabs
var match = tabset.attr('class').match(/level(\d) /);
if (match === null)
return;
var tabsetLevel = Number(match[1]);
var tabLevel = tabsetLevel + 1;
// find all subheadings immediately below
var tabs = tabset.find("div.section.level" + tabLevel);
if (!tabs.length)
return;
// create tablist and tab-content elements
var tabList = $('<ul class="nav ' + navClass + '" role="tablist"></ul>');
$(tabs[0]).before(tabList);
var tabContent = $('<div class="tab-content"></div>');
$(tabs[0]).before(tabContent);
// build the tabset
var activeTab = 0;
tabs.each(function(i) {
// get the tab div
var tab = $(tabs[i]);
// get the id then sanitize it for use with bootstrap tabs
var id = tab.attr('id');
// see if this is marked as the active tab
if (tab.hasClass('active'))
activeTab = i;
// remove any table of contents entries associated with
// this ID (since we'll be removing the heading element)
$("div#" + tocID + " li a[href='#" + id + "']").parent().remove();
// sanitize the id for use with bootstrap tabs
id = id.replace(/[.\/?&!#<>]/g, '').replace(/\s/g, '_');
tab.attr('id', id);
// get the heading element within it, grab it's text, then remove it
var heading = tab.find('h' + tabLevel + ':first');
var headingText = heading.html();
heading.remove();
// build and append the tab list item
var a = $('<a role="tab" data-toggle="tab">' + headingText + '</a>');
a.attr('href', '#' + id);
a.attr('aria-controls', id);
var li = $('<li role="presentation"></li>');
li.append(a);
tabList.append(li);
// set it's attributes
tab.attr('role', 'tabpanel');
tab.addClass('tab-pane');
tab.addClass('tabbed-pane');
if (fade)
tab.addClass('fade');
// move it into the tab content div
tab.detach().appendTo(tabContent);
});
// set active tab
$(tabList.children('li')[activeTab]).addClass('active');
var active = $(tabContent.children('div.section')[activeTab]);
active.addClass('active');
if (fade)
active.addClass('in');
if (tabset.hasClass("tabset-sticky"))
tabset.rmarkdownStickyTabs();
}
// convert section divs with the .tabset class to tabsets var tabsets = $("div.section.tabset"); tabsets.each(function(i) { buildTabset($(tabsets[i])); }); };
Introduction to R
Laura Brown
June 29, 2021
Introduction to R
R is a free, open-source software environment for statistical computing and graphics. It is able to compile and run across a wide variety of UNIX platforms, Windows, and MacOS. If after running through the examples today, you would like to learn more about R or complete additional examples, you can download and install R
on your own computer.
Installation
The latest version of R (4.1.0 Camp Pontanezen) is available for download and installation on your personal machines.
Installing R
Windows
- Go to https://cloud.r-project.org/bin/windows/base/
- Click the “Download R 4.1.0 for Windows” link.
- When the file finishes downloading, double-click to install, following the instructions in the dialog.
Mac
- Go to https://cloud.r-project.org/bin/macosx/
- Click the “R-4.1.0.pkg” link
- When the file finishes downloading, double-click to install. You should be able to click “Next” to all dialogs to finish the installation.
RStudio
RStudio is an integrated development environment (IDE) for R. The RStudio IDE is free and open-source that works across Windows, Mac, and Linux platforms.
The examples will be described and made available using R Markdown (integrates Markdown, a simple formatting syntax for web pages, with R scripts) and Sweave (integrates LaTeX, a document formatting syntax, with R scripts). R Studio allows easy use and creation of these documents, but requires the knitr R package.
Installing RStudio
RStudio can be downloaded and installed on a Mac, Windows, or Linux machine using the directions provided.
- Go to https://rstudio.com/products/rstudio/download/#download
- Select the appropriate installer for your operating system:
- When the file finishes downloading, double-click to install. You should be able to click “Next” to all dialogs to finish the installation.
Using RStudio
R is the base statistical computing environment. RStudio is an “Interactive Development Environment” makes it easy to use R. It does things like auto-complete, syntax highlighting, and is generally much easier to use.
After you install R and RStudio, you only need to run RStudio.
The initial view of RStudio should look similar to the following (note, the images used in this demo are old and therefore have an older version of R listed):
The RStudio environment is made of several panels. By default there are four panes: Script Editor (top left), Console (bottom left), and the panes with multiple tabs on the right.
Console Panel
The console panel is where you can quickly and immediately execute R code. You can experiment with functions here, or quickly print data for viewing.
Console Practice
You can try typing 2+5
into the console to see the result in R.
2+5
## [1] 7
Scripting Panel (Script Editor)
The scripting panel is where R scripts (or R Markdown, or R Sweave documents) can be edited. When using R Markdown or Sweave, the files consist of a combination of text (including marked up notation or latex commands) and code. If editing a .R file, then the document is just of code.
When editing a RMarkdown or Sweave document, chunks of code can be run using controls at the upper-right of each code section (play button, etc.)
Other Panes
The Environment tab displays the variables, functions, data frames and other objects that are in the current workspace. Data can be imported from several other data formats, or cleared.
For data loaded into the workspace, it can be selected (click on iris
) to view the data set.
The History tab list all commands recently used of associated with a project.
The Files tab lists the files located in the current working directory.
The Plots tab is where graphical output will be displayed
The Packages tab shows currently installed libraries/packages and which are loaded for use.
The Help tab is available to find help for R packages and functions. Don’t forget you can type ? before a function name in the console to get info in the Help section
Installing R packages
Once you have started RStudio, for the projects used in this session you may be asked to install additional R packages. Those packages can be downloaded and installed from the R Console or via point-and-click: Tools -> Install Packages -> Enter “package name” then click to install.
Packages contain functions, and all functions belong to packages. For example, read.csv belongs to the utils package.
R comes with about 30 packages (“base R”). There are over 10,000 user-contributed packages; you can discover these packages online.
Only install a package once. It will likely install several other packages it depends on.
Example: Installing Knitr
The knitr package can be installed via:
install.packages("knitr", dependencies = TRUE)
Note, when installing this (and other packages), the process will show comments of what it is downloading in red.
View and Change your Working Directory
The working directory is where R pulls files to work with. This is where your datasets, scripts, etc. live. It can be any folder location. (It doesn’t have to be the same folder where you installed R.)
R always has a working directory set. Get your working directory with this command in the console: getwd()
You can change the working directory a few ways.
You can set the working directory via point-and-click: Session (at the top) -> Set Working Directory -> Choose Directory
You can set the working directory via point-and-click from the File tab. Navigate to where you would like the working directory to be then select More -> Set as Working Directory
You can also set the working directory in the console.
This is an example of how I set my working directory using the console: setwd(“file/path/cs4821-cs5831”)
Verify that you have the right directory with getwd(). Note that you can see the working directory listed at the top of the Console.
RMarkdown
Look at the RStudio Documentation on how to create R Markdown files and convert them to HTML. For instance, this file can be converted to HTML using the Knit HTML button on the file install-R.Rmd
.
The file can also be converted on the console with
library('rmarkdown')
render("install-R.Rmd")
The RMarkdown, html, and formatting files can be downloaded in the zip
HTML Style
CSS can be used to generate “better” formatted documents. The standard format can be replaced by adjusting the yaml header for this document to include a CSS file, specifically the file min.css
.
---
title: "Introduction to R"
author: "cs4821-cs5831-s21"
date: "January 8, 2021"
output:
html_document:
theme: null
css: min.css
highlight: null
---
Other formatting
Additional formating, templating and themes can be used. Details and resources can be found at R Markdown.
For example, use theme
to specify a Boostrap theme to be used, e.g., cerulean
, cosmo
, spacelab
, united
, etc.. When using your own css style, the theme should be set to “null”, and the CSS style file specified in the css
parameter.
The option of highlight
can be used to select the syntax highlighting style, e.g., pygments
, kate
, monochrome
, etc.
---
title: "test"
output:
html_document:
theme: flatly
highlight: monochrome
---
Render with command line
This file was rendered from the command line using the following commands:
Rscript -e "rmarkdown::render('introduction.Rmd')"
The files needed to create this page are:
_output.yaml
introduction.Rmd
min.css
images\*
The _output.yml file consists of:
html_document:
self_contained: true
theme: null
css: min.css
highlight: null
mathjax: null
toc_depth: 3