Search code examples
windowsrsystemscreenresolution

How can I get the screen resolution in R


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


Solution

  • 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