简体   繁体   English

如何在 Power BI R Visual 中创建表

[英]How to create a table in a Power BI R Visual

Power BI has a feature that lets you create visuals from R scripts. Power BI 有一项功能可让你从 R 脚本创建视觉对象。 When you add data (columns) to the Values field, it automatically creates a data frame from those columns, which is calls "dataset"当您向 Values 字段添加数据(列)时,它会自动从这些列创建一个数据框,称为“数据集”

It even shows the code it runs: dataset <- data.frame(Col1, Col2, Col3, etc.)它甚至显示了它运行的代码: 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."但它只是返回错误:“没有创建图像。代码没有导致任何视觉效果的创建。确保你的 R 脚本导致 R 默认设备的绘图。”

On the PowerBI website it says: 'Only plots that are plotted to the R default display device are displayed correctly on the canvas'.在 PowerBI 网站上,它说:“只有绘制到 R 默认显示设备的绘图才能在画布上正确显示”。 In simpler terms it means that if an object is printed to the console, it will not be displayed in PowerBI.简单来说,这意味着如果一个对象被打印到控制台,它不会显示在 PowerBI 中。

The tableHTML package let's you create HTML tables that will be displayed in the R default display. tableHTML包可让您创建将在 R 默认显示中显示的 HTML 表格。

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.注意:您需要确保在 PowerBI 使用的 R 库中安装了 tableHTML。 You can see the path for R used by PowerBI in the Global.options under 'R scripting'.您可以在“R 脚本”下的 Global.options 中看到 PowerBI 使用的 R 路径。 Use the path that is displayed there in the code snipped below (this needs to be run from R/RStudio rather that PowerBI):使用在下面截取的代码中显示的路径(这需要从 R/RStudio 而不是 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:如果您在 R 中安装gridExtra包,您应该能够在 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.请记住, grid.table() 需要大量详细的编程来控制图像大小、边距、字体大小等。

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.如果您只是在做一些简单的事情,比如交叉表,那么您应该能够在 PowerBI 中将其作为度量进行计算,然后使用内置的表格或矩阵视觉效果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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