有人知道一个能够检索自己的IP地址(正在使用的PC)的R功能吗?这将是非常有益的!提前谢谢了. 您可以向操作系统发出一个system()命令: 在Windows中可以使用ipconfig 在Linux中,使用ifconfig
>在Windows中可以使用ipconfig
>在Linux中,使用ifconfig
例如,在Windows上,尝试使用参数intern = TRUE调用system()将结果返回给R:
x <- system("ipconfig", intern=TRUE)
这返回:
x [1] "" [2] "Windows IP Configuration" [3] "" [4] "" [5] "Wireless LAN adapter Wireless Network Connection:" [6] "" [7] " Connection-specific DNS Suffix . : tbglondon.local" [8] " Link-local IPv6 Address . . . . . : fe80::c0cb:e470:91c7:abb9%14" [9] " IPv4 Address. . . . . . . . . . . : 10.201.120.184" [10] " Subnet Mask . . . . . . . . . . . : 255.255.255.0" [11] " Default Gateway . . . . . . . . . : 10.201.120.253" [12] "" [13] "Ethernet adapter Local Area Connection:" [14] "" [15] " Connection-specific DNS Suffix . : tbglondon.local" [16] " Link-local IPv6 Address . . . . . : fe80::9d9b:c44c:fd4d:1c77%11" [17] " IPv4 Address. . . . . . . . . . . : 10.201.120.157" [18] " Subnet Mask . . . . . . . . . . . : 255.255.255.0" [19] " Default Gateway . . . . . . . . . : 10.201.120.253" [20] "" [21] "Tunnel adapter Local Area Connection* 13:" [22] "" [23] " Media State . . . . . . . . . . . : Media disconnected" [24] " Connection-specific DNS Suffix . : " [25] "" [26] "Tunnel adapter isatap.tbglondon.local:" [27] "" [28] " Media State . . . . . . . . . . . : Media disconnected" [29] " Connection-specific DNS Suffix . : tbglondon.local" [30] "" [31] "Tunnel adapter Teredo Tunneling Pseudo-Interface:" [32] "" [33] " Media State . . . . . . . . . . . : Media disconnected" [34] " Connection-specific DNS Suffix . : "
现在您可以使用grep找到IPv4的行:
x[grep("IPv4", x)] [1] " IPv4 Address. . . . . . . . . . . : 10.201.120.184" [2] " IPv4 Address. . . . . . . . . . . : 10.201.120.157"
并提取只是ip地址:
z <- x[grep("IPv4", x)] gsub(".*? ([[:digit:]])", "\\1", z) "10.201.120.184" "10.201.120.157"