15

How can I get screen resolution (height, width) in pixels?

gd047
  • 29,749
  • 18
  • 107
  • 146
  • 1
    The more useful question is why; is stuff like tk or other GUI involved? – mbq Sep 05 '11 at 09:54
  • @mbq gvisMotionChart needs a width and height of the chart that it produces. So I thought i could change those parameters according to the screen resolution of the user. – gd047 Sep 05 '11 at 12:11

4 Answers4

12

You could use commad-line interface to WMI

> system("wmic desktopmonitor get screenheight")
ScreenHeight  
900   

You could capture result with system

(scr_width <- system("wmic desktopmonitor get screenwidth", intern=TRUE))
# [1] "ScreenWidth  \r" "1440         \r" "\r"
(scr_height <- system("wmic desktopmonitor get screenheight", intern=TRUE))
# [1] "ScreenHeight  \r" "900           \r" "\r" 

With multiple screens, the output is, e.g.,

[1] "ScreenWidth  \r" "1600         \r" "1600         \r" ""

We want all but the first and last values, converted to numbers

as.numeric(c(
  scr_width[-c(1, length(scr_width))], 
  scr_height[-c(1, length(scr_height))]
))
# [1] 1440  900
Richie Cotton
  • 118,240
  • 47
  • 247
  • 360
Marek
  • 49,472
  • 15
  • 99
  • 121
  • 1
    Error in system("wmic desktopmonitor get screenwidth", intern = TRUE) : 'wmic' not found. Do I have to install something? – gd047 Sep 06 '11 at 14:37
  • @gd047 wmic is available on Windows Professional Edition (XP/7) for sure. I know it's missing on XP Home, but I don't know it could be installed. – Marek Sep 07 '11 at 15:08
6

It's easy with JavaScript: you just do

window.screen.height
window.screen.width

You can call JavaScript from R using the SpiderMonkey package from OmegaHat.


You could also solve this with Java, and use the rJava package to access it.

library(rJava)
.jinit()
toolkit <- J("java.awt.Toolkit")
default_toolkit <- .jrcall(toolkit, "getDefaultToolkit")
dim <- .jrcall(default_toolkit, "getScreenSize")
height <- .jcall(dim, "D", "getHeight")
width <- .jcall(dim, "D", "getWidth")
Community
  • 1
  • 1
Richie Cotton
  • 118,240
  • 47
  • 247
  • 360
  • Haven't actually tested this myself as I'm struggling to get the package to build. Will report back once I manage this. – Richie Cotton Sep 05 '11 at 12:32
  • 2
    What's with the downvote? I know the ideas are only half complete, but they are valid possibilities nonetheless. – Richie Cotton Sep 05 '11 at 14:12
  • 2
    It seems to me that this should be achievable without need for a JVM! Personally I'd just wrap up some Win32 API calls but surely somewhere there is a package that allows you to query hardware information. – David Heffernan Sep 05 '11 at 19:26
  • @David: It depends on the usage. For a Windows only solution, Marek's answer is best. If it needs to be portable, then using Java is preferable. – Richie Cotton Sep 06 '11 at 12:52
  • 1
    best would be if a portable solution avoiding the heavy weight JVM. – David Heffernan Sep 06 '11 at 12:56
  • this solution just gives me an error - says it runs out of memory and then terminates R – Toni H Sep 09 '20 at 18:07
5

Accepted answer does not work on Windows 8 and later.

Use this:

system("wmic path Win32_VideoController get VideoModeDescription,CurrentVerticalResolution,CurrentHorizontalResolution /format:value")

To get you screen resolution in a vector, you can implement it as:

  suppressWarnings(
    current_resolution <- system("wmic path Win32_VideoController get CurrentHorizontalResolution,CurrentVerticalResolution /format:value", intern = TRUE)  %>%
      strsplit("=") %>%
      unlist() %>%
      as.double()
  )
  current_resolution <- current_resolution[!is.na(current_resolution)]

Now you'll have a vector with length 2:

> current_resolution
[1] 1920 1080
MS Berends
  • 4,489
  • 1
  • 40
  • 53
1

On Windows you can call GetSystemMetrics passing SM_CXSCREEN and SM_CYSCREEN. This returns the width/height of the screen of the primary display monitor, in pixels.

DWORD dwWidth = GetSystemMetrics(SM_CXSCREEN);
DWORD dwHeight = GetSystemMetrics(SM_CYSCREEN);
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
Arash
  • 383
  • 4
  • 16
  • 1
    I believe that the poster is looking to do this from the R language rather than C++. – David Heffernan Sep 05 '11 at 09:18
  • 1
    @David Hefferman yes you right, sorry, anyway I found this [link](http://www.nceas.ucsb.edu/scicomp/usecases/CreateRPackageWithC) maybe help – Arash Sep 05 '11 at 09:45