从 tibble 中获取值的最简洁方法 [英] Most concise way to get a value from within a tibble

查看:28
本文介绍了从 tibble 中获取值的最简洁方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设置测试.我对测试数据进行操作,然后想确保正确的值出现在小标题的单元格中.我认为有一种更简洁的方法.使用示例 band_instruments

I'm setting up tests. I operate on test-data, then want to assure that the right value showed up in a cell in a tibble. I think there's a more concise way to this. Using the example band_instruments

library(tidyverse)
test_that("Musicians play instruments", {
      expect_equal(band_instruments %>% filter(name == "Paul") %>% pull("plays"),
                   "bass")
      expect_equal({band_instruments %>% filter(name == "Keith")}$plays,
                   "guitar")
})

这行得通,但太长,太罗嗦了.进行此类测试的最简洁但最易读的方法是什么?

This works, but it's too long, too wordy. What's the most concise, yet readable way to do such tests?

推荐答案

这对我来说看起来很整洁:

This looks quite neat to me:

test_that("Musicians play instruments", {
  expect_equal(with(band_instruments, plays[name == "Paul"]), "bass")
  expect_equal(with(band_instruments, plays[name == "Keith"]), "guitar")})

也许是这样:

with(band_instruments, test_that("Musicians play instruments", {
  expect_equal(plays[name == "Paul"], "bass")
  expect_equal(plays[name == "Keith"], "guitar")}))

这篇关于从 tibble 中获取值的最简洁方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆