DEV Community

Cover image for R and Visual Studio Code
Mike Conner
Mike Conner

Posted on

R and Visual Studio Code

As I've mentioned several times before, I'm learning how to code in a program called Operation Spark. What I haven't mentioned before is what I did before I started Operation Spark. It turns out.... I coded! Sort of. A little. Occasionally. See, in a past life, I used a program called R. RStudio, to be exact. The point is that R is itself a programming language, and its mainly used for statistical computing. Imagine my surprise when worlds collided and I learned that there was an extension that allowed you to execute R code in VS Code. Today, I'm going to walk through the process of installing R and performing some simple statistical analysis.

Getting Started

Set-up is relatively simple. To start, you need to install R. I'm on Windows, so I run VS Code through an Ubuntu shell. Installing R is as simple as opening your terminal and running the following commands:

sudo apt-get update
sudo apt-get install r-base
Enter fullscreen mode Exit fullscreen mode

After that, lets navigate to our extension window and run a search for R. The first package to come up should be the one we're looking for, simply titled "R", with a description of "R language support." Install that and you should be good to go. From there, we can create a new file with the .r file type. Lets do that and move on!

Running code

So remember, R is a completely different language. I have no idea if it's similar to anything out there, but I do know that it does NOT look like JavaScript. So lets go ahead and create a function, then tell R to print the results of that function to the terminal:

add <- function(x, y) {
  x + y
}

print(add(6, 10))
Enter fullscreen mode Exit fullscreen mode

To see the results of our code, we need to run it. There should be a button at the top of your window that looks like this:
Alt Text
Hit that button to see the result of our function in the terminal. Alternatively, you could hit control + enter from anywhere in the code to execute it immediately. So this is pretty cool, if you're into statistics and feel like learning an entirely new language! Unfortunately, at the time of writing, I haven't found a simple way to implement R code into an already-existing JavaScript file and database. But what I have found, because the internet is a beautiful place, is an API that lets you query existing R libraries with your own data!

OpenCPU API

This amazing API is called openCPU and you should definitely read more about it here. In the meantime, lets just make a few simple calls to one of the many routes available to us. Say you needed ten random numbers. There's an easy way to do that in JavaScript, right? What if you need those 10 numbers to follow a normal distribution, assuming a mean of 100 and a standard deviation of 10. That's.... a little more complicated. Luckily, theres a route that can do that for you.

const getArray = (n, mean, sd) => {
  const params = { n, mean, sd }
  return axios.post(`https://cloud.opencpu.org/ocpu/library/stats/R/rnorm/json`, params)
    .then(({data}) => data);
}
Enter fullscreen mode Exit fullscreen mode

The above function will return the desired array! If you're thinking about implementing this, be aware that these aren't truly random numbers, and requests with the same parameters will always yield the same results. This has to do with how R handles seeds, but thats a topic for another day. The cool thing here is that we're offloading data from our app, be it a database or user input or anything of that nature, to be analyzed in a legitimate statistical analysis package!
Lets look at an example applied to some information we might have stored in a database. Say we have test information for two schools stored somewhere and we'd like to know if the difference between these scores is statistically significant. We could perform a t-test on the means of the two schools' test scores and analyze the output. First, we'd need to make a request to our database to get back the scores as two different arrays, say "schoolOne" and "schoolTwo". From there, we can simply query the api as follows:

const getPValue = (schoolOne, schoolTwo) => {
  const params = { x: schoolOne, y: schoolTwo }
  return axios.post(`https://cloud.opencpu.org/ocpu/library/stats/R/t.test/print`, params)
    .then(({data}) => data);
}
Enter fullscreen mode Exit fullscreen mode

This is going to return a string of something you might not be that familiar with:
Alt Text
For our purposes, the only thing we're worried about is the p-value. In this example, the p-value is less than 0.05, indicating that there is a difference in means between the two schools. You can add more parameters to test for the directionality of this difference as well.

Conclusion

R is a powerful statistical analysis tool. Using specific API calls, you can perform analysis on data stored in a database on your server. There are literally hundreds (maybe even thousands) of other uses for this tool, as R is an open-source software and is continually being developed.

Top comments (1)

Collapse
 
jamesfthomas profile image
James F. Thomas

You gonna write one on SPSS next?

Man, I remember that program and trying to move all my subject data points over to an excel spreadsheet, whoooooooo doggy this article brought back memories of hours in a lab next to a treadmill staring at the vo2 readings each minute.

Very good to know that VScode has an extension that will allow me to perform statistics.

Great read thanks for taking the time to write it.