8000 Consistent handling of NA values in CharacterMatrix and Vector · rdotnet/rdotnet@c97910a · GitHub
[go: up one dir, main page]

Skip to content
Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit c97910a

Browse files
committed
Consistent handling of NA values in CharacterMatrix and Vector
1 parent 9db8f32 commit c97910a

File tree

5 files changed

+89
-11
lines changed

5 files changed

+89
-11
lines changed

R.NET/CharacterMatrix.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ protected internal CharacterMatrix(REngine engine, IntPtr coerced)
6363
{
6464
int offset = GetOffset(rowIndex, columnIndex);
6565
IntPtr pointer = Marshal.ReadIntPtr(DataPointer, offset);
66+
if (pointer == Engine.NaStringPointer)
67+
{
68+
return null;
69+
}
6670
return new InternalString(Engine, pointer);
6771
}
6872
}
@@ -83,14 +87,20 @@ protected internal CharacterMatrix(REngine engine, IntPtr coerced)
8387
}
8488
}
8589

90+
private Rf_mkChar _mkChar = null;
91+
92+
private IntPtr mkChar(string value)
93+
{
94+
if (_mkChar == null)
95+
_mkChar = this.GetFunction<Rf_mkChar>();
96+
return _mkChar(value);
97+
}
98+
8699
private void SetValue(int rowIndex, int columnIndex, string value)
87100
{
88101
int offset = GetOffset(rowIndex, columnIndex);
89-
SymbolicExpression s = value == null ? Engine.NilValue : new InternalString(Engine, value);
90-
using (new ProtectedPointer(s))
91-
{
92-
Marshal.WriteIntPtr(DataPointer, offset, s.DangerousGetHandle());
93-
}
102+
IntPtr stringPointer = value == null ? Engine.NaStringPointer : mkChar(value);
103+
Marshal.WriteIntPtr(DataPointer, offset, stringPointer);
94104
}
95105

96106
/// <summary>

RDotNet.TestBase/RDotNet.TestBase.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
2+
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
33
<Import Project="..\packages\xunit.core.2.0.0\build\portable-net45+win+wpa81+wp80+monotouch+monoandroid+Xamarin.iOS\xunit.core.props" Condition="Exists('..\packages\xunit.core.2.0.0\build\portable-net45+win+wpa81+wp80+monotouch+monoandroid+Xamarin.iOS\xunit.core.props')" />
44
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
55
<PropertyGroup>
@@ -10,7 +10,7 @@
1010
<AppDesignerFolder>Properties</AppDesignerFolder>
1111
<RootNamespace>RDotNet</RootNamespace>
1212
<AssemblyName>RDotNet.TestBase</AssemblyName>
13-
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
13+
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
1414
<FileAlignment>512</FileAlignment>
1515
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\</SolutionDir>
1616
<RestorePackages>true</RestorePackages>

RDotNet.Tests/CharacterMatrixTest.cs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using Xunit;
2+
3+
namespace RDotNet
4+
{
5+
public class CharacterMatrixTest : RDotNetTestFixture
6+
{
7+
[Fact]
8+
public void TestCharacter()
9+
{
10+
SetUpTest();
11+
var engine = this.Engine;
12+
var matrix = engine.Evaluate("x <- matrix(c(1, NA, 2, 3, NA, 4), nrow=3, ncol=2, byrow=TRUE)").AsCharacterMatrix();
13+
Assert.Equal(3, matrix.RowCount);
14+
Assert.Equal(2, matrix.ColumnCount);
15+
Assert.Equal("1", matrix[0,0]);
16+
Assert.Null(matrix[0,1]);
17+
Assert.Equal("2", matrix[1, 0]);
18+
Assert.Equal("3", matrix[1, 1]);
19+
Assert.Null(matrix[2, 0]);
20+
Assert.Equal("4", matrix[2, 1]);
21+
22+
matrix[0, 0] = null;
23+
matrix[0, 1] = "A";
24+
Assert.Null(matrix[0,0]);
25+
Assert.Equal("A", matrix[0, 1]);
26+
27+
var logical = engine.Evaluate("is.na(x)").AsLogical(); // This gets strung out as col1 + col2
28+
// The original x was never modified, just our copy of it did. So the logical should reflect the
29+
// original value of x.
30+
Assert.False(logical[0]); // 1
31+
Assert.False(logical[1]); // 2
32+
Assert.True(logical[2]); // NA
33+
Assert.True(logical[3]); // NA
34+
Assert.False(logical[4]); // 3
35+
Assert.False(logical[5]); // 4
36+
}
37+
38+
[Fact]
39+
public void TestCharacter_NoConversion()
40+
{
41+
SetUpTest();
42+
var engine = this.Engine;
43+
var matrix = engine.Evaluate("x <- matrix(c('1', NA, '2', '3', NA, '4'), nrow=3, ncol=2, byrow=TRUE)").AsCharacterMatrix();
44+
Assert.Equal(3, matrix.RowCount);
45+
Assert.Equal(2, matrix.ColumnCount);
46+
Assert.Equal("1", matrix[0, 0]);
47+
Assert.Null(matrix[0, 1]);
48+
Assert.Equal("2", matrix[1, 0]);
49+
Assert.Equal("3", matrix[1, 1]);
50+
Assert.Null(matrix[2, 0]);
51+
Assert.Equal("4", matrix[2, 1]);
52+
53+
matrix[0, 0] = null;
54+
matrix[0, 1] = "A";
55+
Assert.Null(matrix[0, 0]);
56+
Assert.Equal("A", matrix[0, 1]);
57+
58+
var logical = engine.Evaluate("is.na(x)").AsLogical();
59+
Assert.True(logical[0]); // Was 1, now NA because we modified the original x
60+
Assert.False(logical[1]); // 2
61+
Assert.True(logical[2]); // NA
62+
Assert.False(logical[3]); // "A"
63+
Assert.False(logical[4]); // 3
64+
Assert.False(logical[5]); // 4
65+
}
66+
}
67+
}

RDotNet.Tests/RDotNet.Tests.csproj

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
2+
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
33
<Import Project="..\packages\xunit.runner.console.2.4.1\build\xunit.runner.console.props" Condition="Exists('..\packages\xunit.runner.console.2.4.1\build\xunit.runner.console.props')" />
44
<Import Project="..\packages\xunit.runner.visualstudio.2.3.1\build\net20\xunit.runner.visualstudio.props" Condition="Exists('..\packages\xunit.runner.visualstudio.2.3.1\build\net20\xunit.runner.visualstudio.props')" />
55
<Import Project="..\packages\xunit.core.2.0.0\build\portable-net45+win+wpa81+wp80+monotouch+monoandroid+Xamarin.iOS\xunit.core.props" Condition="Exists('..\packages\xunit.core.2.0.0\build\portable-net45+win+wpa81+wp80+monotouch+monoandroid+Xamarin.iOS\xunit.core.props')" />
@@ -11,7 +11,7 @@
1111
<AppDesignerFolder>Properties</AppDesignerFolder>
1212
<RootNamespace>RDotNet</RootNamespace>
1313
<AssemblyName>RDotNet.Tests</AssemblyName>
14-
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
14+
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
1515
<FileAlignment>512</FileAlignment>
1616
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\</SolutionDir>
1717
<RestorePackages>true</RestorePackages>
@@ -76,10 +76,11 @@
7676
</Reference>
7777
</ItemGroup>
7878
<ItemGroup>
79+
<Compile Include="CharacterMatrixTest.cs" />
80+
<Compile Include="CharacterVectorTest.cs" />
7981
<Compile Include="MultiThreadingTest.cs" />
8082
<Compile Include="S4ClassesTest.cs" />
8183
<Compile Include="ListsTest.cs" />
82-
<Compile Include="CharacterVectorTest.cs" />
8384
<Compile Include="REngineInitTest.cs" />
8485
<Compile Include="DataConversionTest.cs" />
8586
<Compile Include="EnvironmentTest.cs" />

RDotNet.Tests/packages.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77
<package id="xunit.core" version="2.0.0" targetFramework="net45" />
88
<package id="xunit.extensibility.core" version="2.0.0" targetFramework="net45" />
99
<package id="xunit.MSBuild" version="2.0.0.0" targetFramework="net45" developmentDependency="true" />
10-
<package id="xunit.runner.console" version="2.4.1" targetFramework="net45" developmentDependency="true" />
10+
<package id="xunit.runner.console" version="2.4.1" targetFramework="net45" developmentDependency="true" requireReinstallation="True" />
1111
<package id="xunit.runner.visualstudio" version="2.4.1" targetFramework="net45" developmentDependency="true" />
1212
</packages>

0 commit comments

Comments
 (0)
0