R:如何存储 foreach 循环的输出?

R: how to store the output of a foreach loop?

我试图在 R 中存储 foreach 循环的输出。我使用的代码如下:

tweets_all = list()
foreach(i = 1:5, .packages = c("magrittr", "foreach", "academictwitteR")) %dopar% {
  tweets_all[[i]] = bind_tweets(data_path = paste0("./data_new/data", i), output_format = "tidy")
  }

所以基本上我遵循存储 for 循环输出的逻辑,首先创建一个空列表 tweets_all,然后将每次迭代的输出放入该列表。但是,输出没有存储在 tweets_all 列表中,tweets_all 列表仍然是空的。而上面代码的输出是这样自动打印出来的:

[[1]]
# A tibble: 17,854 x 31
   tweet_id      user_username text  possibly_sensit~ conversation_id author_id source lang  created_at user_url user_protected user_created_at user_profile_im~
   <chr>         <chr>         <chr> <lgl>            <chr>           <chr>     <chr>  <chr> <chr>      <chr>    <lgl>          <chr>           <chr>           
 1 146544706849~ kwcpainter    RT @~ FALSE            14654470684960~ 349969646 Tweet~ en    2021-11-2~ ""       FALSE          2011-08-07T00:~ https://pbs.twi~
 2 146544705236~ Deepdiver600  RT @~ FALSE            14654470523604~ 14195830~ Twitt~ en    2021-11-2~ ""       FALSE          2021-07-26T08:~ https://pbs.twi~
 3 146544705200~ Jabroni_Niner RT @~ FALSE            14654470520079~ 468137468 Twitt~ en    2021-11-2~ ""       FALSE          2012-01-19T06:~ https://pbs.twi~
 4 146544704385~ St_Diogenes   RT @~ FALSE            14654470438585~ 13997184~ Twitt~ en    2021-11-2~ ""       FALSE          2021-06-01T13:~ https://pbs.twi~
 5 146544703368~ kristinkgl    RT @~ FALSE            14654470336874~ 10936760~ Twitt~ en    2021-11-2~ ""       FALSE          2013-01-16T00:~ https://pbs.twi~
 6 146544702777~ ivory319      RT @~ FALSE            14654470277733~ 48449979~ Twitt~ en    2021-11-2~ ""       FALSE          2016-01-25T10:~ https://pbs.twi~
 7 146544702475~ VeritasTB2    RT @~ FALSE            14654470247535~ 85285252~ Twitt~ en    2021-11-2~ ""       FALSE          2017-04-14T11:~ https://pbs.twi~
 8 146544702070~ scot79033947  RT @~ FALSE            14654470207061~ 13556114~ Twitt~ en    2021-11-2~ ""       FALSE          2021-01-30T20:~ https://pbs.twi~
 9 146544701752~ ukcitizen007  RT @~ FALSE            14654470175267~ 12242558~ Twitt~ en    2021-11-2~ ""       FALSE          2020-02-03T08:~ https://pbs.twi~
10 146544701752~ lucashartley~ RT @~ FALSE            14654470175224~ 14450708~ Twitt~ en    2021-11-2~ ""       FALSE          2021-10-04T16:~ https://pbs.twi~
# ... with 17,844 more rows, and 18 more variables: user_description <chr>, user_verified <lgl>, user_name <chr>, user_location <chr>,
#   user_pinned_tweet_id <chr>, retweet_count <int>, like_count <int>, quote_count <int>, user_tweet_count <int>, user_list_count <int>,
#   user_followers_count <int>, user_following_count <int>, sourcetweet_type <chr>, sourcetweet_id <chr>, sourcetweet_text <chr>, sourcetweet_lang <chr>,
#   sourcetweet_author_id <chr>, in_reply_to_user_id <chr>

[[2]]
# A tibble: 6,741 x 31
   tweet_id  user_username text  created_at conversation_id source author_id lang  possibly_sensit~ user_profile_im~ user_created_at user_verified user_location
   <chr>     <chr>         <chr> <chr>      <chr>           <chr>  <chr>     <chr> <lgl>            <chr>            <chr>           <lgl>         <chr>        
 1 14312213~ gpalmen       RT @~ 2021-08-2~ 14312213388286~ Twitt~ 22672330  en    FALSE            https://pbs.twi~ 2009-03-03T20:~ FALSE         "Mook"       
 2 14312213~ loyddaroyd    RT @~ 2021-08-2~ 14312213222108~ TwitP~ 255531287 en    FALSE            https://pbs.twi~ 2011-02-21T15:~ FALSE         "FL, USA"    
 3 14312213~ realdhbenson  RT @~ 2021-08-2~ 14312213148538~ Twitt~ 89227576~ en    FALSE            https://pbs.twi~ 2017-08-01T06:~ FALSE         "British Col~
 4 14312213~ AliceBallard~ RT @~ 2021-08-2~ 14312213037349~ Twitt~ 70978902~ en    FALSE            https://pbs.twi~ 2016-03-15T17:~ FALSE         " USA"       
 5 14312213~ scarredbushi~ RT @~ 2021-08-2~ 14312213004758~ Twitt~ 75023725~ en    FALSE            https://pbs.twi~ 2016-07-05T07:~ FALSE         "Hinata Spri~
 6 14312212~ Jamie04042155 RT @~ 2021-08-2~ 14312212986598~ Twitt~ 11001545~ en    FALSE            https://pbs.twi~ 2019-02-25T22:~ FALSE          NA          
 7 14312212~ BertvantZand  RT @~ 2021-08-2~ 14312212906990~ Twitt~ 12808028~ en    FALSE            https://pbs.twi~ 2013-03-19T15:~ FALSE          NA          
 8 14312212~ jussaguy2u    RT @~ 2021-08-2~ 14312212745424~ Twitt~ 23021090  en    FALSE            https://pbs.twi~ 2009-03-06T03:~ FALSE         "Nunya"      
 9 14312212~ sandrabloomB  RT @~ 2021-08-2~ 14312212665272~ Twitt~ 40407170~ en    FALSE            https://pbs.twi~ 2015-10-25T18:~ FALSE         "Global"     
10 14312212~ b_indaclouds  RT @~ 2021-08-2~ 14312212643881~ Twitt~ 525362634 en    FALSE            https://pbs.twi~ 2012-03-15T13:~ FALSE         "NC"         
# ... with 6,731 more rows, and 18 more variables: user_name <chr>, user_protected <lgl>, user_url <chr>, user_description <chr>, user_pinned_tweet_id <chr>,
#   retweet_count <int>, like_count <int>, quote_count <int>, user_tweet_count <int>, user_list_count <int>, user_followers_count <int>,
#   user_following_count <int>, sourcetweet_type <chr>, sourcetweet_id <chr>, sourcetweet_text <chr>, sourcetweet_lang <chr>, sourcetweet_author_id <chr>,
#   in_reply_to_user_id <chr>

[[3]]
# A tibble: 6,439 x 31
   tweet_id     user_username text  created_at source possibly_sensit~ conversation_id lang  author_id in_reply_to_use~ user_name user_verified user_profile_im~
   <chr>        <chr>         <chr> <chr>      <chr>  <lgl>            <chr>           <chr> <chr>     <chr>            <chr>     <lgl>         <chr>           
 1 13961156432~ Adaya77       "To ~ 2021-05-2~ Twitt~ FALSE            13961156432298~ en    16395830  NA               "Adaya77" FALSE         https://pbs.twi~
 2 13961153462~ PeaknikMicki  "CDC~ 2021-05-2~ Twitt~ FALSE            13961153462059~ en    88940731~ NA               "Peaknik~ FALSE         https://pbs.twi~
 3 13961146269~ benstu37      "RT ~ 2021-05-2~ Twitt~ FALSE            13961146269376~ en    741552206 NA               "BOB the~ FALSE         https://pbs.twi~
 4 13961144860~ WorthingtonE~ "RT ~ 2021-05-2~ Twitt~ FALSE            13961144860339~ en    70883242  NA               "Proneto~ FALSE         https://pbs.twi~
 5 13961143524~ T2Rantor      "RT ~ 2021-05-2~ Twitt~ FALSE            13961143524539~ en    10372470~ NA               "Rantor"  FALSE         https://pbs.twi~
 6 13961122762~ marina_sapir  "Cyc~ 2021-05-2~ Twitt~ FALSE            13961122762776~ en    98851699~ NA               "Marina ~ FALSE         https://pbs.twi~
 7 13961121089~ Yorksceptic   "Two~ 2021-05-2~ Twitt~ FALSE            13961121089039~ en    99938813  NA               "Sir Ian" FALSE         https://pbs.twi~
 8 13961116823~ SouthILdream~ "RT ~ 2021-05-2~ Twitt~ FALSE            13961116823012~ en    83728699~ NA               "Red Red~ FALSE         https://pbs.twi~
 9 13961101930~ TonyGreyMan   "RT ~ 2021-05-2~ Twitt~ FALSE            13961101930214~ en    11766208~ NA               "Tony"    FALSE         https://pbs.twi~
10 13961099635~ fgiangerelli  "RT ~ 2021-05-2~ Twitt~ FALSE            13961099635844~ en    81932295~ NA               "MAGA Fr~ FALSE         https://pbs.twi~
# ... with 6,429 more rows, and 18 more variables: user_created_at <chr>, user_description <chr>, user_protected <lgl>, user_url <chr>, user_location <chr>,
#   user_pinned_tweet_id <chr>, retweet_count <int>, like_count <int>, quote_count <int>, user_tweet_count <int>, user_list_count <int>,
#   user_followers_count <int>, user_following_count <int>, sourcetweet_type <chr>, sourcetweet_id <chr>, sourcetweet_text <chr>, sourcetweet_lang <chr>,
#   sourcetweet_author_id <chr>

[[4]]
# A tibble: 5,067 x 31
   tweet_id            user_username   text     created_at author_id possibly_sensit~ conversation_id source lang  in_reply_to_use~ user_protected user_location
   <chr>               <chr>           <chr>    <chr>      <chr>     <lgl>            <chr>           <chr>  <chr> <chr>            <lgl>          <chr>        
 1 1404971960656961539 VronikaSDB1     "RT @Yv~ 2021-06-1~ 572861879 FALSE            14049719606569~ Twitt~ en    NA               FALSE          Monaco       
 2 1404959613712277507 FranceBis92     "RT @Yv~ 2021-06-1~ 10330186~ FALSE            14049596137122~ Twitt~ en    NA               FALSE          France       
 3 1404947871963783173 NoelSully11     "RT @Ri~ 2021-06-1~ 78233909~ FALSE            14049478719637~ Twitt~ en    NA               FALSE          United States
 4 1404946634967031811 RiekiErasmusAtt "RT @tn~ 2021-06-1~ 10070835~ FALSE            14049466349670~ Twitt~ en    NA               FALSE          Roodepoort/H~
 5 1404946589911715841 RiekiErasmusAtt "Can th~ 2021-06-1~ 10070835~ FALSE            14049465899117~ Twitt~ en    NA               FALSE          Roodepoort/H~
 6 1404946316858478597 jasperlionel    "RT @tn~ 2021-06-1~ 99329128~ FALSE            14049463168584~ Twitt~ en    NA               FALSE          NA           
 7 1404942539992481794 CasseJ2         "RT @Yv~ 2021-06-1~ 13936841~ FALSE            14049425399924~ Twitt~ en    NA               FALSE          NA           
 8 1404940933125181443 mtz_holly       "RT @Yv~ 2021-06-1~ 13660867~ FALSE            14049409331251~ Twitt~ en    NA               FALSE          NA           
 9 1404939998722412546 jean2florette   "first-~ 2021-06-1~ 84418207~ FALSE            14049399987224~ Twitt~ en    NA               FALSE          NA           
10 1404939283258089484 corijkon        "RT @Yv~ 2021-06-1~ 11078537~ FALSE            14049392832580~ Twitt~ en    NA               FALSE          NA           
# ... with 5,057 more rows, and 19 more variables: user_description <chr>, user_profile_image_url <chr>, user_url <chr>, user_verified <lgl>, user_name <chr>,
#   user_created_at <chr>, user_pinned_tweet_id <chr>, retweet_count <int>, like_count <int>, quote_count <int>, user_tweet_count <int>, user_list_count <int>,
#   user_followers_count <int>, user_following_count <int>, sourcetweet_type <chr>, sourcetweet_id <chr>, sourcetweet_text <chr>, sourcetweet_lang <chr>,
#   sourcetweet_author_id <chr>

[[5]]
# A tibble: 12,386 x 31
   tweet_id            user_username   text  created_at lang  conversation_id possibly_sensit~ author_id source user_protected user_description user_profile_im~
   <chr>               <chr>           <chr> <chr>      <chr> <chr>           <lgl>            <chr>     <chr>  <lgl>          <chr>            <chr>           
 1 1490748491282452480 CarolynClburr   RT @~ 2022-02-0~ en    14907484912824~ FALSE            84501053~ Twitt~ FALSE          ""               https://pbs.twi~
 2 1490748475381719040 kathrine_munk   RT @~ 2022-02-0~ en    14907484753817~ FALSE            13788268~ Twitt~ FALSE          "I love aquariu~ https://pbs.twi~
 3 1490748465894367234 zenjk0          RT @~ 2022-02-0~ en    14907484658943~ FALSE            21422783  Twitt~ FALSE          "Slavic slav. S~ https://pbs.twi~
 4 1490748449490411524 ArcticTidalSurg RT @~ 2022-02-0~ en    14907484494904~ FALSE            71129108~ Twitt~ FALSE          "Drugs or vacci~ https://pbs.twi~
 5 1490748446734696449 napoliangel10   RT @~ 2022-02-0~ en    14907484467346~ FALSE            480682459 Twitt~ FALSE          "I'm not fuckin~ https://pbs.twi~
 6 1490748438463627269 brights_lights  RT @~ 2022-02-0~ en    14907484384636~ FALSE            14741131~ Twitt~ FALSE          "« Le non-sens ~ https://pbs.twi~
 7 1490748437595361282 funkymonkey374  RT @~ 2022-02-0~ en    14907484375953~ FALSE            14099191~ Twitt~ FALSE          ""               https://pbs.twi~
 8 1490748435607269378 TonyGreyMan     RT @~ 2022-02-0~ en    14907484356072~ FALSE            11766208~ Twitt~ FALSE          "The Grey Man. ~ https://pbs.twi~
 9 1490748427914948609 7_firefly       RT @~ 2022-02-0~ en    14907484279149~ FALSE            13922322~ Twitt~ FALSE          "<U+2640><U+FE0F>Patriot #TRUM~ https://pbs.twi~
10 1490748423921934337 Gingatossa1     RT @~ 2022-02-0~ en    14907484239219~ FALSE            242780839 Twitt~ FALSE          "Vegan \U0001f4~ https://pbs.twi~
# ... with 12,376 more rows, and 19 more variables: user_verified <lgl>, user_created_at <chr>, user_url <chr>, user_name <chr>, user_location <chr>,
#   user_pinned_tweet_id <chr>, retweet_count <int>, like_count <int>, quote_count <int>, user_tweet_count <int>, user_list_count <int>,
#   user_followers_count <int>, user_following_count <int>, sourcetweet_type <chr>, sourcetweet_id <chr>, sourcetweet_text <chr>, sourcetweet_lang <chr>,
#   sourcetweet_author_id <chr>, in_reply_to_user_id <chr>

有没有办法将每次迭代的输出存入空列表?如果您知道如何处理这个问题,请给我一些建议。感谢您的帮助!

您可以简单地这样做:

tweets_all = foreach(i = 1:5, .packages = c("magrittr", "foreach", "academictwitteR")) %dopar% {
  bind_tweets(data_path = paste0("./data_new/data", i), output_format = "tidy")
}