0

I created a simple function.

kelvin_to_celsius <- function(temp_K) {
  temp_C <- temp_K - 273.15
  return(temp_C)
}

I put it in here:

C:\Users\Excel\Documents\kelvin_to_celsius.r

getwd()
[1] "C:/Users/Excel/Documents"

I'm calling the function and getting an error.

kelvin_to_celsius(150)
Error in kelvin_to_celsius(150) : 
  could not find function "kelvin_to_celsius"

Am I missing something here???

ASH
  • 20,759
  • 19
  • 87
  • 200
  • 2
    Have you `source`'d that file? – divibisan Feb 15 '19 at 18:43
  • Conceptually, think about what a mess you'd have if every variable, function, etc were present in your current environment just because they were defined in files within your working directory. – camille Feb 15 '19 at 19:15

1 Answers1

1

you probably need to source it with source("kelvin_to_celsius.r")

# > kelvin_to_celsius(150)
# Error in kelvin_to_celsius(150) : 
#   could not find function "kelvin_to_celsius"
# > source("kelvin_to_celsius.r")
# > kelvin_to_celsius(150)
# [1] -123.15
  • Yeah, source worked. I haven't used any R functions in a few months; just worked with Python recently. I don't remember every having to source a UDF. Is that a new thing now? Thanks for the help. – ASH Feb 15 '19 at 19:21
  • I don't think `source` is a new thing. cheers! – Joseph Fann Feb 15 '19 at 19:41