Access elements of an R6 class that instantiates another R6 class - r

Say I have a class SimpleClass and one of the methods of that class can return an object of another class, e.g.
SimpleClass <- R6::R6Class(
"SimpleClass",
public = list(
initialize = function() {
private$a <- 1
},
cls_two = function() SimpleClass2$new()
),
private = list(
a = numeric()
)
)
Where SimpleClass2 is
SimpleClass2 <- R6::R6Class(
"SimpleClass2",
public = list(
initialize = function() {
private$b <- 2
},
get_a = function() private$a
),
private = list(
b = numeric()
)
)
Here, if I were instantiate SimpleClass and call the method cls_two(), the resulting object will not have access to the elements of the first object unless I pass them on. Is there a way for the secondary class to access elements of the first class?
first <- SimpleClass$new()
second <- first$cls_two()
second$get_a()
# NULL
Note that I do not want to use inheritance in the traditional sense because I do not want to reinstantiate the first class. I would also prefer not to have to pass on all of the objects to SimpleClass2$new().

Extend SimpleClass2’s constructor to take an object of type SimpleClass, and pass self when calling the constructor in SimpleClass::cls_two:
SimpleClass2 <- R6::R6Class(
"SimpleClass2",
public = list(
initialize = function(obj) {
private$b <- 2
private$obj <- obj
},
get_a = function() private$obj
),
private = list(
b = numeric(),
obj = NULL
)
)

You can make SimpleClass2 have a member that is a Simpleclass and have an option to pass a simpleclass in its constructor:
SimpleClass <- R6::R6Class(
"SimpleClass",
public = list(
initialize = function() {
private$a <- 1
},
cls_two = function() SimpleClass2$new(self)
),
private = list(
a = numeric()
)
)
SimpleClass2 <- R6::R6Class(
"SimpleClass2",
public = list(
initialize = function(Simp1 = SimpleClass$new()) {
private$a <- Simp1
private$b <- 2
},
get_a = function() private$a
),
private = list(
a = SimpleClass$new(),
b = numeric()
)
)
Which works like this:
first <- SimpleClass$new()
second <- first$cls_two()
second$get_a()
#> <SimpleClass>
#> Public:
#> clone: function (deep = FALSE)
#> cls_two: function ()
#> initialize: function ()
#> Private:
#> a: 1

Related

How to integrate database connection to R6 class in R

Hello I would like to assign a dbConnection to a R6 class but it fails.
LastProfilZdb <- R6Class(
classname = "LastProfilZdb",
public = list(
name = NULL,
zp = NULL,
data = NULL ,
zp_id = function() {
pool::poolWithTransaction(self$db_server, function(conn){
DBI::dbGetQuery(conn, paste0("SELECT ZP_ID FROM
ZP_ID WHERE LP_ZP = '", self$zaehlpunkt,
"' OR ZP_NAME_SAP = '", self$zaehlpunkt, "'"))
})
},
#....
#....
initialize = function(){
message("Init Data Base Connection")
#
self$db_server <- pool::dbPool(drv = odbc::odbc(),
dsn = "Oracle",
schema = "schema" )
},
finalize = function() {
message("Closing Data Base Connection")
pool::poolClose(self$db_server)
}
),
private = list(
# db Connection is stored in the calss so we don't need to care any more
# This way, input data can be collected in a neat way,
# and stored inside our object.
db_server = NULL
)
)
This fails with error:
Error in self$db_server <- pool::dbPool(drv = odbc::odbc(), dsn = "Oracle", :
cannot add bindings to a locked environment
What can I do?

R: Assign vector element as list variable name

Here is the end result that I would like to achieve:
json_creative_pause = list("47770124" =
list(patch = list(
`$set` = list (
status = "PAUSED"
))))
The issue is that I would like to create this same structure dynamically for multiple ids. I have this variable:
creative_ids_to_pause = c("75196186", "78369656", "80050466")
And I would like to recreate the same structure for each creative id. But running such code doesn't really work:
json_creative_pause = list(get(creative_ids_to_pause[1]) =
list(patch = list(
`$set` = list (
status = "PAUSED"
))))
Does anyone know how to assign a vector element as a variable name inside of a list?
Thank you for your help!
Arben
We can use setNames
setNames(list(patch = list(
`$set` = list (
status = "PAUSED"
))), creative_ids_to_pause[1])
#$`75196186`
#$`75196186`$`$set`
#$`75196186`$`$set`$status
#[1] "PAUSED"
Or another option is dplyr::lst
dplyr::lst(!! creative_ids_to_pause[1] :=
list(patch = list(
`$set` = list (
status = "PAUSED"
))))
#$`75196186`
#$`75196186`$patch
#$`75196186`$patch$`$set`
#$`75196186`$patch$`$set`$status
#[1] "PAUSED"
I also found this solution:
creative_ids_to_pause = c("75196186", "78369656", "80050466")
unnamed_json_structure = list(
patch = list(
`$set` = list(
status = "PAUSED"
)))
list_all_creative_ids = list()
for(i in 1:length(creative_ids_to_pause)){
list_all_creative_ids[[creative_ids_to_pause[i]]] = unnamed_json_structure
}
list_all_creative_ids %>%
toJSON(pretty = TRUE, auto_unbox = TRUE)
Result
"75196186": {
"patch": {
"$set": {
"status": "PAUSED"
}
}
},
"78369656": {
"patch": {
"$set": {
"status": "PAUSED"
}
}
},
"80050466": {
"patch": {
"$set": {
"status": "PAUSED"
}
}
}
}

Error when implementing a custom layer in keras for R

I am trying to implement a custom layer for the package keras in R (github).
The layer I am implementing is based on this AttentionWithContext layer available here: gist
Here is my code:
AttentionWithContext <- R6::R6Class("AttentionWithContext",
inherit = KerasLayer,
public = list(
W_regularizer = NULL,
b_regularizer = NULL,
u_regularizer = NULL,
W_constraint=NULL,
b_constraint=NULL,
u_constraint=NULL,
bias=NULL,
b=NULL,
W=NULL,
u=NULL,
supports_masking=NULL,
init=NULL,
name = NULL,
initialize = function(name = 'attention',
W_regularizer = NULL,
b_regularizer = NULL,
u_regularizer = NULL,
W_constraint=NULL,
b_constraint=NULL,
u_constraint=NULL,
bias=TRUE ) {
self$supports_masking = TRUE
self$init = keras::initializer_glorot_uniform()
self$W_regularizer = W_regularizer
self$b_regularizer = b_regularizer
self$u_regularizer = u_regularizer
self$W_constraint = W_constraint
self$b_constraint = b_constraint
self$u_constraint = u_constraint
self$bias = bias
self$name = name
},
build = function(input_shape) {
assertthat::assert_that(length(input_shape) == 3)
self$W = self$add_weight(shape = reticulate::tuple(input_shape[[3]],input_shape[[3]], NULL),
initializer = self$init,
name=stringr::str_interp('${self$name}_W'),
regularizer = self$W_regularizer,
constraint = self$W_constraint)
if (self$bias) {
self$b = self$add_weight(shape = reticulate::tuple(input_shape[[3]]),
initializer='zero',
name = stringr::str_interp('${self$name}_b'),
regularizer = self$b_regularizer,
constraint = self$b_constraint)
}
self$u = self$add_weight(shape = reticulate::tuple(input_shape[[3]]),
initializer=self$init,
name = stringr::str_interp('${self$name}_u'),
regularizer = self$u_regularizer,
constraint = self$u_constraint)
},
compute_mask = function(input, input_mask=NULL) {
return(NULL)
},
call = function(x, mask = NULL) {
uit = keras::k_squeeze(keras::k_dot(x, keras::k_expand_dims(self$W)), axis=-1)
if (self$bias) {
uit = uit + self$b
}
uit = keras::k_tanh(uit)
ait = keras::k_dot(uit, self$u)
a = keras::k_exp(ait)
if (!is.null(mask)) {
a = a * keras::k_cast(mask, keras::k_floatx())
}
a = a/keras::k_cast(keras::k_sum(a, axis = 1, keepdims = TRUE) + keras::k_epsilon(), keras::k_floatx())
weighted_input = x * keras::k_expand_dims(a)
keras::k_sum(weighted_input, axis=1)
},
compute_output_shape = function(input_shape) {
list(input_shape[[1]], input_shape[[3]])
}
)
)
# define layer wrapper function
layer_attention_with_context <- function(object, W_regularizer = NULL,
b_regularizer = NULL,
u_regularizer = NULL,
W_constraint=NULL,
b_constraint=NULL,
u_constraint=NULL,
bias=TRUE,
name = 'attention_with_context') {
create_layer(AttentionWithContext, object, list(W_regularizer = W_regularizer,
b_regularizer = b_regularizer,
u_regularizer = u_regularizer,
W_constraint= W_constraint,
b_constraint=b_constraint,
u_constraint=u_constraint,
bias=bias,
name = name
))
}
# Example
model <- keras_model_sequential()
model %>%
layer_embedding(input_dim = 20000,
output_dim = 128,
input_length = 30) %>%
layer_lstm(64, return_sequences = TRUE) %>%
layer_attention_with_context() %>%
time_distributed(layer_dense(units=10))
When I run this, I get a cryptic error message:
Error in py_call_impl(callable, dots$args, dots$keywords) :
RuntimeError: Evaluation error: TypeError: unsupported operand type(s) for *: 'NoneType' and 'int'.
I tried to explore this error and I think it might come from this line :
reticulate::tuple(input_shape[[3]],input_shape[[3]], NULL)
In the original code, in python, we can see this:
(input_shape[-1], input_shape[-1],)
I could not find a way to create this structure in R.
Any ideas ?

Change initialize method in subclass of an R6 class

Let's say I have a R6 class Person:
library(R6)
Person <- R6Class("Person",
public = list(name = NA, hair = NA,
initialize = function(name, hair) {
self$name <- name
self$hair <- hair
self$greet()
},
greet = function() {
cat("Hello, my name is ", self$name, ".\n", sep = "")
})
)
If I want to create a subclass whose initialize method should be the same except for adding one more variable to self how would I do this?
I tried the following:
PersonWithSurname <- R6Class("PersonWithSurname",
inherit = Person,
public = list(surname = NA,
initialize = function(name, surname, hair) {
Person$new(name, hair)
self$surname <- surname
})
)
However when I create a new instance of class PersonWithSurname the fields name and hair are NA, i.e. the default value of class Person.
PersonWithSurname$new("John", "Doe", "brown")
Hello, my name is John.
<PersonWithSurname>
Inherits from: <Person>
Public:
clone: function (deep = FALSE)
greet: function ()
hair: NA
initialize: function (name, surname, hair)
name: NA
surname: Doe
In Python I would do the following:
class Person(object):
def __init__(self, name, hair):
self.name = name
self.hair = hair
self.greet()
def greet(self):
print "Hello, my name is " + self.name
class PersonWithSurname(Person):
def __init__(self, name, surname, hair):
Person.__init__(self, name, hair)
self.surname = surname
R6 works very much like Python in this regard; that is, you just call initialize on the super object:
PersonWithSurname <- R6Class("PersonWithSurname",
inherit = Person,
public = list(surname = NA,
initialize = function(name, surname, hair) {
super$initialize(name, hair)
self$surname <- surname
})
)

Problems with "observe" in Shiny

I am working in a ShinyApp which objective here is to create a selection based on attributes that are generated from an Excel database (the number of attributes may vary). Below is the most important part of my server.R code:
shinyServer(function(input, output, session){
seleciona_planilha <- observe({
dados = input$arquivo
if (is.null(dados))
{
return(NULL)
}
else
{
capturar = names(getSheets(loadWorkbook(dados$datapath)))
updateSelectInput(session,"planilha",choices = capturar)
}
})
carrega_dados <- reactive({
dados = input$arquivo
if (is.null(dados))
{
return(NULL)
}
else{return(read.xlsx(dados$datapath, sheetName = input$planilha))}
})
The first "observe" function above works well in order to select the correct sheet and proceed to analysis with the code below.
rotina <- reactive({
dados = carrega_dados()
tam_dados = length(dados)
pos_ini = 22
vet_comp = vector()
resultados = as.data.frame(matrix(nrow = length(seq(pos_ini,tam_dados,2)), ncol = 10))
nomes = names(dados)
cd = 4
k = 1
amostra = vector()
for(i in 1:length(dados[,1]))
{
if(dados[i,6] == 1)
{
amostra[1] = as.character(dados[i,7])
break
}
}
for(i in 1:length(dados[,1]))
{
if(dados[i,6] == 2)
{
amostra[2] = as.character(dados[i,7])
break
}
}
names(resultados) = c("Name",amostra[1], amostra[2],"NSD",
"Tau","Var(Tau)","D-Prime",
"Var(D-Prime)","IC(D-Prime)","p-value(D-Prime)")
for(i in seq(pos_ini,tam_dados,2))
{
cNSD = 0
c1 = 0
c2 = 0
for(j in 1:length(dados[,i]))
{
if(as.character(dados[j,i]) == " NSD" || as.character(dados[j,i]) == "NSD")
{
cNSD = cNSD + 1
}
if(as.character(dados[j,i]) == "1")
{
c1 = c1 + 1
}
if(as.character(dados[j,i]) == "2")
{
c2 = c2 + 1
}
}
vet_comp = c(c1,cNSD,c2)
resultados[k,1] = nomes[i]
resultados[k,2] = c1
resultados[k,3] = c2
resultados[k,4] = cNSD
resultados[k,5] = round(twoAC(vet_comp)$coefficients[1,1],cd)
resultados[k,6] = round((twoAC(vet_comp)$coefficients[1,2])^2,cd)
resultados[k,7] = round(twoAC(vet_comp)$coefficients[2,1],cd)
resultados[k,8] = round((twoAC(vet_comp)$coefficients[1,2])^2,cd)
if(vet_comp[1] != 0 && vet_comp[2] != 0 && vet_comp[3] != 0)
{
resultados[k,9] = paste("[",round(twoAC(vet_comp)$confint[,1],cd),";",
round(twoAC(vet_comp)$confint[,2],cd),"]")
}
else
{
resultados[k,9] = paste("No IC")
}
resultados[k,10] = round((twoAC(vet_comp)$p.value)/2,cd)
k = k + 1
}
return(resultados)
})
The reactive function "rotina" returns a data.frame. The first column in the data.frame are the attribute names that I would like to use in a selector.
But for some reason I don't know, when I call another "observe" function to get the attribute names and pass to the selector, it not works.
seleciona_atributo <- observe({
resultados = rotina()
atributos = resultados[,1]
updateSelectInput(session,"atributo",choices = atributos)
})
I tried to assign "resultados" as a global variable too, but with no success.
Finally, my ui.R code:
shinyUI(fluidPage(
titlePanel("2-AC Sensory Tool"),
sidebarLayout(
sidebarPanel(
fileInput('arquivo', 'Choose XLS/XLSX File',
accept=c('.xls','.xlsx')),
tags$hr(),
selectInput("planilha",label = h4("Select data sheet"),""),
tags$hr(),
selectInput("atributo",label = h4("Select attribute to generate d-prime graphic",""),
downloadButton('download', 'Download results')
),
mainPanel(
plotOutput("grafico_dp"),
plotOutput("grafico_dist"),
h4("Results Table"),
dataTableOutput("saida")
)
)
))
Thanks in advance!

Resources