15

When I am working with graphs in R, I try to output them directly into PDF. For e.g. the code for creating a graph is as follows.

library(ggplot2)
levels_fp = ggplot(data=df_forest_2,
    aes(x = levels,y=center, ymin=lower, ymax=upper))+
    geom_pointrange(aes(col=levels))+
    geom_hline(aes(fill=levels),yintercept =1, linetype=2)+
    xlab('Outcomes Evaluated')+ geom_errorbar(aes(ymin=lower, ymax=upper,col=levels),width=0.2,cex=1)+ 
    facet_wrap(~outcome,strip.position="top",nrow=1,scales = "free_x") + theme(
    axis.title = element_text(size = 18),
    axis.text = element_text(size = 14),
    legend.text = element_text(size = 16),
    legend.title = element_text(size = 16),
  
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
      
    axis.line = element_line(colour = "black"),
    legend.box.background = element_rect(),axis.text.x = element_blank(), strip.text.x = element_text(size = 14)
  ) + ylab("Odds Ratio")+labs(col="Interventions")

pdf(file="Forest_levels_2.pdf", width=12,height =12 ) 
levels_fp
dev.off()

The thing I am intrigued about is: suppose I have kept the pdf (which I output from the previous step, namely Forest_levels_2.pdf) open in a PDF viewer. Then, if I run the code again, the file gets output and overwrites the previous PDF, and the output in PDF viewer is also instantly changed.

But if I try to do the same in Windows, that is I have kept the PDF open with a PDF viewer, and I try to run the code, the following error will come --> cannot access pdf. Why there is a difference in behavior in Linux (I am working with Ubuntu 20.04)?

Adding the output of the Windows error

Error in pdf(file = "Forest_levels_2.pdf", width = 12, height = 12) : 
  cannot open file 'Forest_levels_2.pdf'

In order to remove some variables, I tried to download the Evince PDF viewer for windows (Evince 2.32) and then opened the PDF using Evince in Windows and did the same experiment again.

Guess what happened!?!

I was able to successfully modify the open PDF in Windows. Evince works same in both Ubuntu and Windows. So probably, it is more dependent on the property of PDF viewer also.

8
  • If I do understand you correctly your are aware of the specific file monitoring capabilities of the particular PDF viewer, but wonder if there is a fundamental difference on the file system level? Is the Windows behaviour temporary, or does it crash the viewer? Did you try it with different viewers? Who issues the error? R or the PDF viewer? – Don Question Aug 27 '20 at 9:10
  • 1
    @DonQuestion In windows it does not crash the PDF viewer, but it will never ever allow R to write in it when it is open. I have not tried with different viewer in Windows. Will do it. The error is issued by R that the file is unable to be accessed in Windows. In Ubuntu, no such error surfaces. – Praveen Kumar-M Aug 27 '20 at 9:49
  • I think you discovered something very interesting and to share it with a greater audience you should edit your initial qestion. Maybe add the error message to it, e.g.': Cannot access pdf under Windows while Linux allows writes to an open PDF file? – Don Question Aug 27 '20 at 9:54
  • 1
    @DonQuestion I have added the error of PDF Viewer in Windows – Praveen Kumar-M Aug 28 '20 at 11:27
  • 1
    @JörgWMittag I have answered your question in the new edit – Praveen Kumar-M Aug 28 '20 at 11:47
40

This is a function of the PDF viewer you are using.

I suspect, the PDF viewer opens the file, and then reads/processes the contents. It does not have to close the file, but it might.

Next, it sets up an inotify, meaning that if someone writes to the file, your PDF viewer will be notified.

When you wrote to the file, your PDF viewer received a signal that the file it was watching changed. This gives the PDF viewer a chance to re-read the file and re-process the contents.

Something similar is possible in Windows, but the PDF viewer you're using simply hasn't implemented that. When your Windows PDF viewer opens a PDF, it gets exclusive access by default to the PDF file which will cause another program to get an error when they attempt to open the file for writing. The viewer would have to make system calls to explicitly drop the exclusive access in order to operate in a similar manner to the linux version.


I found an interesting wikipedia article on File locking. It says:

Windows inherits the semantics of share-access controls from the MS-DOS system, where sharing was introduced in MS-DOS 3.3 . Thus, an application must explicitly allow sharing when it opens a file; otherwise it has exclusive read, write, and delete access to the file until closed (other types of access, such as those to retrieve the attributes of a file are allowed.)

It also says:

Unix-like operating systems (including Linux and Apple's macOS) do not normally automatically lock open files.

17
  • 33
    The latter part is the most significant in my mind; by default, Windows locks open files, which causes the error mentioned in the question. – Stephen Kitt Aug 27 '20 at 9:21
  • 4
    The Adobe reader behaviour is a deliberate design choice: The pdf file is locked and only partly read into memory. This allows Reader to handle very large pdf files. Evince on the other hand will read the whole file into memory, which can create problems on systems which are short on RAM. (from memory, though I recall the source seemed a reliable one: adobe developers or similar) – James K Aug 27 '20 at 20:58
  • 8
    That point about file locking is rather significant. In most cases, you cannot replace an open file on Windows because of this behavior (and this is why you have to reboot for every last little OS update on Windows), but you can pretty much always do so on UNIX-like systems (possibly even if the file is locked depending on the specifics). – Austin Hemmelgarn Aug 27 '20 at 22:38
  • 5
    @ChrisH SumatraPDF is a PDF viewer for windows that does not lock the PDF files, if it's of any interest. – VisualMelon Aug 28 '20 at 8:07
  • 3
    Adding to @VisualMelon's comment, SumatraPDF is installed with RStudio, so it might actually be already available on the OP's system. SumatraPDF is my personal non-locking PDF viewer of choice for viewing PDFs while editing them (using R or LaTeX). – CL. Aug 28 '20 at 9:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.