Reputation: 103
I have installed rtweet and when I want to collect tweets from a specific country other than USA, the package doesn´t collect any data. I have already installed the Geocoding API, Maps JavaScript API and the Geolocation API with the same API key from Google as an environment variable in R. Here are the steps I followed:
generate a google maps_token
google_maps_token<-"my key XXXXXXXXXX"
path of home directory
home_directory <- path.expand("~/")
combine with name for token
file_name <- file.path(home_directory, "google_maps_token.rds")
save token to home directory
saveRDS(google_maps_token, file = file_name)
Create an environment variable
cat(paste0("GOOGLE_MAPS_PAT=", file_name),
file = file.path(home_directory, ".Renviron"),
append = TRUE)
Visualize API key envirnomental variables
usethis::edit_r_environ()
TWITTER_PAT=/Users/xxxxxx/.rtweet_token1.rds GOOGLE_MAPS_PAT=/Users/xxxxxx//google_maps_token.rds
After saving the google api key, I restart RStudio to start collecting tweets and this happens:
load rtweet library
library(rtweet)
collect tweets from USA
tweets_USA <- search_tweets("#lang:en",
geocode = lookup_coords("USA",apikey = "google_maps_token"),
n=10, include_rts = FALSE)
Searching for tweets... Finished collecting tweets!
collect tweets from Mexico
tweets_Mexico <- search_tweets("#lang:es",
geocode = lookup_coords("mexico",apikey = "google_maps_token"),
n=10, include_rts = FALSE)
Searching for tweets... Finished collecting tweets!
Also, when I use the lookup_coords() function from rtweet package with "usa" it works
get coordinates from USA
usa <- lookup_coords("usa")
but when I try to use another address following the examples in the help section it does not collect any data
get coordinates from Brazil
bz <- lookup_coords("brazil")
But when I directly write the API key in the function it works. For security reasons I write instead "XXXXXXXXXX"
mexico_coord <- lookup_coords(address = "mexico",
components = "country:Mexico",
apikey = "XXXXXXXXXXXX")
Am I doing something wrong or do I need to install the API key in another directory? I have to say that my working directory (where I save my projects and scripts) is different from the home directory that appears as a result of running path.expand("~/")
Upvotes: 4
Views: 1911
Reputation: 103
I found a solution to get the lookup_coords() function to work properly. I simply have to use the Sys.getenv() function to call the google_maps_token.rds
generate a variable with the coordinates from Mexico
mexico_coord <- lookup_coords(address = "mexico",
components = "country:Mexico",
apikey=Sys.getenv("google_maps_token.rds"))
Afterwards I can use the search_tweet() function to collect 10 tweets from Mexico in spanish
tuits_Mex_esp_10 <- search_tweets("lang:es",
geocode = mexico_coord, n = 10)
Upvotes: 3