R equivalent of Java map - r

I would like to pass a key/value pair from my R code to a java function.
My java function has argument Map<String,String > .
How can I write R function which calls my Java function and pass values to map ??
EDIT :
config <- list(Portname="PORT.H.2",MktValue="8000000",WtScheme="Closed")
createPortfolio<-function(config)
{
m <- .jnew("java/util/HashMap")
for( key in names(config)){
m$put( key, config[key])
}
m
getting
Error in FUN(X[[2L]], ...) :
Sorry, parameter type `NA' is ambiguous or not supported.

How about trying something like this?
m <- .jnew("java/util/HashMap")
m$put( "key", "value" )

I think you meant
for (key in names(config)) m$put(key, config[[key]])
since you want to pass string as value to put and not a list.
(Consider asking on the rJava mailing list stats-rosuda-devel to get a more prompt answer)

For people, like myself, who had this issue and don't have access to the Java code, it seems to be possible to make a HashMap, and cast it to a Map, like this:
m <- .jnew("java/util/HashMap")
m$put( "key", "value" )
map <- .jcast(m, "java/util/Map")

Related

GeoDmsRun cannot find 'Values' attribute inside Unique values unit, while GUI can

In GeoDMS, a geographic coding language by Object Vision, I cannot run code in GeoDmsRun.exe, which I could run without problems in GeoDmsGui.exe. The problem is that it cannot find the parameter 'Values' which is indeed not defined, but apparently implicit somewhere in GeoDMS. The GUI could find this parameter.
I tried defining the Values that lookup is looking for explicitly using
attribute<uint32>values1:=values;
But that didn't work. It would be best to get this lookup functionality without having to use any implicit variables, but how to do that?
Code:
unit<uint32> heatNet2 := unique(buildingWithHeatDemand/roadID)
, dialogType = 'map'
, dialogData = 'geometry'
{
attribute<rdc> geometry(arc) := lookup(values,input/geographic/roads/geometry);
}
Version: 7177
Thanks for helping!
The unique(D->V) operator indeed defines an attribute E->V with the name values of the resulting unit E that maps that resulting unit E to the found values of V. GeoDmsRun.exe should process scripts the same way as GeoDmsGui.exe does, so it is a good idea to report this as issue at http://www.mantis.objectvision.nl.
Meanwhile you can try to define the values attribute explicitly:
unit<uint32> heatNet2 := unique(buildingWithHeatDemand/roadID)
, dialogType = 'map'
, dialogData = 'geometry'
{
attribute<input/geographic/roads> values(heatNet2);
attribute<rdc> geometry(arc) := lookup(values,input/geographic/roads/geometry);
}
The now explicitly defined values will refer to the attribute of the result of the unique operator.

Referencing list element inside of map in Kotlin

I am new to Kotlin and am still trying to learn it. I have been researching this problem for several hours now and still have not figured it out. I want to get an element from inside of a list by it's index. I figured out how to do this with a plain list, like so
val my_list = listOf(1,2,3)
println(my_list.get(0))
The above works, but when I try to do this with a list that is stored inside of a map
val my_list = mutableMapOf<String, Any>()
my_list["set1"] = listOf(1,2,3)
my_list["set2"] = listOf("A","B","C")
my_list["set3"] = listOf("d","e","f")
val sub_list = my_list["set1"]
println(sub_list.get(0))
I get the following error
Unresolved reference. None of the following candidates is applicable
because of receiver type mismatch: #InlineOnly public inline operator
fun <#OnlyInputTypes K, V> Map.get(key: Int): ???
defined in kotlin.collections #SinceKotlin public operator fun
MatchGroupCollection.get(name: String): MatchGroup? defined in
kotlin.text
Note: I primarily use Python, so that is what I am used to. The functionality from Python that I am trying to reproduce in Kotlin is having a dictionary of lists.
The problem is the type declaration of your map, it should be:
val my_list = mutableMapOf<String, List<Any>>()
Any doesn't have a get() method, so there's no way to invoke it.
Even when that problem is solved, you'll probably have to deal with nullability, though, as:
val sub_list = my_list["set1"]
Will return List<Any>?, which means that my_list might not have a value for the specified key. If that's the case, you'll have to do something like:
sub_list?.get(0)?.run { println(it) }
Which in turn, could also cause an exception if the sub_list is empty. That could be solved with something more like:
vsub_list?.firstOrNull()?.run { println(it) }

R only specify optional parameters if specified

I have an R function with optional parameters like so:
myFunc <- function(
requiredParam,
optionalParam1 = optionalValue1,
optionalParam2 = optionalValue2,
...
optionalParamN = optionalValueN) {
# implementation
}
I have another function which calls this function and has the necessary parameters stored in a dataframe:
optionalParam1 optionalParam3 optionalParam10
1 "val1" "val2" "val3"
I only want to pass the optional parameters specified in the dataframe. For the others, I want it to use the default values. How can I accomplish this without typing up all permutations of optionalParameters existing/not existing?
Call the function using do.call (not knowing what your data.frame is called I will just assume you have a list or something of the parameters called myParams):
do.call(myFunc, as.list(myParams))
You can also build your function call as a string by parsing your dataframe column names and using paste.
Then, use eval(parse(text="your string"))

Subsetting data as generic function in R

I am trying to create a function that plots graphs for either an entire dataset, or a subset of the data. The function needs to be able to do both so that you can plot the subset if you so wish. I am struggling with just coming up with the generic subset function.
I currently have this code (I am more of a SAS user so R is confusing me a bit):
subset<-function(dat, varname, val)
if(dat$varname==val) {
data<-subset(dat, dat$varname==val)
}
But R keeps returning this error message:
Error in if (dat$varname == val) { : argument is of length zero
Could someone help me to resolve this? Thanks so much! I figure it may have to do with the way I wrote it.
First off all the $ operator can not handle variables. In your code you are always looking up a column named varname.
Replace $varname with [varname] instead.
The next error is that you are conditioning on a vector, dat$varname==val will be vector of booleans.
A third error in your code is that you are naming your function subset and thus overlayering the subset function in the base package. So the inner call to subset will be a recursive call to your own function. To fix this rename your function or you have to specify that it is the subset function in the base package you are calling with base::subset(dat, dat[varname]==val).
The final error in the code is that your function does not return anything. Do not assign the result to the variable data but return it instead.
Here is how the code should look like.
mySubset<-function(dat, varname, val)
if(any(dat[varname]==val)) {
subset(dat, dat[varname]==val)
} else {
NA
}
Or even better
mySubset <- function(dat,varname,val) dat[dat[varname] == val]

R function argument naming. sigm2 = sigm2_prior?

I went through the R documentation and I could not find a clue for this. So the problem is that it seems that l$sigm2 is also assigned when l$sigm2_prior is assigned. Does R behave like this because their similar names? Is there a way go around it?
Function call:
l$sigm2 is not assigned if I change l$sigm2_prior 's name to l$prior.
lik_gaussian(lik=lik[[1]],sigm2_prior=pn[[1]], debug=TRUE);
function:
lik_gaussian <-function(...){
l <- list(...);
inputarray <- NULL;
if(!(length(l$lik)==0)){
inputarray <- c(l$lik);
}
if(!(length(l$sigm2)==0)){
inputarray <- c(inputarray, l$sigm2);
}
if(!(length(l$sigm2_prior)==0)){
inputarray <- c(inputarray,l$sigm2_prior);
}
print(inputarray);
return(inputarray)
}
Thanks in advance.
The '$' operator uses partial matching. That is, if you evaluate l$sigm2 it will actually match to l$sigm2_prior as well. To use exact matching you need to use '[[' or and the name of the object as a string:
l[['sigm2_prior']]
'[[' differs from '$' in that it has the exact argument set to TRUE by default. Se also:
?'$'

Resources