Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 在管道中的同一对象上调用两个不同的函数(%>;%)_R_Magrittr - Fatal编程技术网

R 在管道中的同一对象上调用两个不同的函数(%>;%)

R 在管道中的同一对象上调用两个不同的函数(%>;%),r,magrittr,R,Magrittr,我想知道是否有一种方法可以同时调用html\u name()和html\u text(从rvest包)并将两个不同的结果存储在同一管道中(magrittr::%>%) 以下是一个例子: uniprot_ac <- "P31374" GET(paste0("https://www.uniprot.org/uniprot/", uniprot_ac, ".xml")) %>% content(as = "raw", content = "text/xml") %>%

我想知道是否有一种方法可以同时调用
html\u name()
html\u text
(从
rvest
包)并将两个不同的结果存储在同一管道中(
magrittr::%>%

以下是一个例子:

uniprot_ac <- "P31374"

GET(paste0("https://www.uniprot.org/uniprot/", uniprot_ac, ".xml")) %>%
    content(as = "raw", content = "text/xml") %>%
    read_html %>%
    html_nodes(xpath = '//recommendedname/* |
               //name[@type="primary"] | //comment[@type="function"]/text |
               //comment[@type="interaction"]/text')
通过重写整个管道,将最后一行更改为
html\u text()

所需的输出可以是这样的,矢量或数据。帧不重要

  [1] fullname: "Serine/threonine-protein kinase PSK1"                                                                                                                                                                                                                                                                             
  [2] ecnumber: "2.7.11.1"                                                                                                                                                                                                                                                                                                         
  [3] Name: "PSK1"                                                                                                                                                                                                                                                                                                             
  [4] Text: "Serine/threonine-protein kinase involved ... ... 

可能有点小技巧,但您可以在管道中使用括号内的匿名函数:

library("magrittr")
library("httr")
library("xml2")
library("rvest")

uniprot_ac <- "P31374"

GET(paste0("https://www.uniprot.org/uniprot/", uniprot_ac, ".xml")) %>%
  content(as = "raw", content = "text/xml") %>%
  read_html %>%
  html_nodes(xpath = '//recommendedname/* |
             //name[@type="primary"] | //comment[@type="function"]/text |
             //comment[@type="interaction"]/text') %>% 
  (function(x) list(name = html_name(x), text = html_text(x)))
#$name
#[1] "fullname" "ecnumber" "name"     "text"    
#
#$text
#[1] "Serine/threonine-protein kinase PSK1"                                                                                                                                                                                                                                                                             
#[2] "2.7.11.1"                                                                                                                                                                                                                                                                                                         
#[3] "PSK1"                                                                                                                                                                                                                                                                                                             
#[4] "Serine/threonine-protein kinase involved in the control of sugar metabolism and translation. Phosphorylates UGP1, which is required for normal glycogen and beta-(1,6)-glucan synthesis. This phosphorylation shifts glucose partitioning toward cell wall glucan synthesis at the expense of glycogen synthesis."

这可以说是一种更为magrittr惯用的方法,它实际上记录在
帮助(“%%>%”)中

可能有点小技巧,但您可以在管道中使用括号内的匿名函数:

library("magrittr")
library("httr")
library("xml2")
library("rvest")

uniprot_ac <- "P31374"

GET(paste0("https://www.uniprot.org/uniprot/", uniprot_ac, ".xml")) %>%
  content(as = "raw", content = "text/xml") %>%
  read_html %>%
  html_nodes(xpath = '//recommendedname/* |
             //name[@type="primary"] | //comment[@type="function"]/text |
             //comment[@type="interaction"]/text') %>% 
  (function(x) list(name = html_name(x), text = html_text(x)))
#$name
#[1] "fullname" "ecnumber" "name"     "text"    
#
#$text
#[1] "Serine/threonine-protein kinase PSK1"                                                                                                                                                                                                                                                                             
#[2] "2.7.11.1"                                                                                                                                                                                                                                                                                                         
#[3] "PSK1"                                                                                                                                                                                                                                                                                                             
#[4] "Serine/threonine-protein kinase involved in the control of sugar metabolism and translation. Phosphorylates UGP1, which is required for normal glycogen and beta-(1,6)-glucan synthesis. This phosphorylation shifts glucose partitioning toward cell wall glucan synthesis at the expense of glycogen synthesis."

这可以说是一种更为magrittr惯用的方法,它实际上记录在
帮助(“%%>”

中。您可以创建一个自定义函数,接收
html\u节点
对象,并对其执行任何所需的操作:

html_name_text <- function(nodes) {
    list(html_name(nodes), html_text(nodes))
}

GET(paste0("https://www.uniprot.org/uniprot/", uniprot_ac, ".xml")) %>%
    content(as = "raw", content = "text/xml") %>%
    read_html %>%
    html_nodes(xpath = '//recommendedname/* |
               //name[@type="primary"] | //comment[@type="function"]/text |
               //comment[@type="interaction"]/text') %>%
    html_name_text()

[[1]]
[1] "fullname" "ecnumber" "name"     "text"    

[[2]]
[1] "Serine/threonine-protein kinase PSK1"                                                                                                                                                                                                                                                                             
[2] "2.7.11.1"                                                                                                                                                                                                                                                                                                         
[3] "PSK1"                                                                                                                                                                                                                                                                                                             
[4] "Serine/threonine-protein kinase involved in the control of sugar metabolism and translation. Phosphorylates UGP1, which is required for normal glycogen and beta-(1,6)-glucan synthesis. This phosphorylation shifts glucose partitioning toward cell wall glucan synthesis at the expense of glycogen synthesis."
html\u name\u text%
内容(as=“raw”,content=“text/xml”)%%>%
读取\u html%>%
html_节点(xpath='//recommendedname/*|
//name[@type=“primary”]|//注释[@type=“function”]/text|
//注释[@type=“interaction”]/text')%>%
html_name_text()
[[1]]
[1] 全名“ecnumber”名称“text”
[[2]]
[1] “丝氨酸/苏氨酸蛋白激酶PSK1”
[2] "2.7.11.1"                                                                                                                                                                                                                                                                                                         
[3] “PSK1”
[4] 丝氨酸/苏氨酸蛋白激酶,参与糖代谢和翻译的控制。使正常糖原和β-(1,6)-葡聚糖合成所需的UGP1磷酸化。这种磷酸化将葡萄糖分配转移到细胞壁葡聚糖合成,而以糖原合成为代价

您可以创建一个自定义函数,用于接收
html\u节点
对象并对其执行任何所需操作:

html_name_text <- function(nodes) {
    list(html_name(nodes), html_text(nodes))
}

GET(paste0("https://www.uniprot.org/uniprot/", uniprot_ac, ".xml")) %>%
    content(as = "raw", content = "text/xml") %>%
    read_html %>%
    html_nodes(xpath = '//recommendedname/* |
               //name[@type="primary"] | //comment[@type="function"]/text |
               //comment[@type="interaction"]/text') %>%
    html_name_text()

[[1]]
[1] "fullname" "ecnumber" "name"     "text"    

[[2]]
[1] "Serine/threonine-protein kinase PSK1"                                                                                                                                                                                                                                                                             
[2] "2.7.11.1"                                                                                                                                                                                                                                                                                                         
[3] "PSK1"                                                                                                                                                                                                                                                                                                             
[4] "Serine/threonine-protein kinase involved in the control of sugar metabolism and translation. Phosphorylates UGP1, which is required for normal glycogen and beta-(1,6)-glucan synthesis. This phosphorylation shifts glucose partitioning toward cell wall glucan synthesis at the expense of glycogen synthesis."
html\u name\u text%
内容(as=“raw”,content=“text/xml”)%%>%
读取\u html%>%
html_节点(xpath='//recommendedname/*|
//name[@type=“primary”]|//注释[@type=“function”]/text|
//注释[@type=“interaction”]/text')%>%
html_name_text()
[[1]]
[1] 全名“ecnumber”名称“text”
[[2]]
[1] “丝氨酸/苏氨酸蛋白激酶PSK1”
[2] "2.7.11.1"                                                                                                                                                                                                                                                                                                         
[3] “PSK1”
[4] 丝氨酸/苏氨酸蛋白激酶,参与糖代谢和翻译的控制。使正常糖原和β-(1,6)-葡聚糖合成所需的UGP1磷酸化。这种磷酸化将葡萄糖分配转移到细胞壁葡聚糖合成,而以糖原合成为代价

这里有一种
purr
方法,它返回一个
tibble

库(tidyverse)
图书馆(rvest)
uniprot_ac%
html_节点(xpath='//recommendedname/*|
//name[@type=“primary”]|//注释[@type=“function”]/text|
//注释[@type=“interaction”]/text')%>%
映射(~list(name=html\u name(.),text=html\u text(.))%>%
绑定行
#>#tibble:4 x 2
#>名称文本
#>                                                                  
#>1全名丝氨酸/苏氨酸蛋白激酶PSK1
#>2 EC2编号2.7.11.1
#>3名称PSK1
#>4文本丝氨酸/苏氨酸蛋白激酶参与suga的控制~

由(v0.2.1)于2019-03-26创建,这里有一个
purr
方法,它返回一个
tibble

库(tidyverse)
图书馆(rvest)
uniprot_ac%
html_节点(xpath='//recommendedname/*|
//name[@type=“primary”]|//注释[@type=“function”]/text|
//注释[@type=“interaction”]/text')%>%
映射(~list(name=html\u name(.),text=html\u text(.))%>%
绑定行
#>#tibble:4 x 2
#>名称文本
#>                                                                  
#>1全名丝氨酸/苏氨酸蛋白
html_name_text <- function(nodes) {
    list(html_name(nodes), html_text(nodes))
}

GET(paste0("https://www.uniprot.org/uniprot/", uniprot_ac, ".xml")) %>%
    content(as = "raw", content = "text/xml") %>%
    read_html %>%
    html_nodes(xpath = '//recommendedname/* |
               //name[@type="primary"] | //comment[@type="function"]/text |
               //comment[@type="interaction"]/text') %>%
    html_name_text()

[[1]]
[1] "fullname" "ecnumber" "name"     "text"    

[[2]]
[1] "Serine/threonine-protein kinase PSK1"                                                                                                                                                                                                                                                                             
[2] "2.7.11.1"                                                                                                                                                                                                                                                                                                         
[3] "PSK1"                                                                                                                                                                                                                                                                                                             
[4] "Serine/threonine-protein kinase involved in the control of sugar metabolism and translation. Phosphorylates UGP1, which is required for normal glycogen and beta-(1,6)-glucan synthesis. This phosphorylation shifts glucose partitioning toward cell wall glucan synthesis at the expense of glycogen synthesis."
GET(paste0("https://www.uniprot.org/uniprot/", uniprot_ac, ".xml")) %>%
    content(as = "raw", content = "text/xml") %>%
    read_html %>%
    html_nodes(xpath = '//recommendedname/* |
               //name[@type="primary"] | //comment[@type="function"]/text |
               //comment[@type="interaction"]/text') %>% {
    list(name = html_name(.), text = html_text(.))
    }
iris %>% 
  select(Sepal.Length, Sepal.Width) %>% {
     temp <- .
     bind_rows(temp %>% filter(Sepal.Length > 5), 
               temp %>% filter(Sepal.Width <= 3))
} %>% 
  dim()
nodes %>% lapply(list(html_name, html_text), function(x,y) x(y), .)
# [[1]]
# [1] "fullname" "ecnumber" "name"     "text"    
# 
# [[2]]
# [1] "Serine/threonine-protein kinase PSK1"                                                                                                                                                                                                                                                                             
# [2] "2.7.11.1"                                                                                                                                                                                                                                                                                                         
# [3] "PSK1"                                                                                                                                                                                                                                                                                                             
# [4] "Serine/threonine-protein kinase involved in the control of sugar 
nodes %>% {lapply(list(html_name, html_text), do.call, list(.))}
library(purrr)
nodes %>% map(list(html_name, html_text), exec, .)
library("magrittr")
library("httr")
library("xml2")
library("rvest")
nodes <- GET(paste0("https://www.uniprot.org/uniprot/", uniprot_ac, ".xml")) %>%
  content(as = "raw", content = "text/xml") %>%
  read_html %>%
  html_nodes(xpath = '//recommendedname/* |
             //name[@type="primary"] | //comment[@type="function"]/text |
             //comment[@type="interaction"]/text')