Rython tips and tricks – Clipboard

[This article was first published on R – Eran Raviv, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

For whatever reason, clipboard functionalities from Rython are under-utilized. One utility function for reversing backslashes is found here. This post demonstrates how you can use the clipboard to circumvent saving and loading files. It’s convenient for when you just want the quick insight or visual, rather than a full-blown replicable process.

Consider the following scenario: you come across a webpage containing a table, and would like to compute some basic statistics, or visualize a couple of columns from that table. A usual workflow for such a minuscule task is to (1) copy the table, (2) pasting it into Excel, (3) saving the Excel sheet, and (4) reading it into Rython.

Can we not just copy the table and read it directly from the clipboard? Yes we can!

Like so (click to play, and the Rython code below):


The code is below:

In R

tmpdat <- read.csv("clipboard", sep= "",  row.names= NULL, fill= T, header= F)
dim(tmpdat) ; head(tmpdat)

plot(tmpdat[,13]~ seq(NROW(tmpdat)) , 
     ty= "b", col= "darkgreen", xlab="Time", 
     ylab="CHF (millions)", pch=19, 
     las=1)
grid()

In Python

import pandas as pd
import matplotlib.pyplot as plt

tmpdat = pd.read_clipboard(sep= " ")

print(tmpdat.shape) ; print(tmpdat.head())
fig= plt.figure(figsize=(10, 6))
fig.subplots_adjust(left=0.15) 
plt.plot(tmpdat.iloc[:, 12], marker='o', color='darkgreen')
plt.xlabel('Time')
plt.ylabel('CHF (millions)')
plt.grid(True)
plt.show()

To leave a comment for the author, please follow the link and comment on their blog: R – Eran Raviv.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)