cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
nshetty Visitor
Visitor

R script and R visuals in Power BI

I am modeling a R script visual on power BI.

 

When I run the following query on R, it runs succesfully. When I run it on the power BI visual I get an error.

 

Error in (function(...): object 'rScriptWrapper_fileNum' not found

Calls: plot ... plot -> plot.ts -> plotts -> <Anonymous>

Execution Halted

 

 

# Input load. Please do not change #
`dataset` = read.csv('C:/Users/nshetty/REditorWrapper_1ef0340a-90b2-42e8-9e20-3d239e50b122/input_df_f7f4547c-b225-42c1-9133-b9bf16c09d85.csv', check.names = FALSE, encoding = "UTF-8", blank.lines.skip = FALSE);
# Original Script. Please update your script content here and once completed copy below section back to the original editing window #

#############https://community.powerbi.com/t5/R-Script-Showcase/Forecasting/td-p/58485

library(plyr)
library(dplyr)
library(tidyr)
library(ggplot2)
library(scales)
library(RODBC)
library(readr)
library(plotly)
library(readxl)

# Input load. Please do not change #
rm(list= ls())
dataset = read.csv("C:/Users/nshetty/Desktop/BA/Requests/Efrain's Requests/5 Year Strategic Plan/data.csv")
# `dataset` = read.csv('C:/Users/nshetty/REditorWrapper_3c2cc288-4dc2-487c-8a95-f650ee1d5b9f/input_df_8a06638e-93da-44e9-93c1-01a94f2ad8c9.csv', check.names = FALSE, encoding = "UTF-8", blank.lines.skip = FALSE);
#
# REFERENCES: https://stat.ethz.ch/R-manual/R-devel/library/stats/html/loess.html

# 
# #PBI_EXAMPLE_DATASET for debugging purposes 
# 
# 
# if(!exists("dataset"))
# {
# sample=USAccDeaths
# dates=unclass(as.POSIXlt(as.Date(sample),tz="UTC"))
# dataset=data.frame(Year=dates$year+1900,Quarter=rep("Q",length(sample)),Month=month.name[dates$mon+1],Day=dates$mday, Value=as.numeric(sample))
# }



############ User Parameters #########
##PBI_PARAM: Should warnings text be displayed?
#Type:logical, Default:TRUE, Range:NA, PossibleValues:NA, Remarks: NA
showWarnings=TRUE

##PBI_PARAM: Should additional info about the forcasting method be displayed?
#Type:logical, Default:TRUE, Range:NA, PossibleValues:NA, Remarks: NA
showInfo=TRUE

##PBI_PARAM: Forecast length
#Type:integer, Default:NULL, Range:NA, PossibleValues:NA, Remarks: NULL means choose forecast length automatically
forecastLength=NULL

##PBI_PARAM Error type
#Type: string, Default:"Automatic", Range:NA, PossibleValues:"Automatic","Multiplicative","Additive"
errorType = "Automatic"

##PBI_PARAM Trend type
#Type: string, Default:"Automatic", Range:NA, PossibleValues:"Automatic","Multiplicative","Additive","None"
trendType = "Automatic"

##PBI_PARAM Season type
#Type: string, Default:"Automatic", Range:NA, PossibleValues:"Automatic","Multiplicative","Additive","None"
seasonType = "Automatic"

##PBI_PARAM Confidence level band display
#Type:logical, Default:TRUE, Range:NA, PossibleValues:NA, Remarks: NA
drawConfidenceLevels = FALSE


###############Library Declarations###############


libraryRequireInstall = function(packageName, ...)
{
if(!require(packageName, character.only = TRUE)) 
warning( paste ("*** Failed to install '", packageName, "' ***", sep = ""))
}

#ets
libraryRequireInstall("graphics")
libraryRequireInstall("scales")
libraryRequireInstall("forecast")
libraryRequireInstall("zoo")
library(ggplot2)
###############Internal parameters definitions#################
#PBI_PARAM Minimal number of points
#Type:integer, Default:5, Range:[0,], PossibleValues:NA, Remarks: NA
minPoints = 5

##PBI_PARAM Color of time series line
#Type:string, Default:"orange", Range:NA, PossibleValues:"orange","blue","green","black"
pointsCol = "black"

##PBI_PARAM Color of forecast line
#Type:string, Default:"red", Range:NA, PossibleValues:"red","blue","green","black"
forecastCol = "blue"

#PBI_PARAM Transparency of scatterplot points
#Type:numeric, Default:0.4, Range:[0,1], PossibleValues:NA, Remarks: NA
transparency = 0.4

#PBI_PARAM Shaded band for confidence interval
#Type:logical, Default:TRUE, Range:NA, PossibleValues:NA, Remarks: NA
fillConfidenceLevels=TRUE

#PBI_PARAM damping
#Type:logical, Default: NULL, Remarks: NULL selects damped or undamped trend depending on which fits better
damped = NULL

#PBI_PARAM Size of points on the plot
#Type:numeric, Default: 1 , Range:[0.1,5], PossibleValues:NA, Remarks: NA
pointCex = 1

#PBI_PARAM Size of subtitle on the plot
#Type:numeric, Default: 0.5 , Range:[0.1,5], PossibleValues:NA, Remarks: NA
cexSub = 0.75

###############Internal functions definitions#################


###############Upfront input correctness validations (where possible)#################


N=nrow(dataset)

labTime = "Time"
labValue=names(dataset)[ncol(dataset)]

dataset = dataset[,!colnames(dataset) %in% "Quarter"]
colnames(dataset) <- c("Year","Month","PosixDate","Value")
dataset <-dataset %>% select(Year, Month, Value, PosixDate)
dataset = dataset[order(dataset$PosixDate),]

start=2010

end=2017
frequency = 12

timeSeries=ts(start=2010,end=2017,frequency = 12,
data=dataset$Value)

 

dataset<-dataset[complete.cases(dataset),] #remove corrupted rows


##############Main Visualization script###########
pbiWarning = NULL
pbiInfo = NULL

if(length(timeSeries)>=minPoints) {

ets_params = list(Automatic="Z",Multiplicative="M",Additive="A",None="N")

fit = ets(timeSeries, 
model=paste(ets_params[[errorType]],ets_params[[trendType]],ets_params[[seasonType]],sep=""), 
damped=damped)

forecastLength = 119
if (is.null(forecastLength))
prediction = forecast(fit)
else
prediction = forecast(fit, h=forecastLength)
lastValue = tail(prediction$x,1)
prediction$mean=ts(c(lastValue,prediction$mean), 
frequency = frequency(prediction$mean), 
end=end(prediction$mean))

prediction$upper=ts(rbind(c(lastValue,lastValue),prediction$upper), 
frequency = frequency(prediction$upper), 
end=end(prediction$upper))

prediction$lower=ts(rbind(c(lastValue,lastValue),prediction$lower), 
frequency = frequency(prediction$lower), 
end=end(prediction$lower))


plot(prediction, lwd=2, col=pointsCol, fcol=forecastCol, flwd=2,shaded=fillConfidenceLevels)
if(showInfo)
pbiInfo=paste(pbiInfo,"Forecasts from ", fit$method, sep="")

plot(prediction, lwd=2, col=pointsCol, fcol=forecastCol, flwd=2, shaded=fillConfidenceLevels, 
main = "", sub = pbiInfo, col.sub = "gray60",cex.sub=cexSub, xlab = labTime, ylab = labValue)

} else{ #empty plot
plot.new()
pbiWarning<-paste(pbiWarning, "Not enough data points", sep="\n")
}
#add warning as subtitle
if(showWarnings)
title(main=NULL, sub=pbiWarning,outer=FALSE, col.sub = "gray60", cex.sub=cexSub)


remove("dataset")

 

 

1 REPLY 1
v-ljerr-msft Super Contributor
Super Contributor

Re: R script and R visuals in Power BI

Hi @nshetty,

 

Could you also share a sample csv file(with just some mock data), which could help us reproduce the issue? So that we can better assist on it. You can upload it to OneDrive or Dropbox and post the link here. Do mask sensitive data before uploading.

Smiley Happy

 

Regards

Helpful resources

Announcements
Back to School Contest

Back to School Contest

Engage and empower students with Power BI!

MBAS Gallery

Watch Sessions On Demand!

Continue your learning in our online communities.

Summit Australia 2019

Summit Australia 2019

Travel to Melbourne and network with thousands of peers!

PBI Community Highlights

PBI Community Highlights

Check out what's new in the Power BI Community!

Top Ideas
Users Online
Currently online: 230 members 2,822 guests
Please welcome our newest community members: