【问题标题】:R how do I switch off JSON outgoing/incoming header messages?R 如何关闭 JSON 传出/传入标头消息?
【发布时间】:2020-08-07 12:47:25
【问题描述】:

我在 Microsoft PowerBi 中使用 R 脚本作为数据源时遇到问题。我认为这是 PowerBi 的根本问题,但短期内我需要在 R 中找到解决方案。

基本上,如果我使用 R Studio,PowerBi 似乎无法处理将发送到控制台的消息。

在 R 脚本中,我使用 REST API 从 URL 请求数据。接收到的 JSON 消息被转换为 R 数据帧。在 PowerBi 中使用脚本作为数据源时,这仅在我将详细设置设置为 FALSE 时才有效,即如果我使用 R Studio,则不会向控制台发送任何消息(特别是输入的数据)。

response <- GET(<url>,
                body = list(),
                add_headers(.headers = c('<identity token>' = ID_to_use)),
                verbose(data_out = FALSE,
                        data_in = FALSE,
                        info = FALSE,
                        ssl = FALSE),
                encode = "json")

但是,我没有关闭传入/传出 JSON 标头消息的选项(这将回来咬人!)。

<<  {"identity":" <token>"}
*  Connection #54 to <host> left intact
No encoding supplied: defaulting to UTF-8.
-> GET <URL request> HTTP/1.1
-> Host: <host>
-> User-Agent: libcurl/7.64.1 r-curl/4.3 httr/1.4.1
-> Accept-Encoding: deflate, gzip
-> Accept: application/json, text/xml, application/xml, */*
-> <Identity>: <Identity>
-> 
<- HTTP/1.1 200 OK
<- X-Session-Expiry: 3599
<- Content-Type: application/json
<- Transfer-Encoding: chunked
<- Date: Thu, 06 Aug 2020 16:14:26 GMT
<- Server: <Server>
<- 
No encoding supplied: defaulting to UTF-8.
No encoding supplied: defaulting to UTF-8.
No encoding supplied: defaulting to UTF-8.

来自 R 帮助

.
.    
verbose() uses the following prefixes to distinguish between different components of the http messages:
    * informative curl messages
    -> headers sent (out)
    >> data sent (out)
    *> ssl data sent (out)
    <- headers received (in)
    << data received (in)
    <* ssl data received (in)
.
.

将详细设置切换为 FALSE 适用于单个请求,但是,我需要将请求置于循环中并继续请求更多数据,直到 API 网关指示不再接收数据。在脚本中发送/接收五个或更多请求/回复时,PowerBi 似乎失败。

仅从观察来看,我认为这与 JSON Header 消息堆积有关。

我尝试了多种方法,但似乎没有任何效果:sink('NUL')、invisible()、capture.output()。

任何帮助将不胜感激。

【问题讨论】:

    标签: r json powerbi


    【解决方案1】:

    我找到了一个 hacky 解决方案,它至少解决了我在 R 中遇到的问题,但在 PowerBi 中没有。

    通过编写一个“包装器”R 脚本(见下文),它使用 shell 命令调用我的主脚本 THE_SCRIPT.R。 THE_SCRIPT 转储出一个 CSV 文件,然后我在包装脚本中读取该文件:

    #Required by PowerBi
    library(mice)
    
    #set the directory, between R and the shell it's a pain to deal with spaces in the directories and quotes
    setwd("C:/Program Files/R/R-3.6.2/bin/")
    system("Rscript.exe C:\\Users\\<USER>\\Documents\\THE_SCRIPT.R > Nul 2>&1")
    
    A_DATA_TABLE <- read.csv("C:\\Users\\<USER>\\Documents\\THE_FILE.csv")
    

    但是,这仍然没有解决在 PowerBi 中运行时的问题。

    注意,我在 R 中尝试了 sink('Nul 2>&1'),没有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-18
      • 1970-01-01
      • 2017-04-09
      • 1970-01-01
      • 2021-03-21
      相关资源
      最近更新 更多