使用twitteR推文的地理位置似乎是不可避免的

时间:2018-03-03 14:34:53

标签: r twitter

我一直在使用twitteR包来发送推文(文档可以在这里找到:https://cran.r-project.org/web/packages/twitteR/twitteR.pdf)。我正在尝试查找有关特定区域中特定主题的推文,因此我使用searchTweets的地理位置字段来限制我的结果,但它返回的推文位于非常不同的位置,远远超出搜索范围。 / p>

这是我的代码。

Option Explicit

Function HasSheet(fPath As String, fName As String, sheetName As String)
    On Error Resume Next
    Dim f As String
    f = "'" & fPath & "[" & fName & "]" & sheetName & "'!R1C1"
    HasSheet = Not IsError(Application.ExecuteExcel4Macro(f))
    If Err Then HasSheet = False
    On Error GoTo 0
End Function

Sub copyNumber(rgeSrc As Range, rgeDest As Range)
    rgeDest.Value = rgeSrc.Value                ' copy number
    rgeDest.NumberFormat = rgeSrc.NumberFormat  ' copy number format
End Sub

Sub CollectMetrics()

    Dim MetricName As String, Segment As String, Ind As String, Include1 As String, Include2 As String
    Dim file As String, filePath As String, fileName As String
    Dim MonthNbr As Integer, id As Integer, numRows As Integer

    Dim sh1 As Worksheet: Set sh1 = Worksheets("Metrics")
    Dim sh2 As Worksheet: Set sh2 = Worksheets("Metadata")
    With sh1
        numRows = Range("A1", Range("A1").End(xlDown)).Rows.Count

        For id = 2 To numRows
            MetricName = Range("A" & id)
            With Application.WorksheetFunction
                Include1 = .VLookup(MetricName, sh2.Range("B2:L100"), 9, True)
                Include2 = .VLookup(MetricName, sh2.Range("B2:L100"), 10, True)
                Ind = .VLookup(MetricName, sh2.Range("B2:L100"), 2, True)
            End With
            fileName = Ind & " " & MetricName & " 2018.xlsx"

            If Include1 = "auto" And Include2 = "yes" Then
                Segment = Range("B" & id)
                file = "='https://xxx/[" & fileName & "]" & Segment
                filePath = "https://xxx/"

                If HasSheet(filePath, fileName, Segment) Then
                    MonthNbr = Month(Range("C" & id))
                    copyNumber .Range("D" & id), Range(file & "'!D" & (MonthNbr + 13))
                    copyNumber .Range("E" & id), Range(file & "'!E" & (MonthNbr + 13))
                    copyNumber .Range("F" & id), Range(file & "'!F" & (MonthNbr + 13))
                    copyNumber .Range("G" & id), Range(file & "'!G" & (MonthNbr + 13))
                    copyNumber .Range("J" & id), Range(file & "'!D" & (MonthNbr + 40))
                    copyNumber .Range("K" & id), Range(file & "'!E" & (MonthNbr + 40))
                    copyNumber .Range("L" & id), Range(file & "'!F" & (MonthNbr + 40))
                    copyNumber .Range("M" & id), Range(file & "'!G" & (MonthNbr + 40))
                    Range("O" & id) = "Values updated on " & Format(Now(), "dd-mm-yy")
                Else
                    Range("O" & id) = "Sheet available but segment missing"
                End If
            Else
                If Include2 = "no" Then
                    Range("O" & id) = "Metric set to not yet include"
                Else
                    If Include1 = "manual" Then Range("O" & id) = "Metric to be manually updated"
                End If
            End If
        Next id
    End With
    MsgBox "Update completed!"
End Sub

1 个答案:

答案 0 :(得分:1)

请注意,TweetR被“相对悠闲”地弃用,以支持rtweet。在该库的一些simple authentication setup之后,我能够做到这一点:

# A tibble: 81 x 42
   status_id  created_at          user_id  screen_name text             source 
   <chr>      <dttm>              <chr>    <chr>       <chr>            <chr>  
 1 970008327… 2018-03-03 18:49:41 3264407… pschrades21 This is crazy b… Tweetb…
 2 970008317… 2018-03-03 18:49:39 1309321… dizzyizzie… @jamescharles Y… Twitte…
 3 970008309… 2018-03-03 18:49:37 1595660… martin_dev… @winnerbowzer c… Twitte…
 4 970008300… 2018-03-03 18:49:35 45193580 mattglions  You must not ha… Twitte…
 5 970008298… 2018-03-03 18:49:34 1830410… RedLetterM… "Make a grown m… Twitte…
 6 970008282… 2018-03-03 18:49:31 28681763 kirkshould… @Colinj76 And H… Twitte…
 7 970008267… 2018-03-03 18:49:27 4561314… TheTazMan5  RT @PiperBaseba… Twitte…
 8 970008260… 2018-03-03 18:49:25 2828856… luulayy     RT @ZAK_o0: Whe… Twitte…
 9 970008256… 2018-03-03 18:49:24 1009764… Yoo_ItsIma… So glad I let t… Twitte…
10 970008240… 2018-03-03 18:49:21 57181524 jwedlund    Congratulations… Twitte…
# ... with 71 more rows, and 36 more variables: reply_to_status_id <chr>,
#   reply_to_user_id <chr>, reply_to_screen_name <chr>, is_quote <lgl>,
#   is_retweet <lgl>, favorite_count <int>, retweet_count <int>,
#   hashtags <list>, symbols <list>, urls_url <list>, urls_t.co <list>,
#   urls_expanded_url <list>, media_url <list>, media_t.co <list>,
#   media_expanded_url <list>, media_type <list>, ext_media_url <list>,
#   ext_media_t.co <list>, ext_media_expanded_url <list>, ext_media_type <lgl>,
#   mentions_user_id <list>, mentions_screen_name <list>, lang <chr>,
#   quoted_status_id <chr>, quoted_text <chr>, retweet_status_id <chr>,
#   retweet_text <chr>, place_url <chr>, place_name <chr>,
#   place_full_name <chr>, place_type <chr>, country <chr>, country_code <chr>,
#   geo_coords <list>, coords_coords <list>, bbox_coords <list>

输出:

urlencode

请注意Twitter API说“该位置优先从地理标记API中获取[原文如此],但会回归到他们的Twitter个人资料中。”在API中似乎没有办法只返回带地理标记的推文。