查询 WikipediR 时如何遍历一系列修订 ID?

How do I iterate over a range of revision ID's when querying WikipediR?

我正在使用 WikipediR 查询修订 ID 以查看下一次编辑是 'rollback' 还是 'undo'

我对标签和修订评论感兴趣,以确定编辑是否 undone/rolled 返回。 我的单个修订 ID 代码是:

library(WikipediR)

wp_diff<- revision_diff("en", "wikipedia", revisions = "883987486", properties = c("tags", "comment"), direction = "next", clean_response = T, as_wikitext=T)

然后我使用代码

将其输出转换为 df
library(dplyr)
library(tibble)
diff <- do.call(rbind, lapply(wp_diff, as.data.frame, stringasFactors=FALSE))

这对单个修订 ID 非常有用。 我想知道如何循环或映射许多修订 ID 的向量

我试过了

vec <- c("883987486","911412795")
for (i in 1:length(vec)){
wp_diff[i]<- revision_diff("en", "wikipedia", revisions = i, properties = c("tags", "comment"), direction = "next", clean_response = T, as_wikitext=T)
}

但这会产生错误 错误(函数(...,row.names = NULL,check.rows = FALSE,check.names = TRUE,: 参数表示不同的行数:1、0

当我尝试将输出列表转换为数据帧时。 有没有人有什么建议。我不确定如何进行。

谢谢。

试试下面的代码:

# Make a function
make_diff_df <- function(rev){
  wp_diff <- revision_diff("en", "wikipedia", revisions = rev,
                          properties = c("tags", "comment"), 
                          direction = "next", clean_response = TRUE, 
                          as_wikitext = TRUE)

  DF <- do.call(rbind, lapply(wp_diff, as.data.frame, stringasFactors=FALSE))

  # Define the names of the DF
  names(DF) <- c("pageid","ns","title","revisions.diff.from",
                  "revisions.diff.to","revisions.diff..",
                  "revisions.comment","revisions..mw.rollback.")
  return(DF)
}

vec <- c("883987486","911412795")

# Use do.call and lapply with the function
do.call("rbind",lapply(vec,make_diff_df))

请注意,您必须修复 make_diff_df 函数中 DF 的名称,以便 do.call 中的 "rbind" 可以工作。示例中两个版本的名称非常相似。

希望对您有所帮助