-1

I have this function. I'm trying to a tibble with tweets within an object.

 tuits <- function(x, y) {
  x <- search_tweets(y, n=5000, include_rts = FALSE, lang = "es",
                         since = since, until = until) %>% 
    filter(screen_name != y)
}

x is the name of the object and y is the query I want to search in Twitter.

The problem is that the objects are not accessible outside the function.

tuits(Juan, "JuanPerez")
tuits(Pedro, "PedroJimenez")

For instance if I want to execute Juan, R returns this: Error: object 'Juan' not found.

What can I do, because I need those objects accessible outside the function because I want to save all of them in a XLS.

Thanks

Phil
  • 7,287
  • 3
  • 36
  • 66

1 Answers1

1

Update: I now should have solved your problem using the ensym() and assign functions.

Essentially we want to access the global variable with the name passed to the function. In order to do this we capture its name not its contents with ensym and then we assign it using the assign function which we tell that the object we are looking for is in the global environment and has the name that we stored with ensym.

Here is a brief explanation showing how it works.

library(rlang)
 
f <- function(x) {
  x <- ensym(x)
  assign(as_string(x), 2, envir = globalenv())
}

john <- 1

f(john)

print(john)
#> [1] 2

Created on 2021-04-05 by the reprex package (v2.0.0)

For your function we would want to take this approach:

library(rlang)

tuits <- function(x, y) {
  # Get the name of the variable we want to store
  x <- ensym(x)
  tmp <- search_tweets(y, n=5000, include_rts = FALSE, lang = "es",
                         since = since, until = until) %>% 
    filter(screen_name != y)

  # Assign the value to the variable in the global environment
  assign(as_string(x), tmp, envir = globalenv())
}

tuits(Juan, "JuanPerez")

# to test
print(Juan)

Old Answer (improved on in the above section) I believe the issue here is an issue of understanding scope or environments. If an object is modifed or set within the environment used by a function or the sdcope of the function then it can only be accessed in that form within the function.

Usually the scope of a function contains the variables that are assigned inside the function statement.

Usually the way to solve this would be to return the object using return(x) and setting the function call to the object.

tuits <- function(x, y) {
  x <- search_tweets(y, n=5000, include_rts = FALSE, lang = "es",
                         since = since, until = until) %>% 
    filter(screen_name != y)
  return(x)
}

Juan <- tuits(Juan, "JuanPerez")

You could modify the object x using a superassignment (<<-) operation however this is usually not best practise. I will provide this solution for completeness sake.

Superassignment modifies the variable in the global scope. This however will assign the value to x not the object.

tuits <- function(x, y) {
  x <<- search_tweets(y, n=5000, include_rts = FALSE, lang = "es",
                         since = since, until = until) %>% 
    filter(screen_name != y)
}

tuits(Juan, "JuanPerez")
Joel Kandiah
  • 1,465
  • 5
  • 15
  • I tried using return(x). It executes the code but the object is still unaccessible outside the function :( – Alvaro López Sánchez Apr 04 '21 at 21:47
  • The object must be assigned when the function is called otherwise the object won't be stored. This is why I have the line: Juan <- tuits(Juan, "JuanPerez") It may be helpful to include more code or provide a reproducible example of the problem you have so that I can address the problem better. – Joel Kandiah Apr 04 '21 at 21:49
  • If you use the second version of the function I have constructed you should be able to use the variable without the additional assignment. – Joel Kandiah Apr 04 '21 at 21:50
  • The second option worked. I just have to save the x in a new variable. – Alvaro López Sánchez Apr 04 '21 at 22:52
  • I'm not sure I have it working as intended I'll take another look tomorrow. The first solution should be correct but I'll see if I can find a better way of dealing with the environments. – Joel Kandiah Apr 04 '21 at 22:56
  • I have updated this with a metaprogramming approach as previously mentioned. Please check that this solves your problem. Apologies for the delay in gettin back to this. – Joel Kandiah Apr 05 '21 at 21:29