Introduction to blaise

Sjoerd Ophof



The blaise package aims to provide an interface between the blaise software and R by enabling the reading and writing of blaise datafile sin a transparent manner. The aim is for an average user to be able to read or write such a datafile with a single command. Defaults are always set in such a way that the data is not changed if a user reads a datafile to a dataframe and immediately writes it to a blaise datafile afterwards.

Reading a blaise datafile with model

For the purpose of this vignette we need to create some small examples.

model1 = "
  A     : STRING[1]
  B     : INTEGER[1]
  C     : REAL[3,1]
  D     : REAL[3]
  E     : (Male, Female)
  F     : 1..20
  G     : 1.00..100.00
model2 = "
  A     : STRING[1]
  B     : INTEGER[1]
  C     : REAL[3,1]
  D     : REAL[3]
  E     : (Male (1), Female (2), Unknown (9))
  F     : 1..20
  G     : 1.00..100.00

data1 =
"A12.30.11 1  1.00
B23.41.2210 20.20
data2 = 
"A12,30,11 1  1,00
B23,41,2210 20,20
blafile1 = tempfile('testbla1', fileext = '.bla')
datafile1 = tempfile('testdata1', fileext = '.asc')
blafile2 = tempfile('testbla2', fileext = '.bla')
datafile2 = tempfile('testdata2', fileext = '.asc')
writeLines(data1, con = datafile1)
writeLines(model1, con = blafile1)
writeLines(data2, con = datafile2)
writeLines(model2, con = blafile2)

These file can then be simply read to a dataframe by using read_fwf_blaise.

df = read_fwf_blaise(datafile1, blafile1)
#> # A tibble: 3 x 7
#>   A         B     C     D E          F     G
#>   <chr> <int> <dbl> <dbl> <fct>  <int> <dbl>
#> 1 A         1   2.3   0.1 Male       1   1  
#> 2 B         2   3.4   1.2 Female    10  20.2
#> 3 C         3   4.5   0   Male      20 100

Resolving reading issues

If you try to read the second datafile with model you will however get some warnings and the resulting dataframe will not look as expected.

The blaise package uses readr to actually read the file into memory. Reading problems can therefore be analysed by using readr::problems()

These results are somewhat easier to parse but still hard to interpret. In this case it is clear that the comma is an unexpected character. This is because the locale is set to expect “.” as a decimal separator by default. This setting (and others, such as date format, encoding, etc.) can be changed by supplying a readr locale object using readr::locale().

Numbered enums

The second datamodel contains a numbered enum and is therefore read as a factor with number labels. By interpreting it thus the file will be written out exactly the same as can be seen later. This behaviour can be overwritten by using the option numbered_enum = FALSE. If the resulting dataframe is written back to blaise using write_fwf_blaise it will however write the integers in the set 1,2,3 instead of 1,2,9.

output options

Finally, instead of reading the file into memory, a LaF object can be returned instead. For details see the documentation for the LaF package.

Writing blaise datafiles

Dataframes can also be written out as blaise datafiles. By default this will also write a corresponding blaise datamodel with the same filename and a .bla extension

outfile = tempfile(fileext = ".asc")
outbla = sub(".asc", ".bla", outfile)
write_fwf_blaise(df, outfile)
#> [1] "A12.30.11 1  1.0" "B23.41.2210 20.2" "C34.50.0120100.0"
#>  [1] "DATAMODEL df"     "FIELDS"           "  A : STRING[1]" 
#>  [4] "  B : INTEGER[1]" "  C : REAL[3]"    "  D : REAL[3]"   
#>  [7] "  E : (Male,"     "       Female)"   "  F : INTEGER[2]"
#> [10] "  G : REAL[5]"    "ENDMODEL"

As can be seen, this is equivalent to the input data and model. An optional name for the datamodel can be given with output_model or the writing of a model can be entirely suppressed by using write_model = FALSE. For further options see the help file. Implicit conversions from R types to blaise types are as follows:

Note that information about the labels in the datamodel is lost for the numbered enum type. One way to solve this is by providing an existing datamodel and using write_fwf_blaise_with_model as follows.

outfile_model = tempfile(fileext = ".asc")
write_fwf_blaise_with_model(df_enum, outfile_model, blafile2)
#> [1] "A12.30.11 1  1.00" "B23.41.2210 20.20" "C34.50.0920100.00"

This results in the same datafile here, but ensures conformity to the datamodel. One could for instance also force a different model on the same data.

model3 = "
  A     : (A, B, C)
  B     : (Male (1), Female (2), Unknown (3))
blafile3 = tempfile('testbla3', fileext = '.bla')
writeLines(model3, con = blafile3)
outfile_new_model = tempfile(fileext = ".asc")
write_fwf_blaise_with_model(df_enum, outfile_new_model, blafile3)
#> [1] "11" "22" "33"

This explicitly checks for conformity, so if the data can not be converted an error will be shown and nothing will be written to disk.

model4 = "
  A     : (A, B)
  B     : (Male (1), Female (2))
blafile4 = tempfile('testbla4', fileext = '.bla')
writeLines(model4, con = blafile4)
outfile_wrong_model = tempfile(fileext = ".asc")
write_fwf_blaise_with_model(df_enum, outfile_wrong_model, blafile4)
#> Error in cast_type(variables(model)[zonder_dummy][[i]], df[[i]]): numbers in dataframe column (A;B;C) do not correspond to range of indices in model (A;B) for variable A