Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
RMel
Frequent Visitor

How to use R script that reads from .csv as a data source?

I am trying to use an R script as a data source for Power BI. I am a regular user of R but am new to Power BI.

 

When all the datasets that are imported by the R script are from SQL databases I can import the resulting dataframes from the R script fine, however I have a script that reads from a .csv file that Power BI's R session can't find, which results in the error:

 

 

Error: 'times_of_day_grid.csv' does not exist in current working directory ('C:/Users/MyUserName/RScriptWrapper_ac2d4ec7-a4f6-4977-8713-10494f4b0c4f').

 

 


The .pbix file and the R script are both stored in the same folder as the csv

I have tried manually setting the wd by inserting into the script:

 

 

setwd("C:/Users/MyUserName/Documents/R/Projects/This Project Folder")

 

 


But this just results in the message

 

 

"Connecting - Please wait while we establish a connection to R"

 

 

Which later becomes:

 

 

Unable to connect

We encountered an error while trying to connect.

Details: "ADO.NET: R execution timeout. The script execution was terminated, since it was running for more than 1800000 miliseconds."

 

 

The R script itself takes around a minute to run in RStudio. Any ideas as to how I can edit my script (or the settings in Power BI) to get around this? (I know I can import the csv as a source separately but I don't actually want it as a data source, I just want the resulting dataframe created by the R script).

3 REPLIES 3
Icey
Community Support
Community Support

Hi @RMel ,

 

Is this problem solved?


If it is solved, please always accept the replies making sense as solution to your question so that people who may have the same question can get the solution directly.


If not, please let me know.


Best Regards
Icey

Icey
Community Support
Community Support

Hi @RMel ,

I have tested on my side and it works well.

 

csvfile <- read.csv("D:\\CSVFile.csv")

 

r-csv.gif

 

library(rio)
csvfile <- import("D:\\CSVFile.csv")

 

r-csv-import.gif

Please check limitations: Run R scripts in Power BI Desktop.

  • Only data frames are imported, so make sure the data you want to import to Power BI is represented in a data frame;
  • Columns that are typed as Complex and Vector are not imported, and are replaced with error values in the created table;
  • Values that are N/A are translated to NULL values in Power BI Desktop;
  • Any R script that runs longer than 30 minutes times out;
  • Interactive calls in the R script, such as waiting for user input, halts the script’s execution;
  • When setting the working directory within the R script, you must define a full path to the working directory, rather than a relative path.

 

r-csv-option.PNG

 

Best Regards,

Icey

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

RMel
Frequent Visitor

Hi Icey,

 

Thanks so much for your reply!


I have made sure I use the full working directory for the csv path and there are no interactive parts of the script that require user input. The script takes around 3 minutes to run in RStudio so I doubt it would take over 30 minutes in PowerBI. All of the data I would want to import is in data frames created by the script, and the data types for the columns should all be in factor, character, numeric or POSIXct format.

I had assumed that the problem was due to the csv import rather than the post-import data processing but it seems like that may not be the case, I will try creating a minimal version of the script that just involves importing the CSV by itself and if this works then gradually adding the rest of the processing steps to check where the problem is.


Helpful resources

Announcements
PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.