R function not returning values
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.
Function not returning the output in R
You can simple call the function using lapply
as follow:
lapply(1:2,my.function)
Output:
[[1]]
a b
1 1 a
2 2 b
[[2]]
a b
1 I a
2 II b
If you would like to follow your approach then:
my.function <- function(x){
for(i in 1:length(x)){ # This will call for each element in x
if (1 %in% x) {
first.data <- data.frame(a = c(1, 2), b = c("a", "b"))
data = (first.data) # Store intermediate result to data
}
if (2 %in% x){
second.data <- data.frame(a = c("I", "II"), b = c("a", "b"))
data=rbind(data,second.data) # Row wise bind the result
}
return(data) # Return the data
}
}
my.function(x = c(1, 2))
Output:
a b
1 1 a
2 2 b
3 I a
4 II b
Function should not return a value if it doesn't exist in the function
If a free variable (one referred to but not defined) in a function is accessed then it looks in the environment where the function was defined and then in the parent environment of that and so on.
Use get("z", inherits = FALSE)
to look for z
only in the current environment or check whether it exists exists("z", inherits = FALSE)
in the current environment only.
Another possibility is to always give z
a value:
z <- if (length(x) && length(y) && x + y > 10) x + y
In that case z
will have the value NULL if the condition is false because the default else
leg is NULL. Returning NULL invisibly would be about as close as you can get to not returning a value.
xyz2 <- function(x = NULL, y = NULL) {
z <- if (length(x) && length(y) && x + y > 10) x + y
# other processing can go here
if (is.null(z)) invisible(z) else z
}
xyz2(1, 2)
xyz2(5, 9)
## [1] 14
R function with no return value
You need to understand the difference between a function returning a value, and printing that value. By default, a function returns the value of the last expression evaluated, which in this case is the assignment
arg <- arg + 3
(Note that in R, an assignment is an expression that returns a value, in this case the value assigned.) This is why data <- derp(500)
results in data
containing 503.
However, the returned value is not printed to the screen by default, unless you isolate the function's final expression on its own line. This is one of those quirks in R. So if you want to see the value:
derp <- function(arg)
{
arg <- arg + 3
arg
}
or just
derp <- function(arg)
arg + 3
R: return list of objects in my function does not return expected list
You are missing the {}
to define the function. I guess thats why R didn't recognise that you want the output inside your function.
You should try.
function_name <- function(arg_1, arg_2, ...) {
Function body
}
R: Function without arguments and without return value?
Combining the advice from Rushabh and Gregor this would be a solution:
MyFunction = function(){
x = rnorm(10)
x <<- x^2
print("and dinosaurs are scary")
y = TRUE
return(invisible())
}
Thanks for helping!
Related Topics
Handling Dates When We Switch to Daylight Savings Time and Back
How to Append Rows to an R Data Frame
Mean of a Column in a Data Frame, Given the Column's Name
What Is Integer Overflow in R and How Can It Happen
Different Legends and Fill Colours for Facetted Ggplot
Remove Duplicate Column Pairs, Sort Rows Based on 2 Columns
How to Reorder Data.Table Columns (Without Copying)
Add Extra Level to Factors in Dataframe
Is There a Vectorized Parallel Max() and Min()
Why Is Using '<<-' Frowned Upon and How to Avoid It
Detach All Packages While Working in R
Change the Default Colour Palette in Ggplot
Apply a Function Over Groups of Columns
R Extract Rows Where Column Greater Than 40
What's the Difference Between Integer Class and Numeric Class in R