You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

twitteR包无法返回用户位置信息 求技术协助

Troubleshooting Missing location Field in twitteR Package

Hey there, let’s work through why that critical location field has vanished from your twitteR results—even though your code hasn’t changed. Here’s what’s going on and how to fix it:

1. The twitteR Package is Outdated (Key Issue)

First things first: twitteR hasn’t been updated since 2019, and Twitter has rolled out major API changes (including shifting to API v2) and tightened data access rules in that time. It’s almost certain that Twitter adjusted which user profile fields are returned by default in API calls, and twitteR hasn’t been patched to keep up with this shift.

2. Verify Your Twitter Developer Permissions

Even if your code worked before, Twitter may have restricted access to fields like location. Head to your Twitter Developer Portal and check:

  • Ensure your app has at least Read-only access (or higher if required for your use case)
  • Confirm that user profile data access is enabled—location often falls under a category that now requires explicit permission to retrieve.

3. Fix Your Current twitteR Code

If you need to stick with twitteR temporarily, you can manually fetch the location field by accessing the full user object tied to each tweet. Modify your function like this:

TweetFrame <- function(searchTerm, maxTweets) { 
  tweetList <- searchTwitter(searchTerm, n = maxTweets, lang = "en") 
  # Manually pull location from each tweet's author profile
  tweetDF <- do.call("rbind", lapply(tweetList, function(tweet) {
    # Fetch the full user object for the tweet's creator
    tweet_author <- getUser(tweet$screenName)
    # Convert tweet to data frame and add the location field
    tweet_row <- as.data.frame(tweet)
    tweet_row$location <- tweet_author$location
    return(tweet_row)
  }))
  return(tweetDF[order(as.integer(tweetDF$created)), ]) 
}

The original as.data.frame(tweet) call no longer pulls in location automatically, so we’re explicitly fetching it via the getUser() method now.

4. Migrate to a Supported Package (Long-Term Fix)

Since twitteR is unmaintained, you’ll keep hitting issues like this. The official, actively updated R package for Twitter API access is rtweet. Here’s how to rewrite your workflow to get location reliably:

library(rtweet)

# Set up authentication (follow prompts to link your developer account)
auth_setup_default()

# Search for tweets and include associated user data
tweet_df <- search_tweets(
  q = searchTerm, 
  n = maxTweets, 
  lang = "en",
  include_rts = FALSE # Optional: exclude retweets if needed
)

# Extract user profile data (includes location)
user_profile_data <- users_data(tweet_df)

# Merge location into your main tweet data frame
tweet_with_location <- cbind(tweet_df, user_location = user_profile_data$location)

# Sort by creation time to match your original function's behavior
tweet_with_location <- tweet_with_location[order(as.integer(tweet_with_location$created_at)), ]

rtweet supports Twitter’s latest API, so you won’t run into random missing field issues moving forward.

内容的提问来源于stack exchange,提问作者user9044173

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 12:08:21