Xristos Solwmou
Xristos Solwmou

Reputation: 119

Writing a function for getting data from a URL

I'm trying to write the following function for getting Facebook data from a URL and then converting to a dataframe.

read_URL <- function(df_name,start_date,end_date,token){
  URL <-  fromJSON(paste0("https://graph.facebook.com/v5.0/me?fields=posts.limit(30).until"start_date".since"end_date"&access_token=",token,""))
  df_name <- URL$posts$data
}

Unfortunately my function does not work, and i'm not sure why. Obviously the URL works before trying to convert into the function. This is the error that i'm getting:

Error: unexpected symbol in:
"read_URL <- function(df_name,start_date,end_date,token){
  URL <-  fromJSON(paste0("https://graph.facebook.com/v5.0/me?fields=posts.limit(30).until"start_date"
>   df_name <- URL$posts$data
Error: object 'URL' not found
> }

Any help will be greatly appreciated Error: unexpected '}' in "}"

Upvotes: 0

Views: 174

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 388962

I think you are trying to construct the URL using paste. Try :

library(jsonlite)

read_URL <- function(df_name,start_date,end_date,token){
   URL <-  fromJSON(paste0('https://graph.facebook.com/v5.0/me?fields=posts.limit(30).until', start_date, ".since", end_date, "&access_token=",token))
   df_name <- URL$posts$data
   return(df_name)
}

You can also look into glue package which makes constructing of URL with parameters simple.

read_URL <- function(df_name,start_date,end_date,token){
    URL <-  fromJSON(glue::glue('https://graph.facebook.com/v5.0/me?fields=posts.limit(30).until{start_date}.since{end_date}&access_token={token}'))
    df_name <- URL$posts$data
    return(df_name)
}

Upvotes: 1

Related Questions