DndDice/R/mean_print_summary_plot.R

66 lines
1.1 KiB
R
Raw Permalink Normal View History

2019-03-19 03:56:07 +00:00
#'
#' Calculate the mean of a die.
#'
#' Calculate the mean.
#'
#' @param x A die
#' @return The expected value of the die.
#' @examples
#' mean(d20)
#'
#' @export
mean.Die <- function(x){
return(sum(x$numbers*x$probs))
}
#'
#' Print a die.
#'
#' Rolls a die a single time.
#'
#' @param x A die to roll.
#' @param ... Other parameters to pass to \code{\link{roll}}
#' @examples
#' print(d20)
#'
#' @export
print.Die <- function(x, ...){
print(roll(x, ...))
}
#'
#' Print a die's summary.
#'
#' Return the probabilities of a die for different rolls.
#'
#' @param x A die
#' @return A vector of named probabilities for each outcome of the die.
#' @examples
#' summary(advantage(d20,d20))
#' @export
summary.Die <- function(x){
probs <- x$probs
names(probs) <- x$numbers
return(probs)
}
#'
#' Plot a die's summary.
#'
#' Plot the probabilities of a die for different rolls.
#'
#' @param x A die
#' @return A ggplot2 plot.
#' @examples
#' plot(advantage(d20,d20))
#'
#' @export
plot.Die <- function(x){
data = data.frame(Number=x$numbers, Probability=x$probs)
plt = ggplot(data, aes(x=Number, y=Probability)) + geom_col()
return(plt)
}