Blog


Coding in R, Part 1

“What Do I Love About R”;
“What Does R Mean to Me”;
“What Do I Want to R When I Grow Up”;
When I started thisĀ “Why I Love R” blog post most of my introductory sentences felt very… generic. So instead, I’m decided to write about why I enjoy coding in R more than I enjoy coding in other programming languages that I’m equally proficient in. For this blog post in particular, I will be using Java as my comparison language, given its readability and popularity.
When I program in Java (or any strongly-typed language for that matter), I feel a strong need -no, a compulsion– for structure, order and a designated skeleton.

PUBLIC CLASS PROGRAMNAME{}.
PUBLIC STATIC VOID MAIN (STRING[] ARGS){}.
FOR (INT I = 0; I < MAX; I++).

It’s very repetitive, very tedious and almost Orwellian in a way: if you stray from the structure, the entire program falls apart. There isn’t much room for freedom of expression when it comes to Java, and while that’s very smart in terms of practicality, it doesn’t leave much leeway for creativity in structure.


Strongly Typed vs Weakly Typed:
While there isn’t a definitive consensus within the coding community about the definitions of strongly- and weakly- typed languages, I have found two ways to comfortably differentiate between them:
Textually:
Does your language:

  • … permit the type of a variable (and, hence, the type of values it references) to change within the lifetime of that variable?
  • … perform checks for type constraint violations before execution rather than during execution?
  • … forbid or require explicit indication for conversions between different types?
  • … prevent disabling or evading the type system on a language-level?
  • … have a complex, fine-grained type system with compound types?
  • … force the type of its data objects to remain the same over the lifetime of the objects?

If the answer is yes to most of these, your language is strongly-typed. Otherwise, weakly-typed it is.
Visually:
Strongly-Typed code ditty: (Java)
(Although Java is a weakly-typed language, I want to express the form in which Java acknowledges types)

 int x = 5;            // x now holds the number 5.
 System.out.println(x) // Output: 5
 x = "Hello";          // This will throw an error because x has been designated an integer and not a String.
 (String) x = "Hello"; // However, Java has the ability to 'cast' an integer to get around the type system.
 System.out.println(x) // Output: Hello

Weakly-Typed code ditty: (R)

 x <- 5               // x now holds the number 5.
 print(x)             // Output: 5
 x <- "Hello"         // x now holds the string Hello.
 print(x)             // Output: Hello
source 1 | source 2 | source 3

  1. Freedom/Fluidity
  2. Multi-Solutioned / Interactabilitiy
  3. Open Source Libraries
  4. RStudio (IDE)
  5. Plots, Graphs, Charts, oh my!

Freedom / Fluidity

“Writing in Java or C is like being on a no-fat or no-carb diet; you’re restricted by what you eat and that’s that.. But writing in R is like being on a no-fat and no-carb diet at the same time, except you get to choose when either apply; ‘Right now I’m on a no-fat diet, time for carbs! Wait, now I’m on a no-carbs diet, eating ALL the fat!’. It’s so flexible!”
When I write in R, I have the ability to structure my code however I feel is most aesthetically pleasing. The only requirement R has in reference to code structure is that functions must be written before they are called, which isn’t an issue in Java.
Take the following R code samples; this program reverses the letters in a given word:

Example 1a:

reverseText = function (word) {
  newWord <- character()
  for (i in nchar(word):1){
    newWord <- paste0(newWord, substr(word, i, i))
  }
  return (newWord)
}
reverseText("hello")
output: olleh
Example 1b:

reverseText("hello")
reverseText = function (word) {
  newWord <- character()
  for (i in nchar(word):1){
    newWord <- paste0(newWord, substr(word, i, i))
  }
  return (newWord)
}
output: Error in eval(expr, envir, enclos) : 
could not find function "reverseText"

When you source an R program, your code will be read from top to bottom and execute in a similar fashion. In Example 1a, the function reverseText() gets read into the system first, so when reverseText(“hello”) gets read in, the program knows what function to execute. The reason Example 1b fails at line 1 is because when the system reads in the line reverseText(“hello”), it is actively searching for the function reverseText(), which does not yet exist in the system: eek!
Take the following Java code samples; this program also reverses the letters in a given word:

Example 2a:

public class sample{
  public static void main(String[] args){
    System.out.println(reverseText("hello"));
  }
 public static String reverseText(String word){
   int max = word.length() - 1;
   String newWord = "";
   for (int i = 0; i <= max; i++){
     newWord += word.charAt(max - i);
   }
   return newWord;
  }
}
output: olleh
Example 2b:

public class sample{
 public static String reverseText(String word){
   int max = word.length() - 1;
   String newWord = "";
   for (int i = 0; i <= max; i++){
     newWord += word.charAt(max - i);
   }
   return newWord;
  }
  public static void main(String[] args){
    System.out.println(reverseText("hello"));
  }
}
output: olleh

To be short and sweet, we see that nothing different happens between the two variations. This is because Java requires you to run your code through a compiler and R doesn’t. This is where the separation between the two types of languages occurs. Java is a language with which you must complete your code, compile, and then run the whole program (which must be written in a particular structure/outline or everything falls apart), where as R allows you to write your code, basically, however you’d like… as long as you write your information from top to bottom.

Multi-Solutioned / Interactivity

In many languages, we are constrained to one function, one library, one method to solve a particular problem. Sure, we can usually restructure how problems are solved, but we tend to end up limited to one or two particular set of functions. In R, if you want to create a scatterplot, you can write any of the following:

plot(table$x, table$y)
with(table, plot(x, y))
ggplot(table, aes(x, y)) + geom_point()
plot_ly(data = table, x = x, y = y, mode = "markers", filename="r-docs/simple-scatter")

Having leeway to perform single tasks in multiple ways makes R a wonderful language because it embraces creativity and personalization in each programmer’s code.
Also, R allows you to interact not only with R itself, but also with a slew of other programming languages like Java, C, Python, SQL, HTML, CSS, etc. This gives developers the opportunity to interweave the strong points of other languages with the free flow of R. For one, if you have the desire to connect to a SQL database through R, you can import the library RODBC and you can basically write in SQL…. in R. The following is a prime example:

# This sets up the connection to the database (re: imaginary credentials)
driver <- "driver={SQL Server};server=server.website.com,12345;database=db;uid=user;pwd=password"
mainConn <- odbcDriverConnect(driver)
# sqlFetch grabs the table with a particular name from the database you're accessing.
names <- sqlFetch(mainConn, "NameTable")
# sqlQuery does exactly that: it runs a query written in SQL
# this function can insert, alter or retrieve elements from a SQL database!
sqlQuery(mainConn, "INSERT INTO NameTable (FirstName, LastName, FavoriteLanguage) VALUES ("Cassie", "Lebauer", "R"))"
sampleData <- sqlQuery(mainConn, "SELECT * FROM NameTable WHERE FirstName = Cassie ORDER BY LastName")

 

Comments ( 0 )

    Leave A Comment

    Your email address will not be published. Required fields are marked *