简体   繁体   English

使用 Power BI 服务中的 R 脚本将 CSV 文件导出到 OneDrive

[英]Export CSV file to OneDrive Using R Scripting in Power BI Service

I am trying to build a fully automated and sustainable reporting tool in Power BI.我正在尝试在 Power BI 中构建一个完全自动化且可持续的报告工具。 I have built a report in Power BI that among other things uses R scripting at one point to create a data export to my local C: drive which is the following code:我在 Power BI 中构建了一份报告,除其他外,它在某一点使用 R 脚本来创建到我本地 C: 驱动器的数据导出,其代码如下:

# 'dataset' holds the input data for this script
.libPaths(.libPaths()[3])
require(gdata)
write.table(trim(dataset), file="C:\\Users\\Username\\OneDrive\\Folder\\Inventory Log.csv", sep=",", row.names=FALSE, append=TRUE, col.names=FALSE)
plot(dataset);

While all my other data is connected to PBI via OneDrive or online sources, this is still connected to my local machine.虽然我的所有其他数据都通过 OneDrive 或在线资源连接到 PBI,但它仍然连接到我的本地机器。 I have personal gateway setup but that still requires my local machine to be physically on during the scheduled refresh on the PBI service.我有个人网关设置,但这仍然需要我的本地计算机在 PBI 服务的计划刷新期间物理打开。

I have tried used the Microsoft365R Package but my R knowledge and experience is still limited so I wasn't able to come up with a solution that would allow file="OneDrive Path" within the write.table() function to successfully execute on Power BI Desktop, let alone Power BI Service.我试过使用 Microsoft365R Package,但我的 R 知识和经验仍然有限,所以我无法想出一个解决方案,允许write.table() function 中的file="OneDrive Path"在 Power 上成功执行BI 桌面,更不用说 Power BI 服务了。

The goal is to fully automate and not require me to have my computer on during the weekends or a non work day.目标是完全自动化,不需要我在周末或非工作日打开电脑。

Is it possible to write a csv to a OneDrive file?是否可以将 csv 写入 OneDrive 文件? If so, what are some ways that have worked successfully?如果是这样,有哪些成功的方法?

Any ideas?有任何想法吗? Thank you for any help you can provide!感谢您提供任何帮助!

Microsoft365R author here. Microsoft365R 作者在这里。 Disclaimer: I'm not familiar with PowerBI, but I assume you can run R scripts inside it and connect to the Inte.net etc.免责声明:我对 PowerBI 不熟悉,但我假设您可以在其中运行 R 脚本并连接到 Inte.net 等。

There's a few things needed to get this to run unattended.要让它在无人值守的情况下运行,需要做一些事情。

A function to upload a data frame as CSV to Onedrive, without requiring you to login, is as follows:一个function上传一个数据框为CSV到Onedrive,不需要你登录,如下:

upload <- function(dataset, upload_path, drive_id, ...)
{
  outfile <- tempfile()
  on.exit(unlink(outfile))
  write.table(dataset, outfile, ...)

  library(Microsoft365R)
  library(AzureGraph)

  gr <- create_graph_login(
    tenant="{yourtenant}",
    app="{client_id}",
    password="{client_secret}",
    auth_type="client_credentials"
  )
  gr$get_drive(drive_id)$upload_file(outfile, upload_path)
}

On the AAD side, create an app registration and give it a client secret (password).在 AAD 端,创建应用程序注册并为其提供客户端密码(密码)。 You then give it the Microsoft Graph application permissions necessary to read drives--most likely "Files.Readwrite.All" .然后,您授予它读取驱动器所需的 Microsoft Graph 应用程序权限——很可能是"Files.Readwrite.All"

Note down the client ID and client secret for your app registration.记下您的应用程序注册的客户端 ID 和客户端密码。 These are the values you plug into the R function above.这些是您插入上面 R function 的值。

You can get your drive ID with the following R code.您可以使用以下 R 代码获取您的驱动器 ID。

drv <- get_business_onedrive(tenant="yourtenant")
drv$properties$id

You'll probably need the help of your friendly local admin to get all this done, if only because most orgs lock down the ability to register apps.您可能需要友好的本地管理员的帮助才能完成所有这些工作,即使仅仅是因为大多数组织都锁定了注册应用程序的能力。

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

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