In testing a Java API, I need to change a default setting. According to the API's document, it should be done using a method defined within the class using "public void setType". Suppose the class name is 'Node', which is referred using
library(rJava)
.jinit(classpath=jarPath)
Node <- J("Node")
In an Java example from its documents, it's called as
Node nodeX = new Node("X", new Variable[]{x});
nodeX.setType(Type.TEMP);
The default type of nodeX is 'CONTEMP'. How the "setType" method can be called in R through rJava to change its default value to another one? Let's assume 'Type' is an enum variable which has several options, including "CONTEMP","TEMP", etc.
I think you want
library(rJava)
.jinit(classpath=jarPath)
variable <- .jarray(new(J("package.name.Variable", input_arg))
Node <- new(J("package.name.Node"), variable)
Then you can do
type <- J("package.name.Type")$TEMP
Node$setType(type)
Related
Assuming that the class and instantiation of class are held in 2 separate files, how would you import the class data prior to instantiate the class?
Below code works fine if held in one same file, but I suspect that as soon as the code base starts growing you would want to split the data into smaller code chunks.
Should I use [source], does it exist an autoloader or any another guideline?
File: _class_data.R
if (!"package:R6" %in% search()) {
library(R6)
}
# Class 1
Class_1 <- R6Class("Class_1",
public = list(
# Properties:
x = 0,
# Lists:
credentials = list(
user = "user",
password = "pass"
),
# Functions:
myFunction = function() {
return(self$x)
}
)
)
File: run.R
# Should I add a [source] path here to [ _class_data.R] ?
# Instantiate a class by creating an object.
class_1 <- Class_1$new()
If I understand your question, you should be creating a package say MyPackage (containing your classes Class_1) and other person would be consumer, they would need to do library(MyPackage) in their code, before consuming the class.
You can source it if all the consumers are part of the same package.
The most straightforward way is to first run or source the class file. In this case the result will be an environment object that is stored in R:s global environment. This is the class.
As a second step you create an object, by instantiate the same class.
If this instantation is kept within a separate file, you would have to run or source that file also.
Since both objects (class and object) will now exist in the Global environment you can now decide if you want to remove the class and just keep the object.
Following standard guidelines the only name convention difference between the 2 objects would be that the class name start with a capital letter, meanwhile the object holds same name but with all characters in lower case.
If the amount of classes grows it is of course unpractical to administrate the objects one-by-one, and you would probably need some autoload logics.
I'm trying to get the basic Forio Epicenter example from the documentation to work, using Julia and the UI Builder. It looks like this:
#in MyModel.jl
module MyModel
# use the Epicenter Julia package
using Epicenter
# expose methods you want to call
# (e.g. using the Run API or Model Operation API,
# or the Model Operation property of a component in UI Builder)
export doubleIt
# expose variables you want to view or update
# (e.g. using the Run API or Model Variable API,
# or the Model Variable property of a component in UI Builder)
export parameters, result
# make exposed variables global
global parameters, result
# define variables that the end user will change as part of a complex type
type ParametersType
input1
input2
function ParametersType()
item = new()
item
end
end
function doubleIt()
global parameters
global result
result = parameters.input1 * 2
record(:result)
end
# other files in your model
include("Utils.jl")
include("OtherModelCalculations.jl")
end
I can't get any variables to connect. When I make a "Text Input" component and change the "Model Variable" property to match the Julia variable (exposed via "export" and "global"), loading the page gives me
400 Bad Request
Full JSON response:
{"status":400,
"errors":{"status":400,
"internal":{
"procedure":"push!(LOAD_PATH, string(ENV[\"HOME\"],
\"\/model\/\")); import Epicenter; using Juliet; using EpicenterSystem; EpicenterSystem.setup_idle_timeout
(1800); require(\"MyModel.jl\"); using MyModel","sessionId":"c5b8189b-4f4d-4377-87a9-ca8d154863c0","trace":""}
},
"message":"Bad Request."
}
I've tried every combination of the "Model Variable" I can think of, including "input1", "parameters", and "parameters.input1"; always comes back with a 400 error.
Forio Support here.
Two problems
a) since "input1" is a field in of variable parameters (type ParameterTypes), it must be referred to in the interface as "parameters.input1"
b) More importantly, the model attempts to include Utils.jl and OtherModelCalculations.jl, neither of which exist. Thus the model never loads.
So, I'm creating a script to extend the functionality of devtools::create() and I'm noticing some slightly odd behavior when I double check things with utils::maintainer. Here's a MWE where I set the Authors#R section of the description file through the devtools.desc.author option:
options(devtools.desc.license = "AGPL-3")
options(devtools.desc.author = "'Joe Dirt <joe#durt.ee> [aut, cre]'")
descArgs <- list(Package = "testPkg",
Title = "testPkg",
Description = "some desc.")
options(devtools.desc = descArgs)
devtools::create(path = "testPkg", check = TRUE)
Now, if you go ahead and run devtools::install("testPkg", quiet=TRUE), and then maintainer("testPkg") you get
> maintainer("testPkg")
[1] "'Joe Dirt' <joe#durt.ee>"
So my question is: why is the maintainer's name quoted, here?
This seems to be an issue with how the Maintainer field is auto generated from Authors#R. See:http://cran.r-project.org/doc/manuals/r-release/R-exts.html
Both ‘Author’ and ‘Maintainer’ fields can be omitted if a suitable ‘Authors#R’ field is given. This field can be used to provide a refined and machine-readable description of the package “authors” (in particular specifying their precise roles), via suitable R code. The roles can include ‘"aut"’ (author) for full authors, ‘"cre"’ (creator) for the package maintainer, and ‘"ctb"’ (contributor) for other contributors, ‘"cph"’ (copyright holder), among others. See ?person for more information. Note that no role is assumed by default. Auto-generated package citation information takes advantage of this specification. The ‘Author’ and ‘Maintainer’ fields are auto-generated from it if needed when building5 or installing.
Therefore, you should use the person function to specify the author list as follows:
options(devtools.desc.author ="c(person('Joe','Dirt',email='joe#durt.ee',role=c('aut','cre')))")
In a webcrawler/webscraper-setting, I'd like to dynamically extend my base Reference Class URL in order to be able to write specific methods for respective hosts/domains. Just to be clear, by dynamically I mean something like "automatically generate class definitions as new domains are encountered (e.g. class URL_something.com which would inherit from class URL)".
Works a treat, the only problem is that my class WebPage expects the value of field url to be of class URL. It will accept objects of class URL_something.com as this inherits from class URL, but then actually turns the object into an instance of class URL. So I lose the information that it's actually of class URL_something.com.
Do you have any idea of how I can prevent losing that crucial information?
Code Example
setRefClass(Class="URL", fields=list(x="character"))
setRefClass(Class="WebPage", fields=list(url="URL"))
obj <- new("WebPage", url=new("URL", x="http://www.something.com/home/index.html"))
obj$url
# Method would recognize that there is no class 'URL_something.com'
# yet and thus create it:
setRefClass(Class="URL_something.com", contains="URL")
# Another method would take care of mapping field values to
# an instance of the new class:
> url.obj <- new("URL_something.com", x="http://www.something.com/home/index.html")
> inherits(url.obj, "URL")
[1] TRUE
> obj$url <- url.obj
> class(obj$url)
[1] "URL"
# So I lose the information that it was actually of class "URL_something.com"
Picking up on what Martin said (see comments above): R 2.14.0 fixes what I described above.
Hi I am new to terraform and I am getting the following warning on my project when i run plan/apply.
Warning: output "cloudfront_distribution_id": must use splat syntax to access aws_cloudfront_distribution.cloudfront attribute "id", because it has "count" set;
This is usually a warning when you are using count to define the number of a specified resource you intend to create.
Use aws_cloudfront_distribution.cloudfront.*.id to obtain a list of the attributes across all instances
Example
output "cloudfront_distribution_id" {
value = "${join("", aws_cloudfront_distribution.cloudfront.*.id)}"
}