开发者

How do you retrieve the title of a plot device window?

开发者 https://www.devze.com 2023-03-17 12:08 出处:网络
You can set the title of a plot device window with windows(title = \"The title\") #or equivalently x11(title = \"The title\")

You can set the title of a plot device window with

windows(title = "The title")
#or equivalently
x11(title = "The title")

How you retrieve the title from a plot device window?

names(dev.开发者_JAVA技巧cur()), attributes(dev.cur()), str(dev.cur()) and unclass(dev.cur()) don't reveal anything useful.


It shouldn't be possible from device properties cause it's window property.

Under windows you could try mess with names(getWindowsHandles()) which gives me:

> names(getWindowsHandles())
[1] "R Console"
[2] "The title (ACTIVE)"
[3] "R Information"

E.g. for active device grep("\\(ACTIVE\\)$", names(getWindowsHandles()), value=TRUE) return the title.


It was easier than I thought:

getTitle <- function(dev=dev.cur()) {
    all_pointers <- getWindowsHandles(which="R", minimized=TRUE)
    all_pointers <- sapply(all_pointers, deparse)
    to_find <- deparse(getWindowsHandle(dev))
    if (to_find=="NULL") {
        warning("Device not found")
        NULL
    } else {
        names(all_pointers)[to_find==all_pointers]
    }
}

Now some tests:

> getTitle()
Warning in getTitle() : Device not found
NULL
> windows(title="Test window one")
> getTitle()
[1] "Test window one (ACTIVE)"
> getTitle(3)
Warning in getTitle(3) : Device not found
NULL
> windows(title="Test window two")
> windows(title="Test window three")
> sapply(dev.list(), getTitle)
                     windows                      windows                      windows 
"Test window one (inactive)" "Test window two (inactive)" "Test window three (ACTIVE)" 



You could do that, but that approach would get cumbersome when dealing with multiple plots. I suspect Richie is more interested in how/where R stores the title information once you send it to the graphics device—as am I.

0

精彩评论

暂无评论...
验证码 换一张
取 消