Mar*_*rek 11
您可以使用commad-line接口到WMI
> system("wmic desktopmonitor get screenheight")
ScreenHeight
900
Run Code Online (Sandbox Code Playgroud)
你可以捕获结果 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"
Run Code Online (Sandbox Code Playgroud)
使用多个屏幕,输出例如是
[1] "ScreenWidth \r" "1600 \r" "1600 \r" ""
Run Code Online (Sandbox Code Playgroud)
我们想要除了第一个和最后一个值之外的所有值,转换为数字
as.numeric(c(
scr_width[-c(1, length(scr_width))],
scr_height[-c(1, length(scr_height))]
))
# [1] 1440 900
Run Code Online (Sandbox Code Playgroud)
接受的答案不适用于 Windows 8 及更高版本。
用这个:
system("wmic path Win32_VideoController get VideoModeDescription,CurrentVerticalResolution,CurrentHorizontalResolution /format:value")
要获得矢量中的屏幕分辨率,您可以将其实现为:
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)]
Run Code Online (Sandbox Code Playgroud)
现在你将拥有一个长度为 2 的向量:
> current_resolution
[1] 1920 1080
Run Code Online (Sandbox Code Playgroud)
使用JavaScript很容易:您只需
window.screen.height
window.screen.width
Run Code Online (Sandbox Code Playgroud)
您可以使用OmegaHat的SpiderMonkey包从R调用JavaScript 。
您也可以使用Java解决此问题,并使用rJava包对其进行访问。
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")
Run Code Online (Sandbox Code Playgroud)