Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
cour10eygrace opened this issue Jun 6, 2024 · 5 comments

Comments

@cour10eygrace
Copy link

cour10eygrace commented Jun 6, 2024

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
Copy link
Collaborator

cmahony commented Jun 6, 2024

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

@cour10eygrace
Copy link
Author

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
Copy link
Collaborator

cmahony commented Jun 7, 2024

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
Copy link
Collaborator

cmahony commented Jun 16, 2024

@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
Copy link
Author

cour10eygrace commented Jun 18, 2024

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 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants