Skip to content

Instantly share code, notes, and snippets.

@3100
Created August 21, 2014 15:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 3100/6224f73ab8c11b19aa23 to your computer and use it in GitHub Desktop.
Save 3100/6224f73ab8c11b19aa23 to your computer and use it in GitHub Desktop.
how to test a R script
Combine <- function(df1, df2, fmt) {
# 同じサイズの2つの行列を用いて、各セル値を合成します。
#
# Args:
# df1: 行列1
# df2: 行列2
# fmt: 表示書式(sprintf)
#
# Returns:
# 合成した行列
matrix(sprintf(df1, df2, fmt = fmt), nrow = nrow(df1), dimnames = dimnames(df1))
}
# usage:
# library(testthat)
# source('<path of sample.R>')
# test_file('<path of this file'>)
test_that('Combine works', {
m1 <- cbind(c('hoge', 'huga', 'hige'),
c(1, 2, 3))
m2 <- cbind(c('a', 'b', 'c'),
c(5, 6, 7))
combined <- Combine(m1, m2, "%s %s")
expect_equal(combined[1,1], 'hoge a')
expect_equal(combined[3,2], '3 7')
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment