r - 如何将 R 数据框导出到 Power Query 表

标签 r powerbi powerquery

我在 Power Query 中使用 R 脚本来执行一些数据转换并返回一个缩放表。
我的R代码是这样的:

# 'dataset' 

最佳答案

这似乎没有返回似乎很奇怪。在线快速浏览给 this 3 minute youtube video ,它使用与您正在使用的方法相同的方法。进一步搜索来源,可能会遇到 Microsoft Documentation ,这给出了可能存在问题的可能原因。

When preparing and running an R script in Power BI Desktop, there are a few limitations:

  • 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


这些似乎是最明显的原因。打赌你的数据集中没有复杂的列,我相信先验可能是原因。快速重建您的数据集显示 scale函数将您的数据集更改为 matrix类对象。这是由 cbind 保留的,因此输出属于 matrix 类而不是 data.frame .
>dataset <- as.data.frame(abs(matrix(rnorm(1000),ncol=4)))
>class(dataset)
[1]"data.frame"
>library(dplyr)
>df_normal <- log(dataset + 1) %>%
>    select(c(2:4)) %>%
>    scale 
>class(df_normal)
[1] "matrix"
>df_normal <- cbind(dataset[,1], df_normal)
>output <- df_normal
>class(output)
[1] "matrix"
然后一个简单的修复似乎是添加 output <- as.data.frame(output) ,因为这符合 powerBI 的文档。也许它需要一个 return就像最后的声明。在脚本末尾添加一行简单地说明 output应该解决这个问题。
编辑
为了澄清起见,我相信以下编辑过的脚本(你的)应该返回预期的数据
# 'dataset' contém os dados de entrada neste script

library(dplyr)

df_normal <- log(dataset+1) %>%
select(c(2:4)) %>%
scale
df_normal <-cbind(dataset[,c(1)], df_normal)
output <- as.data.frame(df_normal)
#output  ##This line might be needed without the first comment 

关于r - 如何将 R 数据框导出到 Power Query 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56046725/

相关文章:

error-handling - Power BI-处理不存在的参数并为有效参数返回表中的数据

r - 在data.table中按组cbind列表的有效方法

R:将列表中每个数据框的行重新组织到新数据框的新列表中

向量中每个元素的 R grep

ssas - Dax 如何从列中获取不同的值

excel - Power Query - M语言: Sum with Group By for multiple columns

r - 循环多个表并仅在满足条件时才保留的方法?

azure - 通过 PowerBi 虚拟数据网关访问 Databricks

Excel 公式转 DAX : How to Reference Previous Row

excel - 按分隔符幂查询的 X 实例拆分行