Skip to content

sink(): Cannot open file

12 messages · G.Maubach at weinwolf.de, Jim Lemon, John Sorkin +3 more

#
Hi All,

I would like to route the output to a file using sink(). When using the 
example from the ?sink documentation:

sink("sink-examp.txt")
i <- 1:10
outer(i, i, "*")
sink()
unlink("sink-examp.txt")

## capture all the output to a file.
zz <- file("all.Rout", open = "wt")
sink(zz)
sink(zz, type = "message")
try(log("a"))
## back to the console
sink(type = "message")
sink()
file.show("all.Rout")

I can not open the file in Windows Explorer. The error message is:

"Cannot open file. File is in use be another proces."

How can I close the file in a manner that I can open it right after it was 
created?

Kind regards

Georg
#
Hi Georg,
I don't suppose that you have:

1) checked that the file "all.Rout" exists somewhere?

2) if so, looked at the file with Notepad, perhaps?

3) let us in on the secret by pasting the contents of "all.Rout" into
your message if it is not too big?

At a guess, trying:

 close(zz)

might get you there.

Jim
On Tue, May 10, 2016 at 5:25 PM, <G.Maubach at weinwolf.de> wrote:
#
Hi Jim,

thanks for your reply.

ad 1)
"all.Rout" was created in the correct directory. It exists properly with 
correct file properties on Windows, e.g. creation date and time and file 
size information.

ad 2)
I can not access the file with Notepad.exe directly after it was created 
by R.  The error message is (translated):

"Cannot access file "all.Rout". The file is opened by another process."

ad 3)
If I close R completely the file access is released. Then I can read the 
file using Notepad.exe. The contents is:

Error in log("a") : non-numeric argument to mathematical function

I tried

close(zz)

but the error persists.

To me it looks like R is still accessing the file and not releasing the 
connection for other programs. close(zz) should have solved the problem 
but unfortantely it doesn't.

What else could I try?

Kind regards

Georg




Von:    Jim Lemon <drjimlemon at gmail.com>
An:     G.Maubach at weinwolf.de, 
Kopie:  r-help mailing list <r-help at r-project.org>
Datum:  10.05.2016 12:50
Betreff:        Re: [R] sink(): Cannot open file



Hi Georg,
I don't suppose that you have:

1) checked that the file "all.Rout" exists somewhere?

2) if so, looked at the file with Notepad, perhaps?

3) let us in on the secret by pasting the contents of "all.Rout" into
your message if it is not too big?

At a guess, trying:

 close(zz)

might get you there.

Jim
On Tue, May 10, 2016 at 5:25 PM, <G.Maubach at weinwolf.de> wrote:
was
http://www.R-project.org/posting-guide.html
#
Have you tried:

sink("all.Rout")
try(log("a"))
sink()

Jim
On Tue, May 10, 2016 at 9:05 PM, <G.Maubach at weinwolf.de> wrote:
#
Hi Jim,

I tried:

sink("all.Rout")
try(log("a"))
sink()

The program executes without warning or error. The file "all.Rout" is 
begin created. Nothing will be written to it. The file is accessable 
rights after the execution of the program by notepad.exe.

The program

zz <- file("all.Rout", open = "wt")
sink(zz, type = "message")
try(log("a"))
sink()
close(zz)
unlink(zz)

creates the file, does not write anything to it and is not accessable 
after program execution in R with notepad.exe.

Any ideas what happens behind the szenes?

Kind regards

Georg




Von:    Jim Lemon <drjimlemon at gmail.com>
An:     G.Maubach at weinwolf.de, 
Kopie:  r-help mailing list <r-help at r-project.org>
Datum:  10.05.2016 13:16
Betreff:        Re: Re: [R] sink(): Cannot open file



Have you tried:

sink("all.Rout")
try(log("a"))
sink()

Jim
On Tue, May 10, 2016 at 9:05 PM, <G.Maubach at weinwolf.de> wrote:
#
George,
I do not know what operating system you are working with, but when I use sink() under windows, I need to specify a valid path which I don't see in your code. I might, for example specify:

sink("c:\myfile.txt")
 R code goes here
sink()

with the expectation that I would create a file myfile.txt that would contain the output of my R program.
 
John


John David Sorkin M.D., Ph.D.
Professor of Medicine
Chief, Biostatistics and Informatics
University of Maryland School of Medicine Division of Gerontology and Geriatric Medicine
Baltimore VA Medical Center
10 North Greene Street
GRECC (BT/18/GR)
Baltimore, MD 21201-1524
(Phone) 410-605-7119
(Fax) 410-605-7913 (Please call phone number above prior to faxing)
Hi Jim,

I tried:

sink("all.Rout")
try(log("a"))
sink()

The program executes without warning or error. The file "all.Rout" is 
begin created. Nothing will be written to it. The file is accessable 
rights after the execution of the program by notepad.exe.

The program

zz <- file("all.Rout", open = "wt")
sink(zz, type = "message")
try(log("a"))
sink()
close(zz)
unlink(zz)

creates the file, does not write anything to it and is not accessable 
after program execution in R with notepad.exe.

Any ideas what happens behind the szenes?

Kind regards

Georg




Von: Jim Lemon <drjimlemon at gmail.com>
An: G.Maubach at weinwolf.de, 
Kopie: r-help mailing list <r-help at r-project.org>
Datum: 10.05.2016 13:16
Betreff: Re: Re: [R] sink(): Cannot open file



Have you tried:

sink("all.Rout")
try(log("a"))
sink()

Jim
On Tue, May 10, 2016 at 9:05 PM, <G.Maubach at weinwolf.de> wrote:
______________________________________________
R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Confidentiality Statement:
This email message, including any attachments, is for the sole use of the intended recipient(s) and may contain confidential and privileged information. Any unauthorized use, disclosure or distribution is prohibited. If you are not the intended recipient, please contact the sender by reply email and destroy all copies of the original message.
#
Try closing the type of sink you're actually opening:


zz <- file("all.Rout", open = "wt")
sink(zz, type = "message")
try(log("a"))
sink(type = "message")
close(zz)
unlink(zz)


If you look carefully at the example in?sink, there are two close
statements, one for each stream being sent to that file.

Sarah
On Tue, May 10, 2016 at 11:05 AM, <G.Maubach at weinwolf.de> wrote:
#
Hi Sarah, John, Jim,
Hi All,

I have set my envrionment variable 

path <- file.path("H:", "2016", "Analysis")
setwd(dir = path)

This works well cause the file is created in that directory.

I have tried

close(zz)
unlink(zz)

and neither worked nor did it work out using them together.

I had this before when working with IBM SPSS Statistics. There was a 
workaround for the problem in SPSS.

Is there one for R?

Kind regards

Georg





Von:    Sarah Goslee <sarah.goslee at gmail.com>
An:     G.Maubach at weinwolf.de, 
Kopie:  r-help mailing list <r-help at r-project.org>
Datum:  10.05.2016 17:17
Betreff:        Re: [R] Antwort: Re: Re: sink(): Cannot open file



Try closing the type of sink you're actually opening:


zz <- file("all.Rout", open = "wt")
sink(zz, type = "message")
try(log("a"))
sink(type = "message")
close(zz)
unlink(zz)


If you look carefully at the example in?sink, there are two close
statements, one for each stream being sent to that file.

Sarah

----- Weitergeleitet von Georg Maubach/WWBO/WW/HAW am 10.05.2016 18:29 
-----

Von:    "John Sorkin" <jsorkin at grecc.umaryland.edu>
An:     <drjimlemon at gmail.com>, <G.Maubach at weinwolf.de>, 
Kopie:  <r-help at r-project.org>
Datum:  10.05.2016 17:20
Betreff:        Re: [R] Antwort: Re: Re: sink(): Cannot open file



George,
I do not know what operating system you are working with, but when I use 
sink() under windows, I need to specify a valid path which I don't see in 
your code. I might, for example specify:

sink("c:\myfile.txt")
 R code goes here
sink()
with the expectation that I would create a file myfile.txt that would 
contain the output of my R program.
 
John


John David Sorkin M.D., Ph.D.
Professor of Medicine
Chief, Biostatistics and Informatics
University of Maryland School of Medicine Division of Gerontology and 
Geriatric Medicine
Baltimore VA Medical Center
10 North Greene Street
GRECC (BT/18/GR)
Baltimore, MD 21201-1524
(Phone) 410-605-7119
(Fax) 410-605-7913 (Please call phone number above prior to faxing)
On Tue, May 10, 2016 at 11:05 AM, <G.Maubach at weinwolf.de> wrote:
#
On 10/05/2016 11:15 AM, John Sorkin wrote:
Note that the backslash should be doubled (so it isn't interpreted as an 
escape for the "m" that follows it), or replaced with a forward slash.

Duncan Murdoch
#
Duncan,

thanks for the hint.

I have done it correctly in R fashion

## capture all the output to a file.
zz <- file("C:/Temp/all.Rout", open = "wt")
sink(zz)
sink(zz, type = "message")
try(log("a"))
## back to the console
sink(type = "message")
sink()
unlink("C:/Temp/all.Rout")

But the error persits.

Kind regards

Georg




Von:    Duncan Murdoch <murdoch.duncan at gmail.com>
An:     John Sorkin <jsorkin at grecc.umaryland.edu>, drjimlemon at gmail.com, 
G.Maubach at weinwolf.de, 
Kopie:  r-help at r-project.org
Datum:  10.05.2016 19:03
Betreff:        Re: [R] Antwort: Re: Re: sink(): Cannot open file
On 10/05/2016 11:15 AM, John Sorkin wrote:
sink() under windows, I need to specify a valid path which I don't see in 
your code. I might, for example specify:
Note that the backslash should be doubled (so it isn't interpreted as an 
escape for the "m" that follows it), or replaced with a forward slash.

Duncan Murdoch
contain the output of my R program.
Geriatric Medicine
with
file
created
process."
the
the
problem
the
it
http://www.R-project.org/posting-guide.html
#
Sounds like it would be helpful to find out exactly which process is
holding on to the file in order to figure out what's going on. From a
quick look, it seems that

  http://superuser.com/questions/117902/find-out-which-process-is-locking-a-file-or-folder-in-windows

gives some useful info on how to track down the process that looks the file.

/Henrik
On Wed, May 11, 2016 at 9:47 AM, <G.Maubach at weinwolf.de> wrote:
#
Hi Henrik, Jim, Sarah, Duncan,
Hi All,

I have tried the built-in solution using PowerShell:

$lockedFile="C:\Windows\System32\wshtcpip.dll" 
Get-Process | foreach{$processVar = $_;$_.Modules | foreach{if($_.FileName 
-eq $lockedFile){$processVar.Name + " PID:" + $processVar.id}}}

It did not show any processes.

Then I tried the solution using "RessourceMonitor". There I found two 
processes:

rstudio.exe
rsession.exe

Right-clicking on rstudio.exe and selecting "Warteschlange analysieren" (= 
analyse queue?) showed nothing. Right-clicking on rsession.exe and 
selecting "Warteschlage" said:

"Mindestens ein Thread von rsession.exe wartet auf die Fertigstellung von 
Netzwerk E/A". (= "At least one thread of "rsession.exe" is waiting for 
finishing a network i/o operation").

Putting rsession.exe into the search field of the handles tap of 
RessourceMonitor gave no results. No handles were identified.

I can not follow the suggestions where installation of software is 
required due to security rules of the company I work for.

I had a look at different R versions on my machine:

1) R i386 3.2.2
2) R i386 3.2.4 (revised)
3) R i386 3.2.5
4) R x54 3.2.2
5) R x64 3.2.4 (revised)
6) R x64 3.2.5

I did 

## capture all the output to a file.
zz <- file("C:/Temp/all.Rout", open = "wt")
sink(zz)
sink(zz, type = "message")
try(log("a"))
## back to the console
sink(type = "message")
sink()
unlink("C:/Temp/all.Rout")

on R i386 3.2.2 and R x64 3.2.2 directly without RStudio. In both cases 
the file was locked.

Adding

close(zz)

solved the problem in both versions.

Encouraged by this I tired (successivly refered to as "complete code")

## capture all the output to a file.
zz <- file("C:/Temp/all.Rout", open = "wt")
sink(zz)
sink(zz, type = "message")
try(log("a"))
## back to the console
sink(type = "message")
sink()
unlink("C:/Temp/all.Rout")
close(zz)

on R i386 3.2.4 (revised) and R x64 3.2.4 (revised) without RStudio. Works 
in both cases. The same with R i386 3.2.5 and R x64 3.2.5 each without 
RStudio.

It did the same with RStudio altering the R version in the RStudio session 
using "complete code". The results are:

R i386 3.2.2: OK
R. x64 3.2.2: OK
R i386 3.2.4 (revised): OK
R x64 3.2.4 (revised): OK
R i386 3.2.5: OK
R x64 3.2.5: OK

This got me lost. I had tried the complete code the last days a hundred 
times. It never worked.

Then I restarted my machine powering up RStudio x64 3.2.5 using the 
"complete code" and ... it worked.

I have no idea what was wrong the last days.

As far as I can say today the documentation of ?sink in R is currently

## capture all the output to a file.
zz <- file("all.Rout", open = "wt")
sink(zz)
sink(zz, type = "message")
try(log("a"))
## back to the console
sink(type = "message")
sink()
file.show("all.Rout")

and should be - in my opinion  - supplemented with

close(zz).

Any thoughts?

Kind regards

Georg




Von:    Henrik Bengtsson <henrik.bengtsson at gmail.com>
An:     G.Maubach at weinwolf.de, 
Kopie:  Duncan Murdoch <murdoch.duncan at gmail.com>, "r-help at r-project.org" 
<r-help at r-project.org>
Datum:  11.05.2016 21:48
Betreff:        Re: [R] Antwort: Re: Antwort: Re: Re: sink(): Cannot open 
file



Sounds like it would be helpful to find out exactly which process is
holding on to the file in order to figure out what's going on. From a
quick look, it seems that

  
http://superuser.com/questions/117902/find-out-which-process-is-locking-a-file-or-folder-in-windows


gives some useful info on how to track down the process that looks the 
file.

/Henrik
On Wed, May 11, 2016 at 9:47 AM, <G.Maubach at weinwolf.de> wrote:
use
in
http://www.R-project.org/posting-guide.html