library(dplyr)
## 
## Attaching package: 'dplyr'
## 
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## 
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(magrittr)
library(rlist)
library(ggplot2)
library(stringr)

packageVersion("ggplot2")
## [1] '1.0.1.9000'
# 適当なデータをつっこんで、生成される変数名を返す関数
get_generated_variables <- function(stat_func){
  
  dummy_data <- data.frame(a = 1:25,
                           x_grid = rep(1:5, 5),
                           y_grid = rep(1:5, each = 5),
                           z_grid = 1:25 %% 3)
  
  stat_obj <- stat_func()
  required_aes <- stat_obj$stat$required_aes
  
  dummy_aes_args <- list()
  dummy_aes_args[required_aes] <- "a"
  if (stat_obj$stat$objname == "contour") {
    dummy_aes_args["x"] <- "x_grid"
    dummy_aes_args["y"] <- "y_grid"
    dummy_aes_args["z"] <- "z_grid"
  } else if (stat_obj$stat$objname == "ellipse")  {
    dummy_aes_args["y"] <- "y_grid"
  }
  
  dummy_aes <- do.call(aes_string, dummy_aes_args)
  
  nms <- 
    (ggplot(data = dummy_data, mapping = dummy_aes) + stat_obj) %>%
    ggplot_build %>%
    use_series(data) %>%
    extract2(1) %>%
    names
  
  setdiff(nms, c(required_aes, "PANEL"))
}

# main --------------------------------------------------------------------------------

# stat_XXXという名前の関数を取得
### stat_function, stat_vline, stat_hlineはそれぞれデフォルトで指定しなければいけない引数があるので取り除く
stat_funcs <- ls('package:ggplot2', pattern = "^stat_.+") %>%
  extract({is_in(., c("stat_function", "stat_hline", "stat_vline")) %>% `n'est pas`}) %>%
  list.map(get(., envir = asNamespace('ggplot2')))

# 必要なマッピング
required_aes <-
  stat_funcs %>%
  list.map(.()$stat$required_aes) %>%
  list.map(inset(list(), ., "★"))

# 計算される変数
generated_variables <-
  stat_funcs %>%
  list.map(get_generated_variables(.)) %>%
  list.map(inset(list(), ., "○"))
## stat_bin: binwidth defaulted to range/30. Use 'binwidth = x' to adjust this.
## stat_bindot: binwidth defaulted to range/30. Use 'binwidth = x' to adjust this.
## Smoothing formula not specified. Using: y ~ x
## geom_smooth: method="auto" and size of largest group is <1000, so using loess. Use 'method = x' to change the smoothing method.
# 結果をマージ
merged <- list.maps(
  plyr::defaults(..1, ..2),
  generated_variables,
  required_aes) %>%
  list.map(as_data_frame(.)) %>%
  bind_rows
# 見やすいようにNAには空文字を入れる
merged[is.na(merged)] <- ""

merged <- merged[,sort(colnames(merged))]
rownames(merged) <- str_replace(names(stat_funcs), "stat_", "")

library(xtable)
print(xtable(t(merged)), type = "html")
abline bin bin2d bindot binhex boxplot contour density density2d ecdf ellipse identity qq quantile smooth spoke sum summary summary_hex summary2d unique ydensity
angle
binwidth
count
countidx
density
fill
group
intercept
level
lower
middle
n
ncount
ndensity
notchlower
notchupper
order
outliers
piece
prop
quantile
radius
sample
scaled
se
size
slope
stackpos
theoretical
upper
value
violinwidth
weight
width
x
xbin
xend
xint
xmax
xmin
y
ybin
yend
yint
ymax
ymax_final
ymin
ymin_final
z