r - 使用 R 从 API 中检索图片
问题描述
我从未使用过 API,所以这是我的第一次尝试,我什至不知道我正在尝试做的事情是否可行。
我正在尝试从 SwissBioPics API ( https://www.npmjs.com/package/%40swissprot/swissbiopics%2Dvisualizer ) 获取细胞图片并将它们放在我的 R 会话中。
res <- httr::GET('https://www.swissbiopics.org/static/swissbiopics.js',
query = list(taxisid = '9606', sls= 'SL0073',gos = '0005641'))
result <- httr::content(res$content)
但我收到此错误:
Error in httr::content(res$content) : is.response(x) is not TRUE
有什么线索吗?
解决方案
在经历了许多不幸之后,我如约得到了你的答案!
由于它涉及交互式图像,由 JavaScript 和 HTML 提供,因此该解决方案必须.Rmd
在 RStudio 中作为文件运行。交互式图像也可以在同名.html
文件中访问,knitr
当您单击KnitRStudio 中的按钮时输出。
第 1 步:项目设置
my_pics
在 RStudio 中的新目录下创建一个新的 R 项目。在这个项目中,创建一个新的 R Notebook(这里),它应该就在上述目录的my_book.Rmd
旁边。my_pics.Rproj
第 2 步:支持文件
在同一目录下,创建一个./snippets
子目录。后者应包含以下两个文件,从文档.txt
中复制(并更正):swissbiopics-visualizer
templates.txt
:文档中给出的第一个代码块。在这里转载了必要的EOF
和语法更正的评论:
<template id="sibSwissBioPicsStyle">
<style>
ul > li > a {
font-style:oblique;
}
ul.notpresent li > .subcell_description {
display:none;
}
</style>
</template>
<template id="sibSwissBioPicsSlLiItem">
<li class="subcellular_location">
<a class="subcell_name"></a> <!-- the class name is required and textContent will be set on it -->
<span class="subcell_description"></span> <!-- the class name is required and textContent will be set on it -->
</li>
</template>
custom_element.txt
:文档中给出的第三个代码块。必要时在此处转载EOF
:
<script type="module" src="https://www.swissbiopics.org/static/swissbiopics.js"></script>
<script defer>
if (! window.customElements.get("sib-swissbiopics-sl"))
window.customElements.define("sib-swissbiopics-sl", SwissBioPicsSL);
</script>
请注意,这些.txt
文件可以很容易地保存为.html
文件。对于下面代码中的函数,仅需要在templates
和custom_element
参数的默认值中重构文件扩展名。make_html()
my_book.Rmd
第 3 步:在 RStudio 中进行交互
现在我们准备好了!在my_book.Rmd
中,写下以下内容:
---
title: "R Notebook"
output: html_document
---
```{r}
library(htmltools)
library(readr)
library(rlang)
```
# Functions #
Here are the functions that do the trick. The snippets used by `make_html()` are copied from the [documentation](https://www.npmjs.com/package/%40swissprot/swissbiopics-visualizer#usage) for `swissbiopics-visualizer`, and (after fixing the HTML comments) pasted into `.txt` files (`templates.txt` and `custom_element.txt`) under the `./snippets` subdirectory, which lies within the directory containing this `.Rproj`.
```{r}
# Create comma-separated list from vectorized (or listed) items, safely escaped.
csl <- function(items) {
return(paste("\"", paste(htmltools::htmlEscape(unlist(items)), collapse = ",", sep = ""), "\"", sep = ""))
}
# Create the HTML for the interactive imagery given by the parameters. Assembly
# process is as described the documentation for 'swissbiopics-visualizer':
# https://www.npmjs.com/package/%40swissprot/swissbiopics-visualizer#usage
make_html <- function(# The NCBI taxonomy ID.
tax_id,
# The IDs of the cellular elements to highlight.
sl_ids,
# The filepath to (or raw HTML text of) the templates
# snippet.
templates = "./snippets/templates.txt",
# The filepath to (or raw HTML text of) the custom element
# snippet.
custom_element = "./snippets/custom_element.txt",
# Further arguments to 'readr::read_file()', which might
# be useful to process snippet encodings across platforms.
...) {
# Escape any strings supplied.
tax_id <- csl(tax_id[1])
sl_ids <- csl(sl_ids)
# Compile all the HTML snippets into a list:
elements <- list()
# Include the templates (as read)...
elements$templates <- readr::read_file(file = templates, ...)
# ...then include the line (created here) to target the right picture...
elements$identifier <- "<sib-swissbiopics-sl taxid=%s sls=%s></sib-swissbiopics-sl>"
elements$identifier <- sprintf(fmt = elements$identifier, tax_id, sl_ids)
# ...and finally include the definition (as read) for the custom element.
elements$custom_element <- readr::read_file(file = custom_element, ...)
# Append these snippets together, into the full HTML code.
return(paste(unlist(elements), collapse = "\n"))
}
# Display the interactive imagery given by the parameters, visible in both
# RStudio (crowded) and the R Markdown file (well laid out).
visualize <- function(# The NCBI taxonomy ID.
taxid = "9606",
# A list (or vector) of the UniProtKB subcellular location
# (SL) IDs for the cellular elements to highlight.
sls = list("SL0073"),
# Further arguments to 'make_html()'.
...
) {
# Embed the HTML text where this function is called.
return(htmltools::HTML(make_html(tax_id = taxid, sl_ids = sls, ...)))
}
```
# Results #
Here we `visualize()` the **interactive** image, also accessible on [SwissBioPics](https://www.swissbiopics.org):
```{r}
visualize(sls = list("SL0073", "SL0138"))
```
笔记
观察我们如何(在这种情况下)“懒惰地”使用默认值("9606"
)taxid
,而不必指定它。还要观察我们如何同时突出显示多个独立的组件,即收缩液泡( "SL0073"
) 和细胞皮层( "SL0138"
)。
现在在最后一个块下面visualize()
被调用
```{r} visualize(sls = list("SL0073", "SL0138")) ```
您将看到如下所示的交互式输出:
遗憾的是,它在 RStudio 中显得非常拥挤,可能需要一个 HTML 向导来更改支持.txt
(或.html
)文件,以在此 IDE 中实现正确格式化的 HTML。
第 4 步:嵌入报告
与任何.Rmd
文件一样,RStudio 为您提供了Knit将 Markdown 结果转换为.html
文件的选项,该文件可以轻松访问并格式化为报告!
在my_book.Rmd
RStudio 中打开,单击Knit按钮,my_book.html
应该会出现在同一目录中。您可以在网络浏览器(我使用 Chrome)中打开此.html
文件,以查看它的全部荣耀!
综上所述
使用这两个交互式图像中的任何一个,您都可以将鼠标悬停以突出显示图表的各个组件和层。此外,单击任何定义将通过超链接将您带到 UnitProt上的配置文件。
许多剩余的限制是由于swissbiopics-visualizer
API 本身造成的。例如,通过此数据集从GO ID到SL ID的映射似乎存在故障。因此,您应该只向.visualize()
就是说,如果您可以处理该 HTML 并根据您的意愿调整其布局,那么天空就是极限!
享受!
奖金
这是嵌入在 Stack Overflow 中的相同交互式输出的演示!不幸的是,它在这种环境中不稳定且非常笨拙,所以我将其仅作为“脚注”:
<template id="sibSwissBioPicsStyle">
<style>
ul > li > a {
font-style:oblique;
}
ul.notpresent li > .subcell_description {
display:none;
}
</style>
</template>
<template id="sibSwissBioPicsSlLiItem">
<li class="subcellular_location">
<a class="subcell_name"></a> <!-- the class name is required and textContent will be set on it -->
<span class="subcell_description"></span> <!-- the class name is required and textContent will be set on it -->
</li>
</template>
<sib-swissbiopics-sl taxid="9606" sls="SL0073,SL0138" ></sib-swissbiopics-sl>
<script type="module" src="https://www.swissbiopics.org/static/swissbiopics.js"></script>
<script defer>
if (! window.customElements.get("sib-swissbiopics-sl"))
window.customElements.define("sib-swissbiopics-sl", SwissBioPicsSL);
</script>
推荐阅读
- encryption - 使用 Apache Camel 进行 PGP 加密/解密:在密钥环中找不到密钥
- html - 如何将此 HTML 片段转换为引导程序
- maven - 如何将 SoapUI、Maven 和 HermesJMS 与 Jenkins 构建相结合?
- python - 具有空值的 ElasticSearch 过滤器查询
- powershell - 从注册表 ACL 中删除用户
- android - Android 辅助功能中的读取日期
- ionic-framework - Ionic v4 后退按钮查询参数
- r - 相对于数字的列序列拆分数据框
- bash - 将“带有管道的命令”作为 bash 函数参数传递
- fonts - 为字体创建源