Just for completeness, This can be easily illustrated simply in R, no C code 
needed:

cat("foo\n\rbar\n")

In unix terminal:

> cat("foo\n\rbar\n")
foo
bar

In Mac-GUI:

> cat("foo\n\rbar\n")
bar


Cheers,
Simon



> On Apr 17, 2021, at 20:29, Simon Urbanek <simon.urba...@r-project.org> wrote:
> 
> Ah, so you're not using R console, you're using the R.app Mac-GUI. That one 
> is not a terminal, so it has entirely different rules, because it is 
> combining all four streams (stdout, stderr and WriteConsole for both output 
> and message). Also for historical reasons macOS (the original) used to use \r 
> as newline (unix has \n and Windows has \r\n) - for that reason your 
> combination of \n\r doesn't work since it is ambiguous in the Mac context and 
> treated as CR. If you want a proper newline, you can change your example to 
> something like
> 
>   REprintf("\n   Processing data chunk %d of 3\n ",k+1);
> 
> Which makes sure the \n is interpreted as \r\n first and only then you follow 
> with \r. I suppose we could sunset the special handling of \r since it is 
> likely quite rare to see Mac line endings these days... you could file an 
> issue against Mac-GUI. 
> 
> Cheers,
> Simon
> 
> 
> 
>> On Apr 17, 2021, at 19:26, Morgan Morgan <morgan.email...@gmail.com> wrote:
>> 
>> Hi Simon,
>> Thank you for the feedback.
>> It is really strange that you have a different output.
>> I have attached a picture of my R console.
>> I am just trying to port some pure C code that prints progress bars to R but 
>> it does not seem to be printing properly.
>> It seems I am doing something wrong with REprintf and R_FlushConsole.
>> Best regards,
>> Morgan
>> 
>> On Sat, Apr 17, 2021 at 12:36 AM Simon Urbanek <simon.urba...@r-project.org> 
>> wrote:
>> Sorry, unable to reproduce on macOS, in R console:
>> 
>>> dyn.load("test.so")
>>> .Call("printtest",1e4L)
>> 
>>   Processing data chunk 1 of 3
>> [==============================] 100%
>> 
>>   Processing data chunk 2 of 3
>> [==============================] 100%
>> 
>>   Processing data chunk 3 of 3
>> [==============================] 100%
>> NULL
>> 
>> But honestly I'm not sure sure I understand the report. R_FlushConsole is a 
>> no-op for terminal console and your code just prints on stderr anyway (which 
>> is not buffered). All this does is just a lot of \r output (which is highly 
>> inefficient anywhere but in Terminal by definition). Can you clarify what 
>> the code tries to trigger?
>> 
>> Cheers,
>> Simon
>> 
>> 
>>> On Apr 16, 2021, at 23:11, Morgan Morgan <morgan.email...@gmail.com> wrote:
>>> 
>>> Hi,
>>> 
>>> I am getting a really weird behaviour with the R console.
>>> Here is the code to reproduce it.
>>> 
>>> 1/ C code: ---------------------------------------------------
>>> 
>>> SEXP printtest(SEXP x) {
>>> const int PBWIDTH = 30, loop = INTEGER(x)[0];
>>> int val, lpad;
>>> double perc;
>>> char PBSTR[PBWIDTH], PBOUT[PBWIDTH];
>>> memset(PBSTR,'=', sizeof(PBSTR));
>>> memset(PBOUT,'-', sizeof(PBOUT));
>>> for (int k = 0; k < 3; ++k) {
>>>   REprintf("\n   Processing data chunk %d of 3\n",k+1);
>>>   for (int i = 0; i < loop; ++i) {
>>>     perc = (double) i/(loop-1);
>>>     val  = (int) (perc * 100);
>>>     lpad = (int) (perc * PBWIDTH);
>>>     REprintf("\r [%.*s%.*s] %3d%%", lpad, PBSTR, PBWIDTH - lpad, PBOUT,
>>> val);
>>>     R_FlushConsole();
>>>   }
>>>   REprintf("\n");
>>> }
>>> return R_NilValue;
>>> }
>>> 
>>> 2/ Build so/dll: ---------------------------------------------------
>>> 
>>> R CMD SHLIB
>>> 
>>> 3/ Run code :  ---------------------------------------------------
>>> 
>>> dyn.load("test.so")
>>> .Call("printtest",1e4L)
>>> dyn.unload("test.so")
>>> 
>>> 4/ Issue:  ---------------------------------------------------
>>> If you run the above code in RStudio, it works well both on Mac and Windows.
>>> If you run it in Windows cmd, it is slow.
>>> If you run it in Windows RGui, it is slow but also all texts are flushed.
>>> If you run it in Mac terminal, it runs perfectly.
>>> If you run it in Mac R Console, it prints something like :
>>>> .Call("printtest",1e4L)
>>> [==============================] 100%NULL----------------------------]   0%
>>> 
>>> I am using R 4.0.4 (Mac) / 4.0.5 (Windows)
>>> 
>>> Is that a bug or am I doing something wrong?
>>> 
>>> Thank you
>>> Best regards,
>>> Morgan
>>> 
>>>      [[alternative HTML version deleted]]
>>> 
>>> ______________________________________________
>>> R-devel@r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-devel
>>> 
>> 
>> <Screenshot 2021-04-17.png>
> 

______________________________________________
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel

Reply via email to