前言
我通常dplyr
在我的包中使用.在此之前0.7.0
,我使用dplyr
动词的强调版本来避免在注释期间R CMD CHECK
.例如,代码:
x <- tibble::tibble(v = 1:3, w = 2)
y <- dplyr::filter(x, v > w)
Run Code Online (Sandbox Code Playgroud)
本来会产生这样的R CMD CHECK
说明:
* checking R code for possible problems ... NOTE
no visible binding for global variable ‘v’
Run Code Online (Sandbox Code Playgroud)
相比之下,使用标准评估版:
y <- dplyr::filter_(x, ~v > w)
Run Code Online (Sandbox Code Playgroud)
没有发出这样的说明.
但是,使用dplyr编程dplyr 0.7.0
的插图表示在包中包含函数的适当语法(以避免注释)是:dplyr
y <- dplyr::filter(x, .data$v > .data$w)
Run Code Online (Sandbox Code Playgroud)
因此,新闻文件说"不再需要每个主要动词的强调版本,因此这些功能已被弃用(但为了向后兼容性而保持不变)."
题
小插图说上面的新语法不会产生R CMD check
NOTES,"只要你还导入rlang::.data
了@importFrom rlang …
我有以下分组数据框,我想使用该函数dplyr::sample_n
从每个组的数据框中提取行.我想使用NDG
每个组中的分组变量的值作为从每个组中提取的行数.
> dg.tmp <- structure(list(Gene = c("CAMK1", "GHRL", "TIMP4", "CAMK1", "GHRL",
"TIMP4", "ARL8B", "ARPC4", "SEC13", "ARL8B", "ARPC4", "SEC13"
), GLB = c(3, 3, 3, 3, 3, 3, 10, 10, 10, 10, 10, 10), NDG = c(1,
1, 1, 2, 2, 2, 1, 1, 1, 2, 2, 2)), class = c("tbl_df", "tbl",
"data.frame"), row.names = c(NA, -12L), .Names = c("Gene", "GLB",
"NDG"))
> dg <- dg.tmp %>%
dplyr::group_by(GLB,NDG)
> dg
Source: local data frame [12 x 3] …
Run Code Online (Sandbox Code Playgroud) 我用来在R中$
添加一个列表列data.table
.当data.table
有多行时,这可以按预期工作.
library(data.table)
dt2 <- data.table(x = 1:2)
dt2$y <- list(c(1, 1), c(2, 2))
dt2
#> x y
#> 1: 1 1,1
#> 2: 2 2,2
Run Code Online (Sandbox Code Playgroud)
但是,当data.table
只有一行时,只返回列表中向量的第一个元素并带有警告:
dt1 <- data.table(x = 1)
dt1$y <- list(c(1, 1))
#> Warning in `[<-.data.table`(x, j = name, value = value): Supplied 2 items
#> to be assigned to 1 items of column 'y' (1 unused)
dt1
#> x y
#> 1: 1 1
Run Code Online (Sandbox Code Playgroud)
这似乎不一致.它是一个功能还是一个bug?
相比之下,使用data.frame
s …
有效的代码:duration
和period
对象
以下代码分别成功生成了一个duration
对象和一个period
对象.
> lubridate::as.duration(1)
[1] "1s"
> lubridate::seconds(1)
[1] "1S"
Run Code Online (Sandbox Code Playgroud)
不起作用的代码:duration
和s中的period
对象tibble
但是,当我尝试tibble
使用一个duration
或一个period
对象创建s时,我得到无法提供信息的错误消息.
> tibble::tibble(y = lubridate::as.duration(1))
Error: Incompatible duration classes (Duration, numeric). Please coerce with `as.duration`.
> tibble::tibble(y = lubridate::seconds(1))
Error in x < 0 : cannot compare Period to Duration:
coerce with 'as.numeric' first.
Run Code Online (Sandbox Code Playgroud)
有效的代码:duration
和s中的period
对象data.frame
更换tibble::tibble
用base::data.frame
作品.
> data.frame(y = lubridate::as.duration(1))
y …
Run Code Online (Sandbox Code Playgroud) 我想创建一个生成ggplot
图的函数,并为方面变量提供可选参数facet_grid()
。
特别是,如果可能的话,我想纳入条件逻辑里面 facet_grid
。我也想使用整洁的评估框架-所以没有公式字符串!
但是,我所有的尝试都失败了。
library(tidyverse)
iris <- iris %>% add_column(idx = rep(1:2, 75))
Run Code Online (Sandbox Code Playgroud)
我的第一次尝试失败,因为facet_grid
试图找到一个名为NULL
(带有反引号)的变量。
plot_iris <- function(df_in, facet_var = NULL){
ggplot(df_in) +
geom_point(aes(Sepal.Length, Sepal.Width)) +
facet_grid(vars(!!enquo(facet_var)), vars(idx))
}
plot_iris(iris)
#> Error: At least one layer must contain all faceting variables: `NULL`.
#> * Plot is missing `NULL`
#> * Layer 1 is missing `NULL`
Run Code Online (Sandbox Code Playgroud)
plot_iris(iris, Species)
但是,运行正常。
我的第二次尝试也失败了,但是有不同的错误消息。
plot_iris2 <- function(df_in, facet_var = NULL){
facet_quo <- enquo(facet_var)
ggplot(df_in) …
Run Code Online (Sandbox Code Playgroud)