So I'm trying to use the Run R Script in the Query Editor. I'd like to run statistical models using R, however, doing even the simpliest tasks seem a bit difficult.
It seems that the 'dataset' provided automatically is a data.frame, but the columns that have a datetime are converted to factors I was able to verify that doing the following:
# 'dataset' holds the input data for this script output <- dataset output$discovery_date_utc_type <- class(output$discovery_date_utc)
The result is factor, so I did the following to get around that...
output <- as.data.frame(dataset, stringsAsFactors = FALSE)
Then I was able to manipulate the column back into a datetime column doing the following:
# I previously loaded the lubridate package as I was still having issues with standard R date time data types??? It would work in RStudio, but not in PowerBI#library(lubridate) output$discovery_date_utc_new <- ymd_hms(result$discovery_date_utc)
This worked!!!! Yeah The output data frame has a new column and it is a date time column.
Now here is my issue...
I then wanted to get the maximum date and put that into a variable end_date... but I'm getting an error.
end_date <- max(output$discovery_date_utc_new)
Resulting Error (this works fine in RStudio):
DataSource.Error: ADO.NET: R script error. Warning message: In scan(file = file, what = what, sep = sep, quote = quote, dec = dec, : embedded nul(s) found in input Loading required package: methods Attaching package: 'lubridate' The following object is masked from 'package:base': date Error in lapply(list(...), .num_to_date) : object 'result' not found Calls: ymd_hms -> .parse_xxx_hms -> unlist -> lapply Execution halted Details: DataSourceKind=R DataSourcePath=R Message=R script error. Warning message: In scan(file = file, what = what, sep = sep, quote = quote, dec = dec, : embedded nul(s) found in input Loading required package: methods Attaching package: 'lubridate' The following object is masked from 'package:base': date Error in lapply(list(...), .num_to_date) : object 'result' not found Calls: ymd_hms -> .parse_xxx_hms -> unlist -> lapply Execution halted ErrorCode=-2147467259 ExceptionType=Microsoft.PowerBI.Radio.RScriptRuntimeException