Copy and paste data into R

How can I copy and paste data into R? For example, you see a small data set on the web, perhaps in a blog or here on StackOverflow, and you want that data set in your R session. This is a common task for many of us, and there are several ways to go about. Below I present a solution based on this blog post that I have found useful.

First, copy (e.g. “⌘ + C”) the data set.

Then, paste (e.g. “⌘ + V”) to create an R character vector:
x <- " A B C D
1: 2 2 5 3
2: 2 1 2 3
3: 3 4 4 3"

Next, use the read.table() function:
y <- read.table(text = x, header = TRUE)

Done! The data is now in a data frame:
class(y)
[1] "data.frame"

Line Break in R Markdown Reports / R Notebooks

Lately I have been struggling to find a way to accomplish a line break in an R Markdown Report / Notebook. It is in fact described in the RStudio documentation, but it is rather difficult to find and not so well explained. This is what they have to say about it:

A backslash followed by a newline is also a hard line break.

Easy to overlook, and perhaps not so pedagogical.

What they mean is this:

\⎵⎵

That is, a backslash ( \ ) followed by two space characters ( ⎵⎵ ).

Enjoy!

A Simple Modularized Shiny App

A Shiny module is a piece of code that is included as part of a larger Shiny app. I wrote this small Shiny app as I could not find a really simple example elsewhere. It is based on the “Hello Shiny” example that comes with the Shiny package. You can run it with:

library(shiny)
runExample("01_hello")

Hello Shiny is a simple application that uses the sidebarLayout, with a sliderInput in the sidebarPanel and a histogram displayed in the mainPanel. My Shiny app modularizes the sidebarPanel part of the sidebarLayout.

You can see it in action here: https://xplor.shinyapps.io/module_example_1

The code is available here: https://github.com/samuel-bohman/module_example_1

Comment below if this was helpful to you.

Cheers!

Update!

Added a second version of modularized “Hello Shiny” that uses renderUI in the module that switches between two sliderInput functions.

You can see it in action here: https://xplor.shinyapps.io/module_example_2/

The code is available here: https://github.com/samuel-bohman/module_example_2

How to install R and RStudio on Ubuntu 16.04.1 Xenial

    Add R repository

sudo echo "deb http://cran.rstudio.com/bin/linux/ubuntu xenial/" | sudo tee -a /etc/apt/sources.list

    Add R to Ubuntu Keyring

gpg --keyserver keyserver.ubuntu.com --recv-key E084DAB9
gpg -a --export E084DAB9 | sudo apt-key add -

    Install R-Base

sudo apt-get update
sudo apt-get install r-base r-base-dev

    Install OpenSSL

sudo apt-get install libcurl4-openssl-dev

    Install RStudio

sudo apt-get install gdebi-core
wget https://download1.rstudio.org/rstudio-1.0.44-amd64.deb
sudo gdebi -n rstudio-1.0.44-amd64.deb
rm rstudio-1.0.44-amd64.deb

Idiosyncrasies of Windows (R programming)

“One of the idiosyncrasies of Windows is that directory creation may report success but create a directory with a different name, for example dir.create(“G.S.”) creates ‘”G.S”’. This is undocumented, and what are the precise circumstances is unknown (and might depend on the version of Windows). Also avoid directory names with a trailing space. Check it out in the R Documentation by typing ?dir.create() in the R console.