[Rd] Producing different text formats in R

Liuis@Hurt@do m@iii@g oii uv@es Liuis@Hurt@do m@iii@g oii uv@es
Thu Aug 8 11:38:42 CEST 2019


Dear all,

I am facing a strange problem with text files formats.

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

<id> <x> <y> <z>

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

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.

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:

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

And then calling:

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

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

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

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

Thank you in advance,

Lluís Hurtado-Gil

-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: myfile_cpp.txt
URL: <https://stat.ethz.ch/pipermail/r-devel/attachments/20190808/465ee6d9/attachment-0002.txt>

-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: myfile_r.txt
URL: <https://stat.ethz.ch/pipermail/r-devel/attachments/20190808/465ee6d9/attachment-0003.txt>


More information about the R-devel mailing list