4

I am writing my first R function.

IMDBmovierating <- function(movie){
  link <- paste("http://www.omdbapi.com/?t=", movie, "&y=&plot=short&r=json", `sep = "")`
  jsonData <- fromJSON(link)
  df <- data.frame(jsonData)
}

And then nothing happens. Suspect it has something to do with return being needed. Not sure how I would write this.

4
  • 2
    just write the last line as data.frame(jsonData) instead of df <-data.frame(jsonData) Commented Apr 10, 2015 at 20:39
  • 4
    Well, "nothing happens" isn't quite accurate. I'm sure your code runs, but the last line of your function is an assignment. Assignments do have a return value but it is invisible by default so nothing is printed. If you were save the result to a variable,and print() that variable, you should be able to see the result. eg: f<-function(a) {b<-a+1}; x<-f(4); print(x) Commented Apr 10, 2015 at 20:53
  • @MrFlick, any reference to a technical explanation for this? I wonder what is happening when functions end with for example: x <- 1; x, x <- 1 or (x <- 1). And what about = operator. Commented Apr 6, 2022 at 21:31
  • 1
    @DavorJosipovic There is a decent discussion here. adv-r.hadley.nz/functions.html#invisible. The <- and = will behave identically, they both perform assignment. Whey you run an assignment expression, it will return invisibly. But when you run x or use () both of those are separate expressions so the invisible part will not apply and you'll see the value. If you need more details, it would probably be best to open your own new question. Commented Apr 6, 2022 at 21:39

3 Answers 3

7

To return df, simply write return(df):

IMDBmovierating <- function(movie){
  link <- paste("http://www.omdbapi.com/?t=", movie, "&y=&plot=short&r=json", sep = "")
  jsonData <- fromJSON(link)
  df <- data.frame(jsonData)
  return(df)
}

or, even simpler in this case, omit the last assignment:

IMDBmovierating <- function(movie){
  link <- paste("http://www.omdbapi.com/?t=", movie, "&y=&plot=short&r=json", sep = "")
  jsonData <- fromJSON(link)
  data.frame(jsonData)
}

If the last expression evaluates to a result object, as data.frame(..) does, then this gets the return object of the enclosing expression and the explicit return statement may be omitted.

edit: and remove the back-ticks before sep and after you closing parenthesis

edit2: Of course MrFlick's comment is correct: the only thing really wrong with your code are the back-ticks that probably are simply a typo here on the site. Even the assignment produces the assigned value as a result object, but it is invisible. Hence, you can assign it, but it is not automatically printed on the console.

Sign up to request clarification or add additional context in comments.

Comments

5

You simply need to evaluate the object at the end of your function so it will return a value. See the simple example below:

funA <- function(x) {
    a <- x
}

funB <- function(x) {
    b <- x
    b
}

funA(1) # prints nothing
funB(1) # prints 'b'
[1] 1

EDIT:

As @MrFlick points out, both funA and funB return the evaluation of the last expression, but funA will not print anything. However, if you assign the output of funA(1) an object, that object will produce the value 1:

z <- funA(1)
z
[1] 1
z == funB(1)
[1] TRUE

The moral of the story is that you either need to assign the output of IMDBmovierating to an object, or explicitly evaluate df at the end of the function.

Comments

0

Looks like you just had a few typos.

Try this and dont forget to include your library to help people out when answering you. :)

library(RJSONIO)    
IMDBmovierating <- function(movie){
link <- paste("http://www.omdbapi.com/?t=", movie,"&y=&plot=short&r=json", sep = "")
    jsonData <- fromJSON(link)
    df <- data.frame(jsonData)
}

test <- IMDBmovierating(1984)
test

2 Comments

I'm surprised that this works.I always assumed that assignment did not return anything and that short of adding a return(df) line at the end, this function wouldn't work as expected. Seems my assumption was wrong.
I admit its rather poor form but I use it at times when I need a function just to do one thing and return a variable to the Global Env for use in multiple other applications.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.