Write tweets from rtweets package to csv

后端 未结 1 769
一个人的身影
一个人的身影 2020-12-18 17:17

I\'m unable to write tweets from search_tweet() in \'rtweet\' package to csv. It throws the following error:

Here\'s a link to the question I previously asked, that

相关标签:
1条回答
  • 2020-12-18 17:29

    The rtweet package has a function to export to CSV called write_as_csv but for some reason does not expose the append= option. You can take the code of that function and change it to add an append option. For example

    write_as_csv2 <- function(x, file_name,
                             prepend_ids = TRUE,
                             na = "",
                             fileEncoding = "UTF-8", append=FALSE) {
      ## to minimize rounding
      op <- options()
      on.exit(options(op))
      options(scipen = 14, digits = 22)
    
      ## validate inputs
      stopifnot(is.data.frame(x), is.character(file_name), length(file_name) == 1L)
      if (!grepl("\\.csv$", file_name)) {
        file_name <- paste0(file_name, ".csv")
      }
      ## flatten data
      x <- flatten(x)
      if (prepend_ids) {
        x <- prepend_ids(x)
      }
      utils::write.table(x, file_name, row.names = FALSE, na = na,
        fileEncoding = fileEncoding, append=append, sep=",", dec=".", qmethod="double")
    
      # or
      # readr::write_csv(x, file_name, append =  append)
    }
    environment(write_as_csv2) <- asNamespace("rtweet")
    

    Then you can call it like

    write_as_csv2(comments, "comments.csv", append =  TRUE)
    
    0 讨论(0)
提交回复
热议问题