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

Can I use variable for Field in set_value? #21

Closed
BRC555 opened this issue Oct 4, 2018 · 11 comments
Closed

Can I use variable for Field in set_value? #21

BRC555 opened this issue Oct 4, 2018 · 11 comments
Assignees
Labels

Comments

@BRC555
Copy link

BRC555 commented Oct 4, 2018

For my case, I can change the value of a field like this:

model6012Try$"Coil:Cooling:DX:SingleSpeed"$Thermal_Zone_ComputerRoom1_Coil_Cooling_DX_Single_Speed$set_value("Gross_Rated_Cooling_COP"=1.2)
Or
model6012Try$"Coil:Cooling:DX:SingleSpeed"$Thermal_Zone_ComputerRoom1_Coil_Cooling_DX_Single_Speed$set_value(Gross_Rated_Cooling_COP=2.2)

But I cannot do like this in a loop which automatically get the field name from a csv file:
AAA1="Gross_Rated_Cooling_COP";
model6012Try$"Coil:Cooling:DX:SingleSpeed"$Thermal_Zone_ComputerRoom1_Coil_Cooling_DX_Single_Speed$set_value(AAA1=3.2);

Could I know any solution for my case?
Thanks a lot.

@hongyuanjia
Copy link
Owner

There is no non-standard evaluation (NSE) feature implemented in eplusr yet.
AAA1 is treated as normal IDF field name. But by using rlang package, I think you could achieve what you want. Considering this below:

library(rlang)
library(eplusr)

AAA1 <- "Gross_Rated_Cooling_COP";
model6012Try$"Coil:Cooling:DX:SingleSpeed"$Thermal_Zone_ComputerRoom1_Coil_Cooling_DX_Single_Speed$set_value(!!AAA1:=3.2);

Please note the using of !! and := which come from rlang package. I haven't try on my side yet as I can't access to my computer right now.

I am currently refactoring model editing functionalities to make $set_value() more programming friendly. Stay tuned for next release.

@BRC555
Copy link
Author

BRC555 commented Oct 7, 2018 via email

@hongyuanjia
Copy link
Owner

hongyuanjia commented Oct 7, 2018

The images were not uploaded.

I would suggest to comment in GitHub Issues instead of directly replying using email in order to make sure the images can be displayed correctly.

@BRC555
Copy link
Author

BRC555 commented Oct 9, 2018

Thanks for your kind help!
These two are the missed pictures in the last section:
capture3
capture4

@BRC555
Copy link
Author

BRC555 commented Oct 9, 2018

I think there are some problems to use $set_value if some variables are used to replace the class, object or field.
Can u suggest any other alternative ways to set new value for a specific field in a object in a class, e.g. changing value in the "Default" column of the following screen shot?
capture5

Thanks a lot.

@hongyuanjia
Copy link
Owner

hongyuanjia commented Oct 9, 2018

Thanks for reporting that.

Thanks for your kind help!
These two are the missed pictures in the last section:
capture3
capture4

There are probably some bugs in $set_value(). It would be great if you could send me the debugging info by running traceback() after each error occurred. That will make it much easier for me to find where the problem was.


I think there are some problems to use $set_value if some variables are used to replace the class, object or field.
Can u suggest any other alternative ways to set new value for a specific field in a object in a class, e.g. changing value in the "Default" column of the following screen shot?
capture5

Thanks a lot.

Could you please send me the IDF file if there is no confidential concern? Then I can try on my side.

As for alternative ways to set values, you can use $set_object() in Idf class. As I don't have the model, just using an example file here to demonstrate:

library(eplusr)
library(data.table) # for reading input data

# taking this as an example
path_idf <- file.path(eplus_config(8.8)$dir, "ExampleFiles", "HospitalLowEnergy.idf")

idf <- read_idf(path_idf)

# read input data
input <- data.table::fread(
  "
  index , class             , object                                     , field , default , new_value
  1     , Coil:Heating:Fuel , Floor 1 Cafe Heat Pump Sup Heat Coil       , 4     , 0.8     , 0.7
  2     , Coil:Heating:Fuel , Floor 1 Clean Heat Pump Sup Heat Coil      , 4     , 0.8     , 0.6
  3     , Coil:Heating:Fuel , Floor 1 Conference Heat Pump Sup Heat Coil , 4     , 0.8     , 0.5
  4     , Coil:Heating:Fuel , Floor 1 Dining Heat Pump Sup Heat Coil     , 4     , 0.8     , 0.4
  "
)

# get target field names
input[, `:=`(field_name = idf$definition(class)[[1L]]$field_name(field)), by = index]

# get correct input value format for $set_object()
format_input_value <- function (field_name, value) {
  val <- list(value)
  names(val) <- field_name
  val
}

input[, `:=`(value = list(format_input_value(field_name, new_value))), by = index]

idf$set_object(object = input$object, value = input$value)

# check results
vapply(idf$object(input$object), function (obj) obj$get_value(4), list(1))
# $Floor_1_Cafe_Heat_Pump_Sup_Heat_Coil
# [1] 0.7
# 
# $Floor_1_Clean_Heat_Pump_Sup_Heat_Coil
# [1] 0.6
# 
# $Floor_1_Conference_Heat_Pump_Sup_Heat_Coil
# [1] 0.5
# 
# $Floor_1_Dining_Heat_Pump_Sup_Heat_Coil
# [1] 0.4
# 

@hongyuanjia
Copy link
Owner

Also, please make sure to use the develop version as there was a bug in the CRAN version (v0.9.2) when updating value references (see #20).

You can install the develop version by doing:

devtools::install_github("hongyuanjia/eplusr")

@BRC555
Copy link
Author

BRC555 commented Oct 9, 2018

Also, please make sure to use the develop version as there was a bug in the CRAN version (v0.9.2) when updating value references (see #20).

You can install the develop version by doing:

devtools::install_github("hongyuanjia/eplusr")

Thanks,
I try to run "devtools::install_github("hongyuanjia/eplusr")"; But there is something wrong:
capture6

@hongyuanjia
Copy link
Owner

Please restart R and RStudio and then do the installation.

You may also want to manually run install.packages(c("data.table", "lubridate")) as they failed to install automatically in your case.

@hongyuanjia hongyuanjia self-assigned this Oct 9, 2018
@hongyuanjia
Copy link
Owner

The new version of 0.9.3 has been landed on CRAN. You can install the latest version from CRAN which has already fixed the issue #20.

@hongyuanjia
Copy link
Owner

In version v0.10.0, $set() in Idf and IdfObject class directly support inputs as lists. Close this.

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

No branches or pull requests

2 participants