简体   繁体   中英

How to create a table in a Power BI R Visual

Power BI has a feature that lets you create visuals from R scripts. When you add data (columns) to the Values field, it automatically creates a data frame from those columns, which is calls "dataset"

It even shows the code it runs: dataset <- data.frame(Col1, Col2, Col3, etc.)

My question is, how could I go about viewing the data in this dataframe?

I've tried running code like:

g <- xtabs(dataset)
g
print(g)

but it just returns the error: "No image was created. The code didn't result in creation of any visuals. Make sure your R script results in a plot to the R default device."

On the PowerBI website it says: 'Only plots that are plotted to the R default display device are displayed correctly on the canvas'. In simpler terms it means that if an object is printed to the console, it will not be displayed in PowerBI.

The tableHTML package let's you create HTML tables that will be displayed in the R default display.

library (tableHTML)
g <- tableHTML(dataset, rownames = FALSE)
print(g)

Note: you need to make sure tableHTML is installed in the library of R that is used by PowerBI. You can see the path for R used by PowerBI in the Global.options under 'R scripting'. Use the path that is displayed there in the code snipped below (this needs to be run from R/RStudio rather that PowerBI):

install.packages('tableHTML','/path/to/R/R-x.x.x/library)

You need to use a function that turns the table into a visual. If you install the gridExtra package in R, you should be able to do this in PowerBI:

g <- xtabs(dataset)
gridExtra::grid.table(dataset)

Bear in mind, the grid.table() requires a lot of detailed programming to control the image size, margins, font size, etc.

If you're just doing something simple like a crosstab, that's something you should be able to calculate as a Measure in PowerBI, and then use the built in table or matrix visuals.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM