在 R 中使用带有 URL 的 jsonlite 的 fromJSON() 时遇到问题 [英] Trouble using jsonlite's fromJSON() with URL in R

查看:34
本文介绍了在 R 中使用带有 URL 的 jsonlite 的 fromJSON() 时遇到问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 URL,它以 JSON 格式显示来自某个网​​站的 API 的内容(文件 ID).要以编程方式执行此操作,我使用 jsonlite 的 fromJSON(txt) 函数 包,然后将 JSON 解析为向量(或列表,不确定).

I have a URL that displays content (file IDs) from a certain website's API, in JSON format. To do this programmatically, I use the fromJSON(txt) function of the jsonlite package, which then parses the JSON into a vector (or list, not sure).

这在我的家用电脑上完美运行.但是,当我在工作中运行相同的代码时,似乎 fromJSON(txt) 没有识别 URL 而是尝试解析实际的 URL 文本,因为我收到以下错误:

This works perfectly on my home computer. However, when I run the same identical code at work, it seems that the fromJSON(txt) doesn't identify the URL as such and rather tries to parse the actual URL text, since I get the following error:

 Error: lexical error: invalid char in json text.
                                       https://api.gdc.cancer.gov/file
                     (right here) ------^

我已经多次检查并重新检查了我的代码和 URL.将 URL 粘贴到浏览器并返回 JSON 格式的文本时,该 URL 可以完美运行.

I have checked and rechecked my code and the URL numerous times. The URL works perfectly when pasted into a browser and returns JSON formatted text.

我尝试了几种替代方法,例如 jsonlite 包的 unserializeJSON() 和 RJSONIO 包的 fromJSON(),后者产生不同的错误.

I have tried several alternatives, such as the unserializeJSON() of jsonlite package and fromJSON() of the RJSONIO package, the latter of which produces a different error.

如果您能帮助我找出问题所在,我将不胜感激...

I would appreciate any help in working out what is wrong...

这是我的代码的相关部分:

Here is the relevant part of my code:

# The URL (works fine in a browser):
urlForIDs <- "https://api.gdc.cancer.gov/files?filters=%7B%22op%22%3A%22and%22%2C%22content%22%3A%5B%7B%0A%20%20%20%20%22op%22%3A%20%22and%22%2C%0A%20%20%20%20%22content%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22op%22%3A%20%22in%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22content%22%3A%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22field%22%3A%20%22cases.project.program.name%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22value%22%3A%20%22TCGA%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22op%22%3A%20%22and%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22content%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22op%22%3A%20%22in%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22content%22%3A%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22field%22%3A%20%22cases.project.disease_type%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22value%22%3A%20%22%2ACarcinoma%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22op%22%3A%20%22in%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22content%22%3A%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22field%22%3A%20%22cases.project.primary_site%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22value%22%3A%20%22Breast%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%5D%0A%7D%0A%2C%7B%22op%22%3A%22and%22%2C%22content%22%3A%5B%7B%22op%22%3A%22in%22%2C%22content%22%3A%7B%22field%22%3A%22type%22%2C%22value%22%3A%22copy_number_segment%22%7D%7D%2C%7B%22op%22%3A%22in%22%2C%22content%22%3A%7B%22field%22%3A%22data_category%22%2C%22value%22%3A%22Copy%20Number%20Variation%22%7D%7D%2C%7B%22op%22%3A%22in%22%2C%22content%22%3A%7B%22field%22%3A%22data_type%22%2C%22value%22%3A%22Masked%20Copy%20Number%20Segment%22%7D%7D%2C%7B%22op%22%3A%22in%22%2C%22content%22%3A%7B%22field%22%3A%22experimental_strategy%22%2C%22value%22%3A%22Genotyping%20Array%22%7D%7D%5D%7D%5D%7D&fields=file_id&size=5000&related_files=false"

# Another URL which I tried, that does the same thing, but when creating this one I minimised the JSON (removed white spaces) before encoding it:
# The first one worked on Chrome browser but not in Explorer, this one does work in Explorer, but not in the fromJSON() function:
url2 <- "https://api.gdc.cancer.gov/files?filters=%7B%22op%22%3A%22and%22%2C%22content%22%3A%5B%7B%22op%22%3A%22and%22%2C%22content%22%3A%5B%7B%22op%22%3A%22in%22%2C%22content%22%3A%7B%22field%22%3A%22cases.project.program.name%22%2C%22value%22%3A%22TCGA%22%7D%7D%2C%7B%22op%22%3A%22and%22%2C%22content%22%3A%5B%7B%22op%22%3A%22in%22%2C%22content%22%3A%7B%22field%22%3A%22cases.project.disease_type%22%2C%22value%22%3A%22%2ACarcinoma%22%7D%7D%2C%7B%22op%22%3A%22in%22%2C%22content%22%3A%7B%22field%22%3A%22cases.project.primary_site%22%2C%22value%22%3A%22Breast%22%7D%7D%5D%7D%5D%7D%2C%7B%22op%22%3A%22and%22%2C%22content%22%3A%5B%7B%22op%22%3A%22in%22%2C%22content%22%3A%7B%22field%22%3A%22type%22%2C%22value%22%3A%22copy_number_segment%22%7D%7D%2C%7B%22op%22%3A%22in%22%2C%22content%22%3A%7B%22field%22%3A%22data_category%22%2C%22value%22%3A%22Copy%20Number%20Variation%22%7D%7D%2C%7B%22op%22%3A%22in%22%2C%22content%22%3A%7B%22field%22%3A%22data_type%22%2C%22value%22%3A%22Masked%20Copy%20Number%20Segment%22%7D%7D%2C%7B%22op%22%3A%22in%22%2C%22content%22%3A%7B%22field%22%3A%22experimental_strategy%22%2C%22value%22%3A%22Genotyping%20Array%22%7D%7D%5D%7D%5D%7D&fields=file_id&size=5000&related_files=false"

fileIDs <- fromJSON(urlForIDs) # I have tried changing parameters, such as 'simplifyVector = FALSE' but nothing seems to work.

# The following line is not executed, since the error happens before
fileIDs$data$hits$file_id

也许最奇怪的是,复制粘贴的相同代码在我的家用电脑上运行良好.

Perhaps the strangest thing is that the identical code, copied and pasted, worked fine on my home computer.

提前致谢.

更新:尝试调试问题,我发现问题是当到达jsonlite包中的以下函数时,该函数似乎检查是否存在URL,否则将其视为JSON文本.由于某种原因,它进入了其他"部分......这是功能:

Update: Trying to debug the problem, I have found that the issue is when the following function in the jsonlite package is reached, which seems to check whether there is a URL and otherwise treats it as JSON text. For some reason, it enters the "else" part... Here is the function:

function (txt, bigint_as_char = FALSE) 
{
    if (inherits(txt, "connection")) {
        parse_con(txt, bigint_as_char)
    }
    else {
        parse_string(txt, bigint_as_char)
    }
}

更新#2:我意识到当我将链接粘贴到 Microsoft Edge 或 Internet Explorer 中时,某些 URL 被切断,然后我收到一条消息,指出它不是有效的 JSON.我更改了默认设置以使用 Chrome 作为默认浏览器,因为 Chrome 不会将其切断.但它仍然不起作用......这可能是问题吗?有什么建议吗?

Update #2: I realised that when I paste the link into Microsoft Edge or Internet Explorer, some of the URL gets cut off and then I get a message that it is not a valid JSON. I changed the default settings to use Chrome as a default browser, since Chrome doesn't cut it off. But it still doesn't work... Could this be the problem? Any suggestions?

最终更新:我写信给包的创建者 Jeroen Ooms,他建议我从 GitHub 下载包,因为问题已在那里解决.这是一年多前的事情了,所以我想现在从 CRAN 下载时,这个包也没有这个问题.感谢所有回复的人!

Final Update: I wrote to the creator of the package, Jeroen Ooms, who advised me to download the package from GitHub since the problem was fixed there. This was more than a year ago, so I imagine by now the package doesn't have this problem also when downloading from CRAN. Thanks to all who replied!

推荐答案

问题已修复(一年前,但分享以防对其他人有帮助).

Problem fixed (a year ago, but sharing in case it helps anyone else).

我写信给包的创建者 Jeroen Ooms,他建议我从 GitHub 下载包,因为问题已在那里解决.这是一年多前的事情了,所以我想现在从 CRAN 下载时,标准包也不会有这个问题.

I wrote to the creator of the package, Jeroen Ooms, who advised me to download the package from GitHub since the problem was fixed there. This was more than a year ago, so I imagine by now the standard package doesn't have this problem also when downloading from CRAN.

从 GitHub 下载:

To download from GitHub:

devtools::install_github("jeroen/jsonlite")

这篇关于在 R 中使用带有 URL 的 jsonlite 的 fromJSON() 时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆