R: RStudio Plots not showing in the Plots Window

A. Symptom

The plots stopped showing in the pane when running the R codes. The temporary png file generated in the folder didn’t have the graphic output and the file was not able to be deleted.

B. R Code Caused problem

png(file='Construct.png') 
subgroup.analysis.mixed.effects(x = m.hksj, subgroups = madata$Construct, plot=TRUE)
dev.off

In R use png(file=) and dev.off to generate graphic output in the working directory. The subgroup.analysis.mixed.effect function doesn’t work with this method, hence the output graphic file got stuck.

C. Fix

dev.cur()   # to identify output device as "png 4"
dev.off()  # to identify "null device" so the png file in the folder can be deleted;
getOption("device") # to set option to "RStudioGD"

R: Update R version

A. Update R through RGui

  • Open RGui through icon or open RGui from the directory (C:\Program Files\R\R-3.6.1\bin\x64\Rgui.exe)
  • In the Gui, enter the following.
install.packages("installr")
library(installr)
  • Under “install” menu, click “Update R”.
  • Click through the dialogue boxes.

PowerBI and R Integration – Connect SAS dataset

Our database infrastructure is in SAS. Although many data requests can be handled directly through Base SAS, but SAS doesn’t have good visualization capability.

Directly connect PowerBI with SAS raw datasets or analytical result in SAS datasets from PowerBI through R integration can efficiently demonstrate the insights of data.

Here is the R script to connect the SAS dataset to PowerBI.

PowerBI and R Integration – Data Manipulation using R script

Reference: Power BI Introduction: Working with R Scripts in Power BI Desktop — Part 3, by Robert Sheldon.

In previous post I was focusing on using R script to develop visuals in PowerBI directly. In that case, the R scripts that are integrated with the visuals don’t generate datasets for PowerBI, and the R scripts are isolated for each visuals.

The PowerBI and R scripts can also be integrated to get and transform dataset. The caveat is the R scripts can only generate and transform one dataset at a time through Power Query Editor.

Get Data:

  • File / Home Ribbon > Get Data > More … > Other > R Script > Connect
  • The “cars” dataset appears in the “Model” view.
3 examples: cars (simple R script in Source step to GET DATA); cars_mean (R script in Source step and Run R script step); cars_co (more complex R script in Source step)
  • Click “Edit Queries” on the Home Ribbon to get into the “Power Query Editor”
  • Select “Car” from the “Queries” list
  • In the “Query Settings” the “Applied Steps” will show up in the sequece
  • To edit the original R script that gets the dataset, click the * besides the “Source” step (first step) and the “R script” box will appear. If the “Query Settings” is now showing, click “View” ribbon and “Query Settings”.
Source step in Power Query Editor for table cars.

Transform Data:

  • To create another dataset that contains the mean distance by speed group.
  • Repeat the same GET DATA steps above and name the dataset “cars_mean”
  • Get into “Power Query Editor”, select “cars_mean” table.
  • Click “Run R Script” on the “Transform” ribbon and enter the R codes that are showing below.
  • Unless in the sources step, you have to reference the dataset as “dataset” rather than the name of the table, eg “cars_mean”.
Run R script step in the Power Query Editor for table cars_mean
cars_mean table in the Data View.

More Complicated R script for Source step:

  • In the Source step, you can refer the dataset with its name. In this example, it is cars.
  • The R script needs to return a table. If the script only produce a value, you need data.frame() function to convert it to a table.
  • Make sure the required R package have been installed in the R library.
  • Use library() function to attach all the required libraries.
R script in the Source step for table cars_co in Power Query Editor
cars_co in Data View

Advanced Editor:

PowerBI and R Integration – Setup and Visualization Example

Reference: https://docs.microsoft.com/en-us/power-bi/desktop-r-visuals

Notes for setup:

  • In PowerBI: File -> Options and Settings -> Options -> R scripting
  • The libraries required to run R visuals in PowerBI needs to be installed in the R library. The R codes in PowerBI can’t process install.packages(” “) function.
  • Go to c:\program Files\R\R-x.x.x\library folder to check if you already have the required R package installed.
  • In the Options for “R scripting”, click “Change temporary storage location” to point to c:\program Files\R\R-x.x.x\library folder.
Set up R in PowerBI

Note for the PowerBI and R Integration Example:

  • Use “Get Data” to get raw dataset into PoweBI, then the variables will show up in the “Fields” panel.
  • Click “R” in the “Visualizations” panel, then a grey visual placeholder will show up in the body section of the page and the “R script editor” will show up at the bottom of the page.
  • Drag or select variables in the “Fields” panel, the variables will be added to the “Values” section on the “Visualizations” panel, and the variables will be automatically incorporated in a dataframe called “dataset”. You don’t need to do additional code to create the dataframe, and don’t remove the “#” sign from line 1 to line 7 of the code.
  • Starting on line 7, create your own r script.
  • Start with require(” “) and library() to attached the required packaged for this R visual.
  • Filters are interactive with the R visual.
  • Multiple R visuals can be created on the same page and each visual has its own R codes.

R: Neural Network Package

In order to build a predictive model and utilize the neural procedure,  SAS users need to have the SAS Enterprise Miner installed to complete the model comparison and scoring process.

R provides a cheap workaround to the expensive SAS Enterprise Miner soluation.  Install nnet package that provides “feed-forward Neural Networks and Multinomial Log-Linear Models” as an alternative.

install.packages(“nnet”)

library(nnet)