使用 apply 或 map 函数检查多个 rvest set_values 框

Checking multiple rvest set_values boxes with an apply or map function

此题参考了的回答。

这很完美,但是为了整理代码,我正在尝试 lapply 所有需要的类别,但我无法让它工作。我尝试了以下(以及许多其他方法),但没有成功:

cats <- paste0("ctl00$ContentPlaceHolder1$CB",toupper(c('fgp','fgm','ftm','ftp','3pm','pts','reb','ast','stl','to'))) 
form_function <- function(x) {pgform[[1]][[5]][[paste0(x)]]$value <- "checked"} 
lapply(cats, form_function)

它也可以将它全部放在 set_values 函数中,但是同样,代码非常重复:

filled_form <-set_values(pgform[[1]],
                         "ctl00$ContentPlaceHolder1$DDSHOW" = "400",
                         "ctl00$ContentPlaceHolder1$CBFGM" = "checked",
                         "ctl00$ContentPlaceHolder1$CBFGP" = "checked",
                         "ctl00$ContentPlaceHolder1$CBFTM" = "checked",
                         "ctl00$ContentPlaceHolder1$CBFTP" = "checked",
                         "ctl00$ContentPlaceHolder1$CB3PM" = "checked",
                         "ctl00$ContentPlaceHolder1$CBPTS" = "checked",
                         "ctl00$ContentPlaceHolder1$CBREB" = "checked",
                         "ctl00$ContentPlaceHolder1$CBAST" = "checked",
                         "ctl00$ContentPlaceHolder1$CBSTL" = "checked",
                         "ctl00$ContentPlaceHolder1$CBTO" = "checked")

这可能是 for 循环而不是 lapply 函数的更好应用。

cats <- paste0("ctl00$ContentPlaceHolder1$CB",toupper(c('fgp','fgm','ftm','ftp','3pm','pts','reb','ast','stl','to'))) 

for (field in cats){
   pgform[[1]][[5]][[field]]$value <- "checked"
}