Skip to content

Commit 9904520

Browse files
author
Raphael
committed
Updated VectorDist print method
1 parent 01d7b79 commit 9904520

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
## Patches
2222

2323
- Added wrapper for VectorDistribution for quick concatenation of constructed distributions, `c.Distribution`
24+
- `VectorDistribution` `print` method more in line with base R vectors
2425

2526

2627
# distr6 1.3.0

R/Wrapper_VectorDistribution.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,14 @@ VectorDistribution$set("public","modelTable", function(){
257257
private$.wrappedModels
258258
})
259259

260+
VectorDistribution$set("public", "strprint", function(n = 100){
261+
names <- as.character(self$modelTable()$shortname)
262+
lng <- length(names)
263+
if(lng >(2*n))
264+
names = c(names[1:n], "...", names[(lng-n+1):lng])
265+
266+
return(names)
267+
})
260268
VectorDistribution$set("public", "getParameterValue", function(...){
261269
message("Vector Distribution should not be used to get/set parameters. Try to use '[' first.")
262270
return(NULL)

tests/testthat/test-VectorDistribution.R

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,12 @@ test_that("shared d/p/q/r",{
138138
Binom3 = qbinom(0.42,6,0.2)))
139139
})
140140

141+
test_that("print",{
142+
a = VectorDistribution$new(distribution = "Binomial", params = list(list(prob = 0.1, size = 2),
143+
list(prob = 0.6, size = 4),
144+
list(prob = 0.2, size = 6)))
145+
expect_equal(a$strprint(), c("Binom1","Binom2","Binom3"))
146+
expect_equal(a$strprint(1), c("Binom1","...","Binom3"))
147+
expect_output(a$print(), "Binom1")
148+
})
149+

0 commit comments

Comments
 (0)