From e@ @end|ng |rom enr|co@chum@nn@net  Thu Aug  8 14:46:50 2019
From: e@ @end|ng |rom enr|co@chum@nn@net (Enrico Schumann)
Date: Thu, 08 Aug 2019 14:46:50 +0200
Subject: [Rd] Producing different text formats in R
In-Reply-To: <4455417873hurgil@uv.es> (Lluis Hurtado's message of "Thu, 8 Aug
 2019 11:38:42 +0200 (CEST)")
References: <4455417873hurgil@uv.es>
Message-ID: <87o90zomdh.fsf@enricoschumann.net>

>>>>> "Llu?s" ==   <Lluis.Hurtado at uv.es> writes:

    Llu?s> Dear all,
    Llu?s> I am facing a strange problem with text files formats.

    Llu?s> I am currently using a C++ code named voro++ (http://math.lbl.gov/voro++/). This code accepts text files with four columns:

    Llu?s> <id> <x> <y> <z>

    Llu?s> where id is an identification number and x,y,z are the coordinates of a point in a 3D space.

    Llu?s> The input file, name it myfile_cpp.txt, is generated by another C++ code written by myself (named quadscheme.cpp). So far, these calculations involve no R code and it works just fine.

    Llu?s> However, now I am trying to replace my C++ code quadscheme.cpp by a R function. The four columns (id,x,y,z) are produced in a matrix or Data Frame and then saved in a file myfile_r.txt using write.table(). For example using the following function:

    Llu?s> quadscheme <- function(window, ntile) {
    Llu?s>   # Creates a grid of points in a 3D orthohedron. 
    Llu?s>   # First point lies at the (mix,miny,miz) corner of the volume and the last one at (maxx,maxy,maxz)
    Llu?s>   # window: vector. Contains the minimum and maximum values of a 3D orthohedron
    Llu?s>   #       window <- c(minx, maxx, miny, maxy, minz, maxz)
    Llu?s>   # ntile: vector. Number of points per dimension minus one. We manually add a extra row of points per dimension
    Llu?s>   #       ntile <- c(nstepx, nstepy, nstepz)
    Llu?s>   M <- prod(ntile+1) 
    Llu?s>   mat <- matrix(NA,M,4)
    Llu?s>   mat[,1] <- 1:M # id column
  
    Llu?s>   # step length per dimension
    Llu?s>   hi <- (window[2] - window[1])/ntile[1]
    Llu?s>   hj <- (window[4] - window[3])/ntile[2]
    Llu?s>   hk <- (window[6] - window[5])/ntile[3]
  
    Llu?s>   c <- 1
    Llu?s>   for(i in 0:ntile[1]) {
    Llu?s>     x <- i*hi + window[1]
    Llu?s>     for(j in 0:ntile[2]) {
    Llu?s>       y <- hj*j + window[3]
    Llu?s>       for(k in 0:ntile[3]) {
    Llu?s>         z <- k*hk + window[5]
    Llu?s>         mat[c,2:4] <- c(x,y,z) # Add a new point to the grid
    Llu?s>         c <- c + 1
    Llu?s>       }
    Llu?s>     }
    Llu?s>   }
    Llu?s>   write.table(mat, file="myfile_r.txt", row.names=FALSE, col.names=FALSE)
    Llu?s> }

    Llu?s> And then calling:

    >> window <- c(18, 43, 171, 196, 95, 102)
    >> ntile <- c(100,100,28)
    >> quadscheme(window, ntile)

    Llu?s> I see no difference between both myfile_*.txt files,
    Llu?s> one is created with C++ and the other one with
    Llu?s> R. However, function voro++ do not accept the later one
    Llu?s> (created with R and saved with write.table). I've also
    Llu?s> noted that voro++ usually accepts R generated files
    Llu?s> when they are small enough, but it accepts all C++
    Llu?s> generated files, regardless the size.

    Llu?s> I know this is more a C++ than a R question, but may be
    Llu?s> you can help me as well. I suspect that even if both
    Llu?s> files are *.txt they have some differences that I
    Llu?s> cannot see. Is there any way in R to produce different
    Llu?s> kinds of txt files? Like binary instead of text files?
    Llu?s> Could this be the problem?

    Llu?s> I attach two identical files, one produced by C++
    Llu?s> (myfile_cpp.txt) and another one by the previous R
    Llu?s> function (myfile_r.txt). I attach as well the C++ code
    Llu?s> used to generate myfile_cpp.txt.

    Llu?s> Thank you in advance,

    Llu?s> Llu?s Hurtado-Gil

In your R file, scientific notation is used:

R:
  99999 26.5 174.5 96.5
  1e+05 26.5 174.5 96.75
  100001 26.5 174.5 97

cpp:
  99999 26.5 174.5 96.5
  100000 26.5 174.5 96.75
  100001 26.5 174.5 97

Try setting options(scipen = 20) before you write the
file in R.



-- 
Enrico Schumann
Lucerne, Switzerland
http://enricoschumann.net



[Rd] Producing different text formats in R
Enrico Schumann