如何从这些对象的列表中创建对象属性的新列表?

How to create new list of an object attribute from a list of those objects?

我正在与 git2r 合作,想创建一些关于项目 activity 的基本统计信息。

git2r returns 所有作为 S4 对象列表提交。下面我展示了第一个对象的结构:

> library(git2r)
> repo <- repository('/Users/swain/Dropbox/projects/from-github/brakeman')
> last3 <- commits(repo, n=3)
> str(last3)
List of 3
 $ :Formal class 'git_commit' [package "git2r"] with 6 slots
  .. ..@ sha      : chr "f7746c21846d895bd90632df5a2366381ced77d9"
  .. ..@ author   :Formal class 'git_signature' [package "git2r"] with 3 slots
  .. .. .. ..@ name : chr "Justin"
  .. .. .. ..@ email: chr "presidentbeef@users.noreply.github.com"
  .. .. .. ..@ when :Formal class 'git_time' [package "git2r"] with 2 slots
  .. .. .. .. .. ..@ time  : num 1.5e+09
  .. .. .. .. .. ..@ offset: num -420
  .. ..@ committer:Formal class 'git_signature' [package "git2r"] with 3 slots
  .. .. .. ..@ name : chr "GitHub"
  .. .. .. ..@ email: chr "noreply@github.com"
  .. .. .. ..@ when :Formal class 'git_time' [package "git2r"] with 2 slots
  .. .. .. .. .. ..@ time  : num 1.5e+09
  .. .. .. .. .. ..@ offset: num -420
  .. ..@ summary  : chr "Merge pull request #1056 from presidentbeef/hash_access_interpolation_performance_improvements"
  .. ..@ message  : chr "Merge pull request #1056 from presidentbeef/hash_access_interpolation_performance_improvements\n\nHash access i"| __truncated__
  .. ..@ repo     :Formal class 'git_repository' [package "git2r"] with 1 slot
  .. .. .. ..@ path: chr "/Users/swain/Dropbox/projects/from-github/brakeman"

我四处寻找一种方法来将所有对象中的一个槽提取到一个列表中。例如,对于列表 last3 中的所有 S4 对象,我想将 author 拉入这个新列表。请注意,这里有对象的嵌套,因此我可能想从位于顶部对象的插槽中的对象上的某些内容创建一个列表。

最终我想开始创建各个领域的图表和摘要。例如,按星期几的提交条形图;提交者的消息长度箱线图;像这样的东西。将插槽转换为列表或向量是错误的方法吗? (编辑:s/histogram/bar 图表/,doh)

怎么样

lapply(last3,function(x) data.frame(author = x@author@name, email = x@author@email))

这是一个tidyverse solution to what you're trying to achieve. Jenny Bryan has a nice set of introductory documents on how to use purrr (and other packages) for this sort of task: https://jennybc.github.io/purrr-tutorial/

library(git2r)
library(dplyr)
library(ggplot2)
library(purrr)
library(lubridate)

options(stringsAsFactors = FALSE)

repo <- repository("/git-repos/brakeman/")

# Get relevant bits out of the list
analysis_df <-
    repo %>%
        commits(n = 50) %>%
        map_df(
            ~ data.frame(
                name    = .@author@name,
                date    = .@author@when@time %>% as.POSIXct(origin="1970-01-01"),
                message = .@message
            )
        )

# A histogram of commits by day of the week;
analysis_df %>%
    mutate(weekday = weekdays(date)) %>%
    group_by(weekday) %>%
    tally() %>%
    ggplot(aes(x = weekday, y = n)) +
    geom_bar(stat = "identity")

# box plots of the message length by committer
analysis_df %>%
    mutate(message_length = nchar(message)) %>%
    group_by(name) %>%
    summarise(mean_message_length = mean(message_length)) %>% 
    ggplot(aes(x = name, y = mean_message_length)) +
    geom_bar(stat = "identity")