R Programming using RTVS – Part 4

This article assumes that you have RTVS extension added to your VS IDE.  You can refer my previous blogs on installation & configuration of RTVS.

  1. R Support in Visual Studio (RTVS)
  2. R Programming using RTVS – Part 2
  3. R Programming using RTVS – Part 3
Prerequisites:
1. Basic knowledge of R programming
2. RTVS Extension

Creating R projects using RTVS

Create a project in visual studio using R template.

RTVS_2

Open the Script.R file from solution explorer and type in your R program. In the example below, we create a simple line graph that compares sales and profit performance of a company over a period of 5 years.

#Define Data
sales <- c(2000, 3500, 3000, 4700, 6000)
profit <- c(1450, 2600, 750, 2000, 3750)
#range for axis
rnge <- range(0, sales, profit)
#Plot sales values
plot(sales, type = "o", col = "green", ylim = rnge, axes = FALSE, ann = FALSE)
#draw profit values as line
lines(profit, type = "o", pch = 22, lty = 2, col = "red")
box()
#draw X axis and y axis
axis(1, at = 1:5, lab = c("2013", "2014", "2015", "2016", "2017"))
axis(2, las = 1, at = 1000 * 0:rnge[2])
#Graph Title
title(main = "Sales by Year", col.main = "blue", font.main = 4)
#add axis labels
title(xlab = "Year", col.lab = rgb(0, 0, 1))
title(ylab = "Amount", col.lab = rgb(0, 0, 1))

Place your cursor in the first line of code and use Ctrl+Enter to step through each line to R Interactive and execute. You can see each line execution results to the R Plot window as shown below

RTVS_5_R_prioject

Here is the final plot view

RTVS_5_R_prioject_plot

Given below is an updated source which generate the output as PDF file

#Define Data
sales <- c(2000, 3500, 3000, 4700, 6000)
profit <- c(1450, 2600, 750, 2000, 3750)

#File name to save output
pdf(file = "sales_and_profit_byYr.pdf", height = 4, width = 5)

#range for axis
rnge <- range(0, sales, profit)

#Plot sales values
plot(sales, type = "o", col = "green", ylim = rnge, axes = FALSE, ann = FALSE)
#draw profit values as line
lines(profit, type = "o", pch = 22, lty = 2, col = "red")
box()

#draw X axis and y axis
axis(1, at = 1:5, lab = c("2013", "2014", "2015", "2016", "2017"))
axis(2, las = 1, at = 1000 * 0:rnge[2])

#Graph Title
title(main = "Sales by Year", col.main = "blue", font.main = 4)

#add axis labels
title(xlab = "Year", col.lab = rgb(0, 0, 1))
title(ylab = "Amount", col.lab = rgb(0, 0, 1))

#Flush the output to PDF file
dev.off()

Save the date for Microsoft Azure OpenDev virtual event

Capture

Azure OpenDev, the first-ever virtual event from Microsoft that showcases open source technologies in the cloud.

Hosted by John Gossman, Lead Architect, Microsoft Azure, the event features industry thought leaders including Mark Shuttleworth of Canonical and speakers from Docker, Pivotal, Red Hat, and Chef—as well as Microsoft teams dedicated to open source. Hear from customers such as Mastercard about solutions they’ve built with open source that run on Azure today.

Join online to learn how to:

  • Use containers to build microservice-based solutions on Azure, in Java, Node.js, and more.
  • Use existing open source skills and tools to enable your DevOps pipeline in the cloud.
  • Modernize your existing apps and implement community best practices.

Have your questions answered by subject matter experts in a live Q&A, and participate in hands-on sessions with open source on Azure.

Save the date

R Programming using RTVS – Part 3

This article assumes that you have RTVS extension added to your VS IDE.  You can refer my previous blogs on installation & configuration of RTVS.

  1. R Support in Visual Studio (RTVS)
  2. R Programming using RTVS – Part 2
Prerequisites:
1. Basic knowledge of R programming
2. RTVS Extension

Key RTVS features that a developer should be aware of :

The following is just a list for you to explore rather than extensive walk through.

  1. Step through :  use Ctrl+Enter
  2. Execute a block of code in R interactive  : Select a block of code and Ctrl+ Enter (or right click and choose Execute in Interactive)
  3. Debug : Use Debug > Source startup file or F5
  4. Set Debug points : Native VS behavior (refer image below)rtvs_5_r_debug.png
  5. Reset current work space : To clear everything by using R Tools > Session > Reset
  6. Examine variables : Use ‘variable explorer’ (Ctrl + 8)
  7. IntelliSense : RTVS_5_R_IntelliSense
  8. Help : Use ? and ?? (Use ?? to search term in quotes if it include spaces)
  9. Code snippet manager :  View them at Tools > Code Snippets Manager.  RTVS_5_R_CodeSnipptType the abbreviated name and use tab to insert a snippet to your codeRTVS_5_R_CodeSnippt1
  10.  R interactive window or REPL (Read-Evaluate-Print-Loop) window : Use already illustrated in previous steps/posts
  11. Plot window : Ctrl + 6
  12. Plot history : Preserves all your plots in the session.  R Tools > Plots > Plot History Window (refer image below)RTVS_5_R_Plot_history

There are many more which an R programmer or VS developer can explore themselves. We will go through some samples using RTVS in the upcoming posts.

R Programming using RTVS – Part 2

This article assumes that you have RTVS extension added to your VS IDE.  You can refer my previous blog on installation & configuration of RTVS.

Prerequisites:
1. Basic knowledge of R programming
2. RTVS Extension

Once you install ‘Data Science and Analytical Applications’ workload using VS2017 installer (May 2017 update), new R Project template will be available for creating new project.

RTVS_2

Those who are familiar with R Studio will certainly find themselves home with this IDE. Additionally we have all VS native features. Given below is a view that shows Solution Explorer, Script file (Script.R) and Interactive Window (Microsoft R Client which is equivalent to console in R Studio)

RTVS_3

Notice that there is an ‘R Plot’ tab which is where the plots/visualizations will be generated.

To start with lets draw a basic plot

ds <- c(1,500,300,100,350)
plot(ds)

RTVS_4.png

Alternately use the normal VS debug (starting new instance) and watch the interactive window as the plot is generated.

ds <- c(1,500,300,100,350) > plot(ds)
> rtvs::debug_source("~/visual studio 2017/Projects/rproject1/rproject1/script.R")
Sourcing: c:\users\geek2\documents\visual studio 2017\Projects\rproject1\rproject1\script.R
>

That’s basic start. I recommend R developers to explore the VS menu “R Tools” for the complete set of R integration options available.