Skip to content

Commit

Permalink
Updated simulation, summary, and plot functions. Changed vignette. Up…
Browse files Browse the repository at this point in the history
…dated manuals and descriptions.
  • Loading branch information
KPDuBose committed Mar 15, 2023
1 parent 7b18953 commit 0731b36
Show file tree
Hide file tree
Showing 12 changed files with 128 additions and 15 deletions.
10 changes: 7 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
Package: partyGames
Type: Package
Title: Tool to Simulate Various Party Games and Develop Better Strategies
Title: Simulate Party Games
Version: 0.1.0
Author: Kline DuBose
Maintainer: Kline DuBose <[email protected]>
Description: Simulation tools that help develop better strategies to win at party games.
Description: Simulation tools that allow users to play party games and simulate
multiple games at a time to improve strategies.
License: MIT + file LICENSE
Encoding: UTF-8
LazyData: true
Expand All @@ -16,5 +17,8 @@ RoxygenNote: 7.2.3
LinkingTo:
Rcpp
Imports:
Rcpp
Rcpp,
methods,
parallel,
graphics
VignetteBuilder: knitr
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@ export(diceSum)
export(dieElephant)
export(leftRightElephant)
export(lrDieElephant)
export(plotElephant)
export(simulateElephant)
export(summaryElephant)
export(twoCoinDieElephant)
export(twoCoinElephant)
importFrom(Rcpp,evalCpp)
importFrom(graphics,barplot)
importFrom(methods,is)
importFrom(parallel,makeCluster)
importFrom(parallel,parSapply)
importFrom(parallel,stopCluster)
Expand Down
2 changes: 2 additions & 0 deletions R/partyGames-package.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#' @importFrom Rcpp evalCpp
#' @importFrom parallel makeCluster parSapply stopCluster
#' @importFrom graphics barplot
#' @importFrom methods is
#' @useDynLib partyGames, .registration = TRUE
NULL
32 changes: 32 additions & 0 deletions R/plotElephant.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#' Plot results of a White Elephant simulation
#'
#' A general function to plot the results of a White Elephant simulation.
#'
#' @param sim An object of class `elphList` or `elphSum` to be summarized
#' @param ... Arguments to be passed to other methods. See `barplot` for further details.
#'
#' @return A plot showing the distribution of each winning seat and how often they won.
#'
#' @export
#'
#'


plotElephant <- function(sim, ...){
if(!(is(sim, "elphList") | is(sim, "elphSum"))) stop("'sim' must be an 'elphList' or 'elphSum' class object")

if(is(sim, "elphList")){
sim <- summaryElephant(sim)
}


barplot(
sim[[1]] / sum(sim[[1]]),
ylim = c(0, max(sim[[1]] / sum(sim[[1]])) + 0.05),
sub = sprintf("Distribution of %i simulated white elephant games", sum(sim[[1]])),
main = "Who gets the present?",
col = "cornflowerblue",
border = "dimgray"
)

}
9 changes: 3 additions & 6 deletions R/simulateElephant.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
#' @param sides Number of sides on dice
#' @param numDice Number of dice rolled
#'
#' @return Various graphics and tables showing the number of times a seat was
#' selected over many iterations of the game.
#' @return A list of clas `elphList`
#'
#' @export

Expand Down Expand Up @@ -63,10 +62,8 @@ simulateElephant <- function(n,
}
}

total <- sapply(ans, "[[", "winner")
class(ans) <- c("elphList", "list")

tableTotal <- table(total)

return(c(tableTotal))
return(ans)

}
30 changes: 30 additions & 0 deletions R/summaryElephant.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#' Summarize a simulation
#'
#' A general function to summarize a simulation of white elephant games.
#'
#' @param sim An object of class `elphList` to be summarized
#'
#' @return An object of class `elphSum` containing a table of moves over a
#' simulation and a table of times each chair won.
#'
#' @export
#'
#'

summaryElephant <- function(sim){
if (!is(sim, "elphList")) stop("sim must be a 'elphList' class object")

moves <- sapply(sim, "[[", "moves")

moves <- table(moves)

winner <- sapply(sim, "[[", "winner")

winner <- table(winner)

elphSum <- list(winner = winner, moves = moves)

class(elphSum) <- c("elphSum", "list")

return(elphSum)
}
2 changes: 1 addition & 1 deletion inst/tinytest/test_partyGames.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ expect_equal(
set.seed(123)
ans3 <- leftRightElephant(10, 0.5)
expect_equal(
9,
1,
ans3[[2]]
)

Expand Down
19 changes: 19 additions & 0 deletions man/plotElephant.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions man/simulateElephant.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions man/summaryElephant.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/leftRightElephant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ using namespace Rcpp;
}

return List::create(
_["movement"] = movement,
_["moves"] = movement,
_["winner"] = seat
);

Expand Down
12 changes: 10 additions & 2 deletions vignettes/example-vignette.Rmd
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: "Example Vignette"
title: "The Basics of Party Games"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{example-vignette}
Expand All @@ -18,5 +18,13 @@ knitr::opts_chunk$set(
library(partyGames)
```

The goal of this package is to simulate various party game scenarios to help evaluate the effectiveness of certain strategies.
The goal of this package is to simulate various party game scenarios to help evaluate the effectiveness of certain strategies. This vignette hopes to share some of the capabilities of this package.

# Dice Rolling

One of the functions simply rolls a dice.

```{r, eval=FALSE}
diceSum(sides, numDice)
```

0 comments on commit 0731b36

Please sign in to comment.