climr icon indicating copy to clipboard operation
climr copied to clipboard

getting error variables[["Code"]] : object of type 'closure' is not subsettable

Open cour10eygrace opened this issue 1 year ago • 5 comments

I get this error when trying to run the downscale function at any point in an R session after I have already run it previously. So it works the first time but when running the exact same code again throws this error. Restarting my R session and re-running solves the problem but is clunky/annoying, Thanks!

cour10eygrace avatar Jun 06 '24 00:06 cour10eygrace

@cour10eygrace please provide a reprex. does this happen with all your scripts or is it only in one script?

cmahony avatar Jun 06 '24 00:06 cmahony

It happens when I run it in the original script or when I source that script from another - but it happens intermittently so it's hard to pin down what's going on

my_points = data.frame(lon= -116:-120, lat=49:53, elev=500:504, id= c('a','b', 'c', 'd', 'e'), ProjectID= c('l','m', 'n', 'o', 'p'))

clim_dat <- downscale( xyz = my_points, which_refmap = "auto", vars = c("PPT", "MAT"))

cour10eygrace avatar Jun 06 '24 00:06 cour10eygrace

I can't reproduce the error with this reprex. perhaps you need to install the latest version of climr? remotes::install_github("bcgov/climr")

cmahony avatar Jun 07 '24 00:06 cmahony

@cour10eygrace as discussed last week it may be because you have an object in your script called "variables" that is assigned after the downscale call. put your whole script into this issue and i'll have a look.

cmahony avatar Jun 16 '24 21:06 cmahony

ok full script here- thanks!

#libraries----
library(climr)
library(tidyverse)
library(terra)
library(data.table)

#provide a data.frame or data.table of point coordinates, IDs and elevation
BEC_data<-readRDS("data/BEC_data.rds")

#pull out plot data 
plot_dat<-BEC_data$env #70,547 plots

#make dataframe for extracting climate data
my_points <- select(plot_dat, Longitude, Latitude, Elevation, PlotNumber, ProjectID) %>%
  rename(lon = Longitude,   lat = Latitude, 
  elev = Elevation, id = PlotNumber)%>%
  na.omit() #remove NAs

#look at options 
#what to select here?
list_obs_periods()
list_obs_years()
list_vars() 

vars<-climr::variables #look up table for vars 


#climr query for the historic data - only using 1961-1990 for now 
#how to check the resolution of these data? 

clim_dat <- downscale(
  xyz = my_points, which_refmap = "auto", 
  #historic_period = "2001_2020", 
  #historic_ts = C(1961:1990),
  #gcm_models = c("GFDL-ESM4", "EC-Earth3"), # specify two global climate models
  #ssp = c("ssp370", "ssp245"), # specify two greenhouse gas concentration scenarios
  #gcm_period = c("2001_2020", "2041_2060"), # specify two 20-year periods
  #max_run = 3, # specify 3 individual runs for each model
  vars = c("PPT", "MAT")) #decide any other climate variables we want to include! 


#sanity check
#merge back with plot data 
plot_dat<-left_join(plot_dat, rename(clim_dat, PlotNumber=id))

ggplot(plot_dat, aes(x=Elevation, y=MAT))+
  geom_point()+
  geom_smooth(method='lm')+
  theme(legend.position = 'none')+
  ylim(-10, 10) + xlim(0,4000) #git rid of outliers 

ggplot(plot_dat, aes(x=Elevation, y=PPT))+
  geom_point()+
  geom_smooth()+
  #ylim(-10, 10) + 
  xlim(0,4000) #git rid of outliers 

cour10eygrace avatar Jun 18 '24 00:06 cour10eygrace