0

Creating a png file with the png device fails because of long filenames but the names are shorter than the supported size according to documentation.

I am creating graphs from various subsets of hierarchical data in a dataframe with the treemap library. For a better overview i create file directories mirroring that hierarchical structure and save each graph in the corresponding directory.

I have written a function that removes illegal characters and specific german characters that seem to trouble the png device. However the most recent error message seems to be related to the length of the filename in the png command. According to the documentation the maximum length of the filename is 511 characters which i am not close to. I have three questions

  1. Is the length of the filename really the issue?
  2. If so, why when the length is considerably lower than it has to be according to documentation?
  3. What would be a elegant, suitable workaround?
    Breite<-1366
    Hohe<-768

    ###This works:

    png(file="b4-Kapitel 4- Funktionen des kardiovaskulaeren, 
    haematologischen, Immun- und Atmungssystems_1366x768.png",
    width=Breite,
    height=Hohe)

    png(file="ICFGrafiken/Klassifikation der Koerperfunktionen/Kapitel 4- 
    Funktionen des kardiovaskulaeren, haematologischen, Immun- und 
    Atmungssystems/b4-Kapitel 4_1366x768.png",
    width=Breite,
    height=Hohe)

    ###This doesn't work:

    png(file="ICFGrafiken/Klassifikation der Koerperfunktionen/Kapitel 4- 
    Funktionen des kardiovaskulaeren, haematologischen, Immun- und 
    Atmungssystems/b4-Kapitel 4- Funktionen des kardiovaskulaeren, 
    haematologischen, Immun- und Atmungssystems_1366x768.png",
    width=Breite,
    height=Hohe)

The error message is:

Error in png(file = "ICFGrafiken/Klassifikation der Koerperfunktionen/Kapitel 4- Funktionen des kardiovaskulaeren, haematologischen, Immun- und Atmungssystems/b4-Kapitel 4- Funktionen des kardiovaskulaeren, haematologischen, Immun- und Atmungssystems_1366x768.png",  : 
  kann png()-Gerät nicht starten
In addition: Warning messages:
1: In png(file = "ICFGrafiken/Klassifikation der Koerperfunktionen/Kapitel 4- Funktionen des kardiovaskulaeren, haematologischen, Immun- und Atmungssystems/b4-Kapitel 4- Funktionen des kardiovaskulaeren, haematologischen, Immun- und Atmungssystems_1366x768.png",  :
  kann Datei 'ICFGrafiken/Klassifikation der Koerperfunktionen/Kapitel 4- Funktionen des kardiovaskulaeren, haematologischen, Immun- und Atmungssystems/b4-Kapitel 4- Funktionen des kardiovaskulaeren, haematologischen, Immun- und Atmungssystems_1366x768.png' nicht zum Schreiben öffnen
2: In png(file = "ICFGrafiken/Klassifikation der Koerperfunktionen/Kapitel 4- Funktionen des kardiovaskulaeren, haematologischen, Immun- und Atmungssystems/b4-Kapitel 4- Funktionen des kardiovaskulaeren, haematologischen, Immun- und Atmungssystems_1366x768.png",  :
  opening device failed
  • Create the files in a temporary directory and then move them, e.g., using dir.create and file.copy? – Roland Jun 17 '19 at 07:37
  • Eventually you can use `getwd()` and `setwd()` – jogo Jun 17 '19 at 07:39
  • A workaround could also be to create a `data.frame` conatining a short-handle and the long name of your pngs and to then just save with the short-handle (e.g. b4_Kap4_1.png or sth. comparable). That would of course mean, that you might need to look the names up. How long is the rest of your path, the part in front of your relative path shown here. Is that maybe the source of your extra characters? – Max Teflon Jun 17 '19 at 08:08
  • @ Roland using a temporary directory seemed to be the "unelegant" solution to me, but looks like what i have to do, – Steve Piller Jun 17 '19 at 08:33
  • @jogo i tried to create the directory and setwd() for each graph but this produced an error and i seem to remember being discourage to use these commands (though i dont recall, the specific reasons right now) – Steve Piller Jun 17 '19 at 08:34
  • @MaxTeflon The short handle thing seems to be a Solution, since the alphnumerical code b4 already is one and the structure can be reproduced from these codes. I was hesitant to do that, since the intended purpose is a helpdesk for which a Full descripition would be good. But i guess the description doesn't have to be in the filename. Also good point, i wasn't considereing the non-relative part of the path. You might be onto something. – Steve Piller Jun 17 '19 at 08:38
  • @MaxTeflon The length of the whole path is 289 characters, still way below the threshold of 511. So that shouldnt be the problem but it seems it is. – Steve Piller Jun 17 '19 at 09:41

1 Answers1

2

The Problem is not related to RStudio or the png command. Windows has a 260 character limit for filepaths.

Credit goes to the linked Question: Long path/filename in windows makes write.table() error out in R

So there are 3 possible Solutions (or combinations of these 3)

  • Manually enable the LongPath support with Windows 10 by Editing the registry (which i did but this seems not enough and an entirely new Problem)
  • Setting up a virtual drive as suggested in the answer above, to gain a certain amount of "breathing room" (unlikely to get me enough)
  • Using shorthands for the Titles and Labels and providing the full description only in the application where they are used and needed.

Seems like Nr.3 for me. Thanks to everyone who contributed.