2

I have been trying to make a visual of a game theory payoff matrix in R, but can't generate a visual. I found an attempt to answering this question in another payoff matrix post, but the code is not producing the visual. I would appreciate any suggestions.

Here is the code from the other post:

###########################################################################
############# Game Theory Payoff Matrix Visual ############################
###########################################################################

# Packages
library(rmarkdown)
library(knitr)

# Define Variables
T=2
R=1
P=0
S=-1

# Assign Pairs
pair <- function(x,y) sprintf("(%d,%d)", x,y)
all_pairs <- c(pair(T,S), pair(P,P), pair(R,R), pair(S,T))
payoff.mat <- matrix(all_pairs, nrow=2)
dimnames(payoff.mat) <- c(rep(list(c("Gift","NoGift")), 2))
results = "asis"

# Plot 
kable(payoff.mat)

print(all_pairs, type="html")

While I have my own data, the intended template of the other post applies to my publication needs.

Also, is there a way to make a variety of plain to fancy payoff matrix visuals in R?

Again, I would appreciate any considerations.

Community
  • 1
  • 1
  • 1
    Presuming you are putting this in a markdown document, you probably need to set the results chunk option `results = "asis"`. The `format` option is not required within a markdown comment as `knitr` will automatically set it. http://stackoverflow.com/a/19859769/3242130 – manotheshark Jan 13 '17 at 03:09
  • @manotheshark - I have edited the above text to reflect the changed code. That was certainly helpful, but the code still is not producing a graphic, its only generating text output. --The following is the output: ----------------------- | |Gift |NoGift | |:------|:------|:------| |Gift |(2,-1) |(1,1) | |NoGift |(0,0) |(-1,2) | --------------------------- I added the results and deleted the format portions, but don't have a graphic. How do I produce a graphic or document through rmarkdown, or at all? – Bradley Thomas Anderson Jan 13 '17 at 04:01
  • 1
    Are you trying to create this matrix in the console using a script? Or is the visual an HTML document? If so you need to create a markdown document and not a script (.R) - http://rmarkdown.rstudio.com/lesson-1.html – manotheshark Jan 13 '17 at 04:20
  • @manotheshark - I wanted to followup and say that helped tremendously. The code produces exactly what I want. Also, I have been learning a lot about R and all its capacities, and when you brought R Markdown to my awareness I went and researched it. What a fantastic package and resource! Thanks again for your help. – Bradley Thomas Anderson Jan 24 '17 at 18:05

0 Answers0