Skip to content

Commit

Permalink
Fixed RStringVector#toFactorVector() method
Browse files Browse the repository at this point in the history
  • Loading branch information
vruusmann committed Dec 17, 2024
1 parent c6210e6 commit 3c7e888
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
7 changes: 5 additions & 2 deletions pmml-rexp/src/main/java/org/jpmml/rexp/RStringVector.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.jpmml.rexp;

import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -93,9 +94,11 @@ public RFactorVector toFactorVector(List<String> levels){
})
.toArray();

RPair attributes = new RPair(new RString("levels"), new RStringVector(levels, null), null);
RFactorVector result = new RFactorVector(levelValues, null);
result.addAttribute("class", new RStringVector(Arrays.asList("factor"), null));
result.addAttribute("levels", new RStringVector(levels, null));

return new RFactorVector(levelValues, attributes);
return result;
}

@Override
Expand Down
6 changes: 6 additions & 0 deletions pmml-rexp/src/test/R/serialize.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ print(attributes(integerVec))

storeRds(integerVec, "IntegerVector")

stringVec = c("alpha", "beta", "gamma")
print(stringVec)
print(attributes(stringVec))

storeRds(stringVec, "StringVector")

factorVec = factor(c("alpha", "beta", "gamma"))
print(attributes(factorVec))

Expand Down
21 changes: 21 additions & 0 deletions pmml-rexp/src/test/java/org/jpmml/rexp/SerializeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,19 @@ public void rdsIntegerVector() throws IOException {
checkIntegerVector(rdsClone(integerVec, true));
}

@Test
public void rdsStringVector() throws IOException {
RStringVector stringVec = (RStringVector)unserialize("StringVector");

checkStringVector(rdsClone(stringVec));
checkStringVector(rdsClone(stringVec, true));

RFactorVector factorVec = stringVec.toFactorVector();

checkFactorVector(rdsClone(factorVec));
checkFactorVector(rdsClone(factorVec, true));
}

@Test
public void rdsFactorVector() throws IOException {
RFactorVector factorVec = (RFactorVector)unserialize("FactorVector");
Expand Down Expand Up @@ -89,6 +102,14 @@ private void checkIntegerVector(RIntegerVector integerVec){
assertEquals(Arrays.asList(null, null), integerVec.getValues());
}

static
private void checkStringVector(RStringVector stringVec){
assertNull(stringVec.getAttributes());

assertEquals(3, stringVec.size());
assertEquals(Arrays.asList("alpha", "beta", "gamma"), stringVec.getValues());
}

static
private void checkFactorVector(RFactorVector factorVec){
assertTrue(factorVec.hasAttribute("class"));
Expand Down
Binary file added pmml-rexp/src/test/resources/rds/StringVector.rds
Binary file not shown.

0 comments on commit 3c7e888

Please sign in to comment.