Skip to content

Commit eb132b8

Browse files
authored
fix: date/datatime variables are exported as chars when there are categories, number values handled when importing date/datetime variables (#3972)
* chore: code cleaning * fix: date/datatime variables are exported as chars when there are categories, number values handled when importing date/datetime variables
1 parent bdabcd5 commit eb132b8

File tree

5 files changed

+64
-32
lines changed

5 files changed

+64
-32
lines changed

opal-r/src/main/java/org/obiba/opal/r/datasource/RAssignDatasource.java

+17-14
Original file line numberDiff line numberDiff line change
@@ -224,17 +224,20 @@ private void doSendFiles() {
224224
* Read the CSV file in the R workspace with the appropriate data types.
225225
*/
226226
private void doReadCSVFile() {
227-
log.debug("Reading the CSV file into a tibble");
228-
ensurePackage("readr");
229-
ensurePackage("labelled");
230-
eval(String.format("base::source('%s')", COLS_FILE_NAME));
231-
eval(String.format("base::is.null(base::assign('%s', readr::read_csv('%s', col_types = .cols)))", getSymbol(tableName), DATA_FILE_NAME));
232-
eval("base::rm(.cols)");
233-
eval(String.format("base::source('%s')", ATTR_FILE_NAME));
234-
eval(String.format("base::unlink('%s')", DATA_FILE_NAME));
235-
eval(String.format("base::unlink('%s')", COLS_FILE_NAME));
236-
eval(String.format("base::unlink('%s')", ATTR_FILE_NAME));
237-
log.debug("Symbol {} assigned", getSymbol(tableName));
227+
try {
228+
log.debug("Reading the CSV file into a tibble");
229+
ensurePackage("readr");
230+
ensurePackage("labelled");
231+
eval(String.format("base::source('%s')", COLS_FILE_NAME));
232+
eval(String.format("base::is.null(base::assign('%s', readr::read_csv('%s', col_types = .cols)))", getSymbol(tableName), DATA_FILE_NAME));
233+
eval("base::rm(.cols)");
234+
eval(String.format("base::source('%s')", ATTR_FILE_NAME));
235+
log.debug("Symbol {} assigned", getSymbol(tableName));
236+
} finally {
237+
eval(String.format("base::unlink('%s')", DATA_FILE_NAME));
238+
eval(String.format("base::unlink('%s')", COLS_FILE_NAME));
239+
eval(String.format("base::unlink('%s')", ATTR_FILE_NAME));
240+
}
238241
}
239242

240243
}
@@ -309,9 +312,9 @@ else if (variable.getValueType().equals(DecimalType.get()))
309312
type = "double";
310313
else if (variable.getValueType().equals(BooleanType.get()))
311314
type = "logical";
312-
else if (variable.getValueType().equals(DateType.get()))
315+
else if (variable.getValueType().equals(DateType.get()) && !variable.hasCategories())
313316
type = "date";
314-
else if (variable.getValueType().equals(DateTimeType.get()))
317+
else if (variable.getValueType().equals(DateTimeType.get()) && !variable.hasCategories())
315318
type = "datetime";
316319
else
317320
type = "character";
@@ -361,7 +364,7 @@ private void writeVariableAttributes(Variable variable) {
361364
}
362365
// obiba/opal#3829 enforce haven to read spss missings properly
363366
attributesWriter.println(String.format("class(`%s`[['%s']]) <- c('haven_labelled_spss', class(`%s`[['%s']]))",
364-
getSymbol(tableName), variable.getName(), getSymbol(tableName), variable.getName()));
367+
getSymbol(tableName), variable.getName(), getSymbol(tableName), variable.getName()));
365368
}
366369
labels = String.format("c(%s)", Joiner.on(", ").join(getLabelledCategories(variable, variable.getCategories())));
367370
}

opal-spi-r/src/main/java/org/obiba/opal/spi/r/datasource/magma/AbstractRVariableValueSource.java

+46-15
Original file line numberDiff line numberDiff line change
@@ -62,29 +62,60 @@ private Value getNumericValue(Object objValue) {
6262
private Value getDateValue(Object objValue) {
6363
if (objValue == null || "NaN".equals(objValue)) return getValueType().nullValue();
6464
try {
65-
if (objValue instanceof Double) {
66-
Double dbl = (Double) objValue;
67-
if (dbl.isNaN()) return getValueType().nullValue();
68-
Date value = new Date(dbl.longValue() * 24 * 3600 * 1000);
69-
return getValueType().valueOf(value);
70-
} else
71-
return getValueType().valueOf(objValue);
65+
switch (objValue) {
66+
case Float flt -> {
67+
if (flt.isNaN()) return getValueType().nullValue();
68+
Date value = new Date(flt.longValue() * 24 * 3600 * 1000);
69+
return getValueType().valueOf(value);
70+
}
71+
case Double dbl -> {
72+
if (dbl.isNaN()) return getValueType().nullValue();
73+
Date value = new Date(dbl.longValue() * 24 * 3600 * 1000);
74+
return getValueType().valueOf(value);
75+
}
76+
case Integer ii -> {
77+
Date value = new Date(ii.longValue() * 24 * 3600 * 1000);
78+
return getValueType().valueOf(value);
79+
}
80+
case Long lng -> {
81+
Date value = new Date(lng * 1000);
82+
return getValueType().valueOf(value);
83+
}
84+
default -> {
85+
return getValueType().valueOf(objValue);
86+
}
87+
}
7288
} catch (Exception e) {
7389
return getValueType().nullValue();
7490
}
7591
}
7692

7793
private Value getDateTimeValue(Object objValue) {
7894
if (objValue == null || "NaN".equals(objValue)) return getValueType().nullValue();
79-
8095
try {
81-
if (objValue instanceof Double) {
82-
Double dbl = (Double) objValue;
83-
if (dbl.isNaN()) return getValueType().nullValue();
84-
Date value = new Date(dbl.longValue() * 1000);
85-
return getValueType().valueOf(value);
86-
} else
87-
return getValueType().valueOf(objValue);
96+
switch (objValue) {
97+
case Float flt -> {
98+
if (flt.isNaN()) return getValueType().nullValue();
99+
Date value = new Date(flt.longValue() * 1000);
100+
return getValueType().valueOf(value);
101+
}
102+
case Double dbl -> {
103+
if (dbl.isNaN()) return getValueType().nullValue();
104+
Date value = new Date(dbl.longValue() * 1000);
105+
return getValueType().valueOf(value);
106+
}
107+
case Integer ii -> {
108+
Date value = new Date(ii.longValue() * 1000);
109+
return getValueType().valueOf(value);
110+
}
111+
case Long lng -> {
112+
Date value = new Date(lng * 1000);
113+
return getValueType().valueOf(value);
114+
}
115+
default -> {
116+
return getValueType().valueOf(objValue);
117+
}
118+
}
88119
} catch (Exception e) {
89120
return getValueType().nullValue();
90121
}

opal-spi-r/src/main/java/org/obiba/opal/spi/r/datasource/magma/RVariableHelper.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ Variable newVariable() {
8080
String indexStr = extractProperty("opal.index");
8181
if (!Strings.isNullOrEmpty(indexStr)) {
8282
try {
83-
index = new Double(indexStr).intValue();
83+
index = Double.valueOf(indexStr).intValue();
8484
} catch (NumberFormatException e) {
8585
// ignore
8686
}

opal-ui/src/components/charts/CategoricalSummaryChart.vue

-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ import { VariableDto } from 'src/models/Magma';
6161
import { CategoricalSummaryDto, FrequencyDto } from 'src/models/Math';
6262
import FrequenciesTable from 'src/components/datasource/FrequenciesTable.vue';
6363
import VuePlotly from 'src/components/charts/VuePlotly.vue';
64-
import { getLabelsString } from 'src/utils/attributes';
6564
6665
const { t } = useI18n();
6766

opal-ui/src/components/charts/DefaultSummaryChart.vue

-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ import { VariableDto } from 'src/models/Magma';
6161
import { DefaultSummaryDto, FrequencyDto } from 'src/models/Math';
6262
import FrequenciesTable from 'src/components/datasource/FrequenciesTable.vue';
6363
import VuePlotly from 'src/components/charts/VuePlotly.vue';
64-
import { getLabelsString } from 'src/utils/attributes';
6564
6665
const { t } = useI18n();
6766

0 commit comments

Comments
 (0)