Search code examples
rfunctionenvironment

R Function defined in gloabl environment not visible in subsequent function


I have defined four functions. I have executed the code for each and all four appear in the global environment when I call ls(). The first two are used inside the third and this works as expected. However, when I call the third function from the fourth function I get an error message telling me that curent_month doesn't exist. (I eliminated all code from the fourth function as the failure occurs at the first statement, so the rest is not relevant.) I have always understood that any object defined in the global environment is available to any sub-environment (i.e., inside a function). Can anyone point me in the right direction?

## Function returns the most recent month having billing revenues
current_month_POSIX <- function(x){
  ## Fetch current month name for use in label below
  current_month_POSIX <- x  %>%
    filter(Year == 2020) %>%
    filter(!is.na(Billing)) %>%
    select(Month) %>%
    unique()%>%
    arrange() %>%
    tail(1) %>%
    unlist() %>%
    as_datetime()
  return(current_month_POSIX)
}

current_month_name <- function(x){
  current_month_name <- x %>%
    filter(Year == 2020) %>%
    filter(!is.na(Billing)) %>%
    select(Month, month_name) %>%
    unique()%>%
    arrange() %>%
    tail(1) %>%
    select(month_name) %>%
    substr(.,1,3)
  return(current_month_name)
}

curent_month <- function(x){
  POSIX <- current_month_POSIX(x)
  name <- current_month_name(x)
  return(list("current_month_name" = name, "current_month_POSIX" = POSIX))
}

### Function to reduce source data to clustered bar chart table
clustered_bar_data <- function(x){
  latest_month <- current_month(x)
}

Solution

  • current_month does not exist! You named your function curent_month.