zl程序教程

您现在的位置是:首页 >  其他

当前栏目

73-R工具指南20-优雅地在R中設置代理

2023-04-18 15:35:18 时间

前言

某些时候受限于网络限制,我们不得不使用ssr 等。

但是,电脑终端和R 却无法像电脑端一样,可以直接使用小猫clash 来管理:

比如R,每次都需要复制终端代理命令:

export https_proxy=http://127.0.0.1:7890;export http_proxy=http://127.0.0.1:7890;export all_proxy=socks5://127.0.0.1:7891

因此就引出今天的R包:r.proxy

使用

使用非常简单:

install.packages("r.proxy")

# install.packages("devtools")
devtools::install_github("xiayh17/r.proxy")

这个包相当于帮我们把上述的ip 赋值给了变量,并且可以输出一些关于网络代理的其他信息。

一般的代理IP 都是127.0.0.1:7890,所以不用管直接默认就好啦:

r.proxy::proxy()

#> This maybe the first time proxy4you be load. 
#> Please complete below configrations:)
#> Default setting can be accessed by Enter with nothing
#> [socks5 proxy] {Default as 127.0.0.1:7890} (address:port): 192.168.3.15:7890
#> [https proxy] {Default as 127.0.0.1:7890} (address:port): 
#> [http proxy] {Default as 127.0.0.1:7890} (address:port): 
#> Your setting have been save in ~/.Rproxy
#> Proxy info: 
#> http://127.0.0.1:7890http://192.168.3.15:7890socks5://127.0.0.1:7890
#> check what your ip is: 
#> IPv4: 
#> 91.243.81.71
#> 
#> Details: 
#> {
#>     "organization": "MoreneHost",
#>     "longitude": 6.1661,
#>     "timezone": "Europe/Luxembourg",
#>     "isp": "MoreneHost",
#>     "offset": 3600,
#>     "asn": 199524,
#>     "asn_organization": "G-Core Labs S.A.",
#>     "country": "Luxembourg",
#>     "ip": "91.243.81.71",
#>     "latitude": 49.7498,
#>     "continent_code": "EU",
#>     "country_code": "LU"
#> }

如果不用了也可以切回来:

> r.proxy::noproxy()
Proxy was cleaned!

check what your ip is: 
IPv4: 
122.97.183.47

Details: 
{
    "organization": "China Unicom",
    "longitude": 113.722,
    "timezone": "Asia/Shanghai",
    "isp": "China Unicom",
    "offset": 28800,
    "asn": 4837,
    "asn_organization": "CHINA UNICOM China169 Backbone",
    "country": "China",
    "ip": "xxx",
    "latitude": 34.7732,
    "continent_code": "AS",
    "country_code": "CN"
}

就是这么简单~

代理详细信息是哪里来的

#> Details: 
#> {
#>     "organization": "MoreneHost",
#>     "longitude": 6.1661,
#>     "timezone": "Europe/Luxembourg",
#>     "isp": "MoreneHost",
#>     "offset": 3600,
#>     "asn": 199524,
#>     "asn_organization": "G-Core Labs S.A.",
#>     "country": "Luxembourg",
#>     "ip": "91.243.81.71",
#>     "latitude": 49.7498,
#>     "continent_code": "EU",
#>     "country_code": "LU"
#> }

有点好奇这个字典中的数据是哪里来的:

check_ip <- function() {

message("check what your ip is: ")

message("IPv4: ")

ipv4 <- curl_fetch_memory("https://api-ipv4.ip.sb/ip")

message(rawToChar(ipv4$content))

# message("IPv6: ")

message("Details: ")

det <- curl_fetch_memory("https://api.ip.sb/geoip")

message(jsonlite::prettify(rawToChar(det$content)))

}

其实就是https://api-ipv4.ip.sb/ip 这个网站提供了接口。

其他方法

有的小朋友可能会问。我一般设置代理其实也就是为了方便从github 上安装包呀。需要花钱配置一个代理吗?

比如你可以转到gitee 上再下载:可以直接将github上的项目先fork到自己的仓库,再复制到码云中。码云:https://gitee.com/ 实测速度很酥服。

#安装官方包‘remotes’  
install.packages("remotes")  
#安装官方包‘git2r’  
install.packages("git2r")  
remotes::install_git("https://gitee.com/mugpeng/pengToolkit.git")

今天,你学废了吗?

参考资料

[1]xiayh17/r.proxy: Set Proxy in R Console (github.com): https://github.com/xiayh17/r.proxy