我从未使用过 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)
Run Code Online (Sandbox Code Playgroud)
但我收到此错误:
Error in httr::content(res$content) : is.response(x) is not TRUE
Run Code Online (Sandbox Code Playgroud)
有什么线索吗?
在经历了许多不幸之后,我得到了你所承诺的答案!
由于它涉及交互式图像,由 JavaScript 和 HTML 提供,因此该解决方案必须作为.RmdRStudio 中的文件运行。交互式图像也可以在同名.html文件中访问,knitr当您单击KnitRStudio 中的按钮时输出。
my_pics在 RStudio 中的新目录下创建一个新的 R 项目。在这个项目中,创建一个新的 R Notebook(这里my_book.Rmd),它应该紧挨my_pics.Rproj着上述目录。
在同一目录下,创建一个./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>
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
请注意,这些.txt文件可以像.html文件一样轻松保存。对于下面代码中的函数,只需要在templates和custom_element参数的默认值中重构文件扩展名。make_html()my_book.Rmd
现在我们准备好了!中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"))
```
Run Code Online (Sandbox Code Playgroud)
观察我们如何(在这种情况下)“懒惰地”使用默认值 ( "9606") for taxid,而不必指定它。同时也请注意,我们如何能同时突出了不是一个而是多个独立的组件,即伸缩泡("SL0073")和细胞皮层("SL0138")。
现在在最后一个块下面visualize()被调用
Run Code Online (Sandbox Code Playgroud)```{r} visualize(sls = list("SL0073", "SL0138")) ```
您将看到如下所示的交互式输出:
遗憾的是,它在 RStudio 中显得非常拥挤,并且可能需要一个 HTML 向导来更改支持.txt(或.html)文件,以在此 IDE 中实现正确格式化的 HTML。
与任何.Rmd文件一样,RStudio 为您提供了Knit将 Markdown 结果转换为.html文件的选项,该文件可以轻松访问并以报告的形式精美格式化!
随着my_book.Rmd在RStudio打开,点击Knit按钮,my_book.html应该是相同的目录中出现。您可以.html在网络浏览器(我使用 Chrome)中打开此文件,以查看它的全部魅力!
使用这两个交互式图像中的任何一个,您都可以悬停以突出显示图表的各种组件和层。此外,单击任何定义将通过超链接将您带到 UnitProt上的配置文件。
许多剩余的限制是由于swissbiopics-visualizerAPI 本身造成的。例如,通过此数据集从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>
Run Code Online (Sandbox Code Playgroud)