0

EDITED: I have the code below. Essentially, it is grabbing an image from a data frame for a function. I couldn't quite figure out what is the best way to do a for loop or if there is a better option for this. The end goal is to arrive at DF_ALL. The data frame has over 100s of images. So, the solution below is not the most elegant.

# Part 1, Get some profile images from Twitter.

library(rtweet) #I'm not including the key here.

# Get a list of IDs
followers <- get_followers("TWITTER_HANDLE_HERE", n = 10)

# Get the complete Twitter profile for the 10 users
follower_profiles <- lookup_users(followers)

# Create new variable profile_full_url for image API 
follower_profiles$profile_full_url <- gsub("normal", "400x400", follower_profiles$profile_image_url)

# Part 2, Proceed image with API
library(Roxford)
visionkey = 'KEY_FROM_GOOGLE' 

# Run image tag function on the first image
DF1 <- getTaggingResponseURL(follower_profiles$profile_full_url[1], visionkey)
DF1$twitter_url <- follower_profiles$profile_full_url[1]

# Here is the result (Notice how it is show 3 rows. I don't why it is. Would prefer to have 1 row per image)
# name        confidence width height format                                                                 twitter_url
# tags      wall 0.999090671539307  <NA>   <NA>   <NA> http://pbs.twimg.com/profile_images/9999999999_400x400.jpg
# requestId <NA>              <NA>  <NA>   <NA>   <NA> http://pbs.twimg.com/profile_images/9999999999_400x400.jpg
# metadata  <NA>              <NA>   400    400   Jpeg http://pbs.twimg.com/profile_images/9999999999_400x400.jpg

# The problem is... there could be 100+ of images.
# I feel that a for loop could potentially be the solution. 

DF1 <- getTaggingResponseURL(follower_profiles$profile_full_url[1], visionkey)
DF1$twitter_url <- follower_profiles$profile_full_url[1]

DF2 <- getTaggingResponseURL(follower_profiles$profile_full_url[2], visionkey)
DF2$twitter_url <- follower_profiles$profile_full_url[2]

DF3 <- getTaggingResponseURL(follower_profiles$profile_full_url[3], visionkey)
DF3$twitter_url <- follower_profiles$profile_full_url[3]

DF_ALL<-rbind(DF1,DF2,DF3)
Samuel
  • 155
  • 1
  • 10
  • Could you provide some example data? This seems to be easily solved with `lapply`, but without seeing how your data looks like, it's hard to provide a solution. – LAP Jan 25 '17 at 08:42
  • Try: `sapply(1:5, function(x) getTaggingResponseURL(follower_profiles$profile_full_url[x], visionkey))` – Roman Jan 25 '17 at 08:47
  • and then something similar to `do.call(rbind, list(BOD, BOD))` http://stackoverflow.com/questions/2851327/convert-a-list-of-data-frames-into-one-data-frame?noredirect=1&lq=1 – jogo Jan 25 '17 at 09:08

2 Answers2

3

The function for one URL.

foo <- function(x) {
  DF <- getTaggingResponseURL(x, visionkey)
  DF$twitter_url <- x
  DF
}

Apply the foo on vector follower_profiles$profile_full_url and rbind the result.

DF_ALL <- do.call(rbind, lapply(follower_profiles$profile_full_url, foo))

Probably this one would work as well, but I am not sure as I do not know the structure of data.

DF_ALL <- sapply(follower_profiles$profile_full_url, foo)
djhurio
  • 5,437
  • 4
  • 27
  • 48
  • 1
    As an alternative to `do.call('rbind')` you can also use `bind_rows()` from the `dplyr` package. – Paul Hiemstra Jan 25 '17 at 09:12
  • Yes, and `rbindlist` from `data.table`. I did not wanted to load extra packages in this case. – djhurio Jan 25 '17 at 09:33
  • I had a play around with it. This function seems to work for my other problem. This function getTaggingResponseURL() returns 3 rows per image. But, let's assume that this function actually returns 1 row. My new issue is that getTaggingResponseURL() can return nothing i.e. unable to identify the image. So, I need something to return a blank row and to return the DF$twitter_url so that I know which url fails. – Samuel Jan 26 '17 at 12:52
2

Try this:

for (i in 1:nrow(follower_profiles)) {
    DF <- getTaggingResponseURL(follower_profiles$profile_full_url[i], visionkey)
    DF$twitter_url <- follower_profiles$profile_full_url[i]
    if (i == 1) {
        DF_ALL <- DF
    } else {
        DF_ALL <- rbind(DF_ALL,DF)
    }
}
Rodrigo
  • 4,706
  • 6
  • 51
  • 94
  • I had a play around with it. This function seems to work for my other problem. This function getTaggingResponseURL() returns 3 rows per image. But, let's assume that this function actually returns 1 row. My new issue is that getTaggingResponseURL() can return nothing i.e. unable to identify the image. So, I need something to return a blank row and to return the DF$twitter_url so that I know which url fails. – Samuel Jan 26 '17 at 12:42
  • @Samuel Use `class(DF)` to see what "nothing" is this. Then you may try a `if (DF == what_you_call_nothing)` to make a separate treatment. It's hard to help more without a complete, reproducible example. – Rodrigo Jan 26 '17 at 12:56