我从未使用过 API,所以这是我的第一次尝试,我什至不知道我正在尝试做的事情是否可行。
我正在尝试从 SwissBioPics API ( https://www.npmjs.com/package/%40swissprot/swissbiopics%2Dvisualizer ) 获取细胞图片并将它们放入我的 R session 中。
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
有什么线索吗?
最佳答案
历经千辛万苦,我有了你的答案as promised !
由于它涉及交互式图像,由 JavaScript 和 HTML 提供,此解决方案必须作为 .Rmd
运行。 RStudio 中的文件。交互式图像也可以在同名的 .html
中访问。文件,由 knitr
输出当您单击 RStudio 中的编织按钮时。
第 1 步:项目设置
创建一个新的 R 项目 my_pics
在 RStudio 中,在新目录下。在这个项目中,创建一个新的 R Notebook(这里是 my_book.Rmd
),它应该紧挨着 my_pics.Rproj
结束。在上述目录下。
第 2 步:支持文件
在同一个目录下,创建一个 ./snippets
子目录。后者应该包含以下两个.txt
从 swissbiopics-visualizer
复制(并更正)的文件documentation :
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
) ,而不必指定它。还观察我们如何才能同时高亮不是一个而是多个 单独的组件,即 Contractile vacuole ( "SL0073"
) 和 Cell cortex ( "SL0138"
)。现在在最后一个块下面
visualize()
叫做```{r} visualize(sls = list("SL0073", "SL0138")) ```
您将看到如下所示的交互式输出:
可悲的是,它出现 非常拥挤 在 RStudio 中,可能需要一个 HTML 向导来更改支持
.txt
(或 .html
)文件,以在此 IDE 中实现格式正确的 HTML。第 4 步:嵌入报告
与任何
.Rmd
一样文件,RStudio 为您提供了将 Markdown 结果编织成 .html
的选项文件,可以轻松访问和 格式精美作为 举报 !与
my_book.Rmd
在 RStudio 中打开,单击编织按钮,然后 my_book.html
应该出现在同一个目录中。你可以打开这个.html
文件在网络浏览器(我使用 Chrome)中查看它的所有荣耀!综上所述
使用这两个交互式图像中的任何一个,您都可以 悬停以突出显示 图表的各种组件和层。此外,单击任何定义将通过超链接带您到其在 UnitProt 上的配置文件。 .
许多剩余的限制是由于
swissbiopics-visualizer
API 本身。比如它的mapping好像有故障来自 GO IDs至 SL IDs , 通过 this dataset .因此,您应该只向 visualize()
提供 SL 代码 .就是说,如果您可以解决该 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>
关于使用 R 从 API 检索图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68164869/