Q:

Extract p-value from fisher.test() using pipeline

Let's have a data set, e.g.:
set.seed(123)
n <- 50
x <- sample(c(0, 1), replace = TRUE, size = n)
y <- sample(c(1, 2), replace = TRUE, size = n)

The task is to create cross-table, compute Fisher's exact test and extract corresponding p-value. Here is my pipeline:
library(tidyverse)
library(gmodels)
tibble(x, y) %>%
  table() %>% 
  CrossTable(prop.r = FALSE, prop.c = FALSE, prop.t = FALSE, prop.chisq = FALSE, fisher = TRUE)

which gives the following output:
   Cell Contents
|-------------------------|
|                       N |
|-------------------------|

Total Observations in Table:  50 

             | y 
           x |         1 |         2 | Row Total | 
-------------|-----------|-----------|-----------|
           0 |        15 |        10 |        25 | 
-------------|-----------|-----------|-----------|
           1 |        13 |        12 |        25 | 
-------------|-----------|-----------|-----------|
Column Total |        28 |        22 |        50 | 
-------------|-----------|-----------|-----------|

Fisher's Exact Test for Count Data
------------------------------------------------------------
Sample estimate odds ratio:  1.375572 

Alternative hypothesis: true odds ratio is not equal to 1
p =  0.7761301 
95% confidence interval:  0.3927115 4.916038 

Alternative hypothesis: true odds ratio is less than 1
p =  0.8034681 
95% confidence interval:  0 4.094106 

Alternative hypothesis: true odds ratio is greater than 1
p =  0.388065 
95% confidence interval:  0.4686692 Inf 

Any idea how to extract *p*-value ()

I need to extract first p-value (i.e., 0.7761301). Any idea?

A:

How about this
p <- tibble(x, y) %>%
  table() %>% 
  CrossTable(prop.r = FALSE, prop.c = FALSE, prop.t = FALSE, prop.chisq = FALSE, fisher = TRUE) %>% 
  .$fisher.ts %>% 
  .$p.value

p
[1] 0.7761301

Note that you could swap out .$fisher.ts for .$fisher.gt or $.fisher.tl, depending on which one you want.

