Reputation: 825
I just encountered something baffling (at least to me) and hope wiser members can shed some light.
I used RStudio 0.98.490 on Windows XP to save a plot to PNG. The filename was created using strwrap(sprintf())
. I habitually use longer (i.e. more informative) filenames, and I guess I was probably over-enthusiastic this time, which is why this issue surfaced.
I noticed that when the width of the RStudio console is shorter than the length of the filename during run-time, the latter gets truncated and the file created does not have the .png extension. I experimented and dragged the width of RStudio console to longer than the filename - the problem disappears.
My question: why does this happen? More importantly, can I resolve this truncation without changes to my filename? I am a newbie to R and I can't see why 2 seemingly unrelated items should interact.
Truncation shown below:
> writeLines( paste0(FName, " generated") ) # Write to Console
aaaaaa aaaaaaaaaaaaaabcdef ghijk lmnopqrstuvaaaaaa aaaaaaaaaaaaa213424534aaaaaa generated
aaaaaaaaaaaaa.png generated
>
Sample code is attached below:
astring <- "aaaaaa aaaaaaaaaaaaa"
FName <- strwrap( sprintf("%sabcdef ghijk lmnopqrstuv%s213424534%s.png",
astring, astring, astring) ) # simulate long filename
png( filename = FName)
a <- rnorm(100)
b <- rnorm(100)*2
plot(b,a)
dev.off()
writeLines( paste0(FName, " generated") ) # Write to Console
The closest resource I found was https://stackoverflow.com/questions/6104448/preserving-long-comments-in-console-output-not-falling-victim-to-truncat but the problem faced by the author appeared slightly different.
I would appreciate very much if someone can enlighten. Thanks!
EDIT: Thanks to @jlhoward, I looked up strwrap()
and found the width parameter. By assigning '255' (or any big integer), the problem is resolved.
Upvotes: 0
Views: 345
Reputation: 59425
Why are you using strwrap(...)
?
As the documentation explains, strwrap(...)
parses your input into words, and then wraps (by inserting "\n"
) based on a width parameter. The default for this parameter is getOption("width")
, which is based on the console width. Try typing
getOption("width")
then shrink or expand your console window and do it again.
If you just use sprintf(...)
to generate your filename, you don't have this problem.
Upvotes: 0