bcgov / climr

An R package for downscaling monthly climate data for North America
https://bcgov.github.io/climr/
14 stars 6 forks source link

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

Open cour10eygrace opened 1 month ago

cour10eygrace commented 1 month ago

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!

cmahony commented 1 month ago

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

cour10eygrace commented 1 month ago

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"))

cmahony commented 4 weeks ago

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 commented 2 weeks ago

@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.

cour10eygrace commented 2 weeks ago

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