一崇猫、編譯安裝R-4.2.1
1诅福、升級gcc
??R語言及很多它的包匾委,要從源碼編譯安裝,所以要先升級gcc到最新版氓润,這是升級好的赂乐,最新的是11版。
root@VM-0-14-ubuntu:~# apt list --installed | grep gcc
WARNING: apt does not have a stable CLI interface. Use with caution in scripts.
gcc/bionic-security,bionic-updates,now 4:7.4.0-1ubuntu2.3 amd64 [已安裝]
gcc-11/bionic,now 11.1.0-1ubuntu1~18.04.1 amd64 [已安裝]
gcc-11-base/bionic,now 11.1.0-1ubuntu1~18.04.1 amd64 [已安裝咖气,自動]
gcc-7/bionic-security,bionic-updates,now 7.5.0-3ubuntu1~18.04 amd64 [已安裝挨措,自動]
gcc-7-base/bionic-security,bionic-updates,now 7.5.0-3ubuntu1~18.04 amd64 [已安裝,自動]
gcc-7-multilib/bionic-security,bionic-updates,now 7.5.0-3ubuntu1~18.04 amd64 [已安裝崩溪,自動]
gcc-8-base/bionic-security,bionic-updates,now 8.4.0-1ubuntu1~18.04 amd64 [已安裝]
gcc-multilib/bionic-security,bionic-updates,now 4:7.4.0-1ubuntu2.3 amd64 [已安裝]
lib32gcc-7-dev/bionic-security,bionic-updates,now 7.5.0-3ubuntu1~18.04 amd64 [已安裝浅役,自動]
lib32gcc-s1/bionic,now 11.1.0-1ubuntu1~18.04.1 amd64 [已安裝,自動]
lib32gcc1/bionic,now 1:11.1.0-1ubuntu1~18.04.1 amd64 [已安裝伶唯,自動]
libgcc-11-dev/bionic,now 11.1.0-1ubuntu1~18.04.1 amd64 [已安裝觉既,自動]
libgcc-7-dev/bionic-security,bionic-updates,now 7.5.0-3ubuntu1~18.04 amd64 [已安裝,自動]
libgcc-s1/bionic,now 11.1.0-1ubuntu1~18.04.1 amd64 [已安裝抵怎,自動]
libgcc1/bionic-security,bionic-updates,now 1:8.4.0-1ubuntu1~18.04 amd64 [已安裝奋救,可升級至:1:11.1.0-1ubuntu1~18.04.1]
libx32gcc-7-dev/bionic-security,bionic-updates,now 7.5.0-3ubuntu1~18.04 amd64 [已安裝榄审,自動]
libx32gcc-s1/bionic,now 11.1.0-1ubuntu1~18.04.1 amd64 [已安裝航厚,自動]
libx32gcc1/bionic,now 1:11.1.0-1ubuntu1~18.04.1 amd64 [已安裝,自動]
root@VM-0-14-ubuntu:~#
??參閱資料炕吸,依次執(zhí)行下面的命令升級姿染。
root@VM-0-14-ubuntu:~# add-apt-repository ppa:ubuntu-toolchain-r/test
root@VM-0-14-ubuntu:~# apt update
root@VM-0-14-ubuntu:~# apt install gcc-11 g++-11
root@VM-0-14-ubuntu:~# updatedb
root@VM-0-14-ubuntu:~# ldconfig
root@VM-0-14-ubuntu:~# locate gcc | grep -E "/usr/bin/gcc-"
root@VM-0-14-ubuntu:~# update-alternatives --remove-all gcc
root@VM-0-14-ubuntu:~# update-alternatives --remove-all g++
root@VM-0-14-ubuntu:~# update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-7 1
root@VM-0-14-ubuntu:~# update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-11 10
root@VM-0-14-ubuntu:~# update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-7 1
root@VM-0-14-ubuntu:~# update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-11 10
??裝好驗證:
root@VM-0-14-ubuntu:~# gcc --version
gcc (Ubuntu 11.1.0-1ubuntu1~18.04.1) 11.1.0
Copyright (C) 2021 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
root@VM-0-14-ubuntu:~# g++ --version
g++ (Ubuntu 11.1.0-1ubuntu1~18.04.1) 11.1.0
Copyright (C) 2021 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
2背亥、安裝虛擬屏幕以在編譯時支持X11圖形界面
??詳細(xì)介紹見我的另一篇文章《在Rstudio Linux Server上使用X window OpenGL圖形系統(tǒng)》,以及該參考資料1悬赏,參考資料2狡汉。
# apt install xvfb x11vnc
# Xvfb :2 -screen 0 1280x960x24 2>/dev/null &
# export DISPLAY=:2
??配置開機(jī)自動啟動這兩個服務(wù),該鏡像已經(jīng)激活了ubuntu18的rc-local.service闽颇。
root@VM-0-14-ubuntu:~# vi /etc/rc.local
??加入啟動Xvfb的命令盾戴。
# Startup Xvfb and assign DISPLAY :2 to virtual X11 Server.
/usr/bin/Xvfb :2 -screen 0 1280x960x24 2>/dev/null &
export DISPLAY=:2
??參考這篇資料,為x11vnc配置一個開機(jī)啟動服務(wù):
root@VM-0-14-ubuntu:/etc# cd /lib/systemd/system
root@VM-0-14-ubuntu:/lib/systemd/system# vi x11vnc.service
[Unit]
Description=Start x11vnc at startup.
After=multi-user.target
[Service]
Type=simple
ExecStart=/usr/bin/x11vnc -listen 0.0.0.0 -rfbport 5900 -noipv6 -passwd password -display :2 -forever
[Install]
WantedBy=multi-user.target
??激活服務(wù)并重啟系統(tǒng):
root@VM-0-14-ubuntu:/lib/systemd/system# systemctl enable x11vnc.service
root@VM-0-14-ubuntu:/lib/systemd/system# systemctl daemon-reload
root@VM-0-14-ubuntu:/lib/systemd/system# reboot now
root@VM-0-14-ubuntu:~# ps -ef|grep Xvfb
root 2150 1 0 07:55 ? 00:00:00 /usr/bin/Xvfb :2 -screen 0 1280x960x24
root 9678 7329 0 08:38 pts/0 00:00:00 grep --color=auto Xvfb
root@VM-0-14-ubuntu:~# ps -ef|grep x11vnc
root 2171 1 0 07:55 ? 00:00:00 /usr/bin/x11vnc -listen 0.0.0.0 -rfbport 5900 -noipv6 -display :2 -forever
root 9724 7329 0 08:39 pts/0 00:00:00 grep --color=auto x11vnc
3兵多、安裝編譯R的依賴包
??參閱資料1尖啡,參閱資料2,參閱資料3剩膘。
# apt-get install libpng-dev
# apt-get install libjpeg-dev
# apt-get install libtiff-dev
# apt-get install libcairo-dev
# sudo apt-get install libicu-dev
# apt-get install liblzma-dev libblas
# apt-get install libbz2-dev
# apt-get install libreadline-dev
# apt install libssl-dev libcurl4-openssl-dev libxml2-dev
4衅斩、編譯安裝R-4.2.1
??從源碼編譯R-4.2.1需要libicu58,該軟件包用于軟件包的國際化支持怠褐。裝好Python部分后畏梆,系統(tǒng)中裝了好幾個版本的libicu:libicu60.2(ubuntu18.04),libicu58(Anaconda3),libicu56(CUDA 11)奠涌。參考下面的資料宪巨,從源碼重新編譯安裝libicu58,把ubuntu18中l(wèi)ibicu的版本降下來铣猩。參考資料1揖铜,參考資料2,參考資料3达皿,參考資料4天吓。
root@VM-0-14-ubuntu:~# apt list --installed | grep libicu
WARNING: apt does not have a stable CLI interface. Use with caution in scripts.
libicu-dev/bionic-security,bionic-updates,now 60.2-3ubuntu3.2 amd64 [已安裝,自動]
libicu-le-hb-dev/bionic,now 1.0.3+git161113-4 amd64 [已安裝峦椰,自動]
libicu-le-hb0/bionic,now 1.0.3+git161113-4 amd64 [已安裝龄寞,自動]
libicu60/bionic-security,bionic-updates,now 60.2-3ubuntu3.2 amd64 [已安裝]
libiculx60/bionic-security,bionic-updates,now 60.2-3ubuntu3.2 amd64 [已安裝,自動]
root@VM-0-14-ubuntu:~# find / -name libicu*
/usr/lib/x86_64-linux-gnu/libicui18n.so.60
......
/usr/local/cuda-11.2/nsight-compute-2020.3.1/host/linux-desktop-glibc_2_11_3-x64/libicui18n.so.56
/usr/local/cuda-11.2/nsight-compute-2020.3.1/host/linux-desktop-glibc_2_11_3-x64/libicuuc.so.56
/usr/local/cuda-11.2/nsight-compute-2020.3.1/host/linux-desktop-glibc_2_11_3-x64/libicudata.so.56
/usr/local/cuda-11.2/nsight-systems-2020.4.3/host-linux-x64/libicui18n.so.56
/usr/local/cuda-11.2/nsight-systems-2020.4.3/host-linux-x64/libicuuc.so.56
/usr/local/cuda-11.2/nsight-systems-2020.4.3/host-linux-x64/libicudata.so.56
/usr/local/anaconda3/pkgs/icu-58.2-he6710b0_3/lib/libicuio.so.58
......
/usr/local/anaconda3/lib/libicuio.so.58
......
/usr/local/anaconda3/envs/gpu/lib/libicuio.so.58
......
/usr/share/lintian/overrides/libicu60
/usr/share/doc/libicu-le-hb-dev
/usr/share/doc/libicu-le-hb0
/usr/share/doc/libiculx60
/usr/share/doc/libicu-dev
/usr/share/doc/libicu60
......
??用系統(tǒng)默認(rèn)的libicu60.2編譯R時會報錯:
gcc -I../../src/extra -I. -I../../src/include -I../../src/include -I/usr/local/anaconda3/include -I/usr/local/include -I../../src/nmath -DHAVE_CONFIG_H -fopenmp -fpic -g -O2 -c Rmain.c -o Rmain.o
gcc -Wl,--export-dynamic -fopenmp -L"../../lib" -L/usr/local/lib -o R.bin Rmain.o -lR -lRblas
../../lib/libR.so:對‘u_versionToString_58’未定義的引用
../../lib/libR.so:對‘u_getVersion_58’未定義的引用
../../lib/libR.so:對‘uloc_setDefault_58’未定義的引用
../../lib/libR.so:對‘ucol_setStrength_58’未定義的引用
../../lib/libR.so:對‘ucol_open_58’未定義的引用
../../lib/libR.so:對‘ucol_close_58’未定義的引用
../../lib/libR.so:對‘ucol_strcollIter_58’未定義的引用
../../lib/libR.so:對‘ucol_setAttribute_58’未定義的引用
../../lib/libR.so:對‘uiter_setUTF8_58’未定義的引用
../../lib/libR.so:對‘ucol_getLocaleByType_58’未定義的引用
??先重新編譯安裝libicu58:
# cd /home/ubuntu
# wget https://github.com/unicode-org/icu/releases/download/release-58-3/icu4c-58_3-src.tgz
# tar -xzvf icu4c-58_3-src.tgz
# cd icu/source
# ./configure
# # 編譯需要xlocale.h
# ln -s /usr/include/locale.h /usr/include/xlocale.h
# make
# make install
??編譯安裝R-4.2.1:
# cd /home/ubuntu
# wget https://mirrors.tuna.tsinghua.edu.cn/CRAN/src/base/R-4/R-4.2.1.tar.gz
# tar -zxvf R-4.2.1.tar.gz
# cd R-4.2.1
# ./configure --prefix=/usr/lib64/R-4.2.1 --enable-R-shlib --with-pcre1 --with-cairo --with-libpng
# make -j4
# make install
# cd /usr/lib64/R-4.2.1/bin
# DISPLAY=:2 ./R
??把R加入PATH搜索:
# vi /etc/environment
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/usr/lib64/R-4.2.1/bin"
??source 或logout/login使環(huán)境變量生效汤功。
root@VM-0-14-ubuntu:/home/ubuntu/R-4.2.1# R
R version 4.2.1 (2022-06-23) -- "Funny-Looking Kid"
Copyright (C) 2022 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)
R是自由軟件物邑,不帶任何擔(dān)保。
在某些條件下你可以將其自由散布滔金。
用'license()'或'licence()'來看散布的詳細(xì)條件色解。
R是個合作計劃,有許多人為之做出了貢獻(xiàn).
用'contributors()'來看合作者的詳細(xì)情況
用'citation()'會告訴你如何在出版物中正確地引用R或R程序包餐茵。
用'demo()'來看一些示范程序科阎,用'help()'來閱讀在線幫助文件,或
用'help.start()'通過HTML瀏覽器來看幫助文件忿族。
用'q()'退出R.
> capabilities()
jpeg png tiff tcltk X11 aqua
TRUE TRUE TRUE FALSE TRUE FALSE
http/ftp sockets libxml fifo cledit iconv
TRUE TRUE FALSE TRUE TRUE TRUE
NLS Rprof profmem cairo ICU long.double
TRUE TRUE FALSE TRUE FALSE TRUE
libcurl
TRUE
>
5锣笨、指定R軟件包C++14、C++17的編譯器道批。
??后面從源碼安裝的一些R軟件包指定了C++要14或17版以上错英,需要為R指定它們的編譯器。
# cd /root
# mkdir ~/.R
# vi ~/.R/Makevars
CXX14FLAGS=-O3 -march=native -mtune=native -fPIC
CXX14=g++
CXX17FLAGS=-O3 -march=native -mtune=native -fPIC
CXX17=g++
6隆豹、為R進(jìn)程指定X window屏幕椭岩。
root@VM-0-14-ubuntu:/usr/lib64/R-4.2.1/lib/R/etc# vi Renviron
增加下面一行:
# Added by Jean for Xvfb 2022/11/10
DISPLAY=:2
二、安裝Rstudio server
1璃赡、確保軟件的版本符合要求:
RStudio Server requires Debian version 10 (or higher) or Ubuntu version 18 (or higher). RStudio requires a previous installation of R version 3.3.0 or higher.
下載:
# cd /home/ubuntu
# apt-get install gdebi-core
# wget https://download2.rstudio.org/server/bionic/amd64/rstudio-server-2022.07.2-576-amd64.deb
2簿煌、執(zhí)行安裝,會下載安裝依賴的幾個軟件包鉴吹,然后安裝程序要啟動Rstudio Server可能會出錯,因為還沒有在配置文件中指定要用的R惩琉。
root@VM-0-14-ubuntu:/home/ubuntu# gdebi rstudio-server-2022.07.2-576-amd64.deb
Reading package lists... Done
Building dependency tree
Reading state information... Done
Reading state information... Done
需要安裝以下軟件包: libclang-6.0-dev libclang-common-6.0-dev libclang-dev libclang1-6.0 libllvm6.0 libpq5
RStudio Server
RStudio is a set of integrated tools designed to help you be more productive with R. It includes a console, syntax-highlighting editor that supports direct code execution, as well as tools for plotting, history, and workspace management.
您是否想安裝這個軟件包豆励?[y/N]:y
......
正在選中未選擇的軟件包 rstudio-server。
(正在讀取數(shù)據(jù)庫 ... 系統(tǒng)當(dāng)前共安裝有 134749 個文件和目錄。)
正準(zhǔn)備解包 rstudio-server-2022.07.2-576-amd64.deb ...
正在解包 rstudio-server (2022.07.2+576) ...
正在設(shè)置 rstudio-server (2022.07.2+576) ...
Created symlink /etc/systemd/system/multi-user.target.wants/rstudio-server.service → /lib/systemd/system/rstudio-server.service.
TTY detected. Printing informational message about logging configuration. Logging configuration loaded from '/etc/rstudio/logging.conf'. Logging to '/var/log/rstudio/rstudio-server/rserver.log'.
● rstudio-server.service - RStudio Server
Loaded: loaded (/lib/systemd/system/rstudio-server.service; enabled; vendor preset: enabled)
Active: activating (auto-restart) (Result: exit-code) since Wed 2022-11-09 16:01:25 CST; 34ms ago
Process: 6716 ExecStart=/usr/lib/rstudio-server/bin/rserver (code=exited, status=0/SUCCESS)
Main PID: 6725 (code=exited, status=1/FAILURE)
root@VM-0-14-ubuntu:/home/ubuntu#
3良蒸、配置Rstudio Server技扼。
A、增加用戶組rusers嫩痰,加入用戶ubuntu剿吻,后面將配置只有rusers用戶組才能登錄。Rstudio Server默認(rèn)uid 1000以下的用戶(系統(tǒng)用戶)不能登錄串纺,要查詢用戶ubuntu的id丽旅,后面修改一下uid的限制:
root@VM-0-14-ubuntu:/home/ubuntu# groupadd rusers
root@VM-0-14-ubuntu:/home/ubuntu# usermod -a -G rusers ubuntu
root@VM-0-14-ubuntu:/home/ubuntu# grep rusers /etc/group
rusers:x:1001:ubuntu
root@VM-0-14-ubuntu:/home/ubuntu# id ubuntu
uid=500(ubuntu) gid=500(ubuntu) 組=500(ubuntu),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),113(lpadmin),114(sambashare),1001(rusers)
B、服務(wù)器配置纺棺。
root@VM-0-14-ubuntu:/home/ubuntu# vi /etc/rstudio/rserver.conf
# Server Configuration File
www-port=8787
rsession-which-r=/usr/lib64/R-4.2.1/bin/R
auth-required-user-group=rusers
auth-minimum-user-id=500
C榄笙、session配置。
root@VM-0-14-ubuntu:/home/ubuntu# vi /etc/rstudio/rsession.conf
# R Session Configuration File
session-timeout-minutes=0
D祷蝌、為作圖配置Rstudio(Browser Client)全局選項茅撞,否則作圖會出錯,因為服務(wù)器上沒有物理的X11圖形設(shè)備巨朦。這里是正常的作圖米丘,顯示在Rstudio Plots窗口中,不用Xvfb虛擬屏幕糊啡。
Tools menu is the Global Options... command that lets you set the graphics backend (under the General section, Graphics tab). Changing the graphics device backend to "Cairo".
或者配置服務(wù)器的全局選項拄查,參閱:參考資料。
root@VM-0-14-ubuntu:/home/ubuntu# vi /etc/rstudio/rstudio-prefs.json
#"enum": ["default", "cairo", "cairo-png", "quartz", "windows", "ragg"],
#options(RStudioGD.backend = "ragg")
#set it in (say) your R installation's Rprofile.site
{
graphics_backend: "cairo"
}
E悔橄、指定所有R進(jìn)程的圖形設(shè)備都用cairo靶累,否則后面運(yùn)行Shiny App時會報錯:“無法打開鏈結(jié)到X11顯示,無法打開PNG設(shè)備”癣疟,因為ubuntu上它默認(rèn)的圖形設(shè)備是Xlib挣柬,會連接到X11,參閱資料1睛挚,參閱資料2邪蛔。
root@VM-0-14-ubuntu:/usr/lib64/R-4.2.1/lib/R/etc# vi Rprofile.site
options(bitmapType='cairo')
4、管理Rstudio Server扎狱。
先讓服務(wù)器成為系統(tǒng)守護(hù)進(jìn)程侧到,開機(jī)自啟動。
#systemctl enable rstudio-server.service
管理服務(wù):
#systemctl start rstudio-server.service
#systemctl stop rstudio-server.service
#systemctl status rstudio-server
5淤击、查看日志匠抗。
root@VM-0-14-ubuntu:/home/ubuntu# vi /var/log/rstudio/rstudio-server/rserver.log
2022-11-09T08:01:24.931865Z [rserver] ERROR Path to R not specified, and no module binary specified; Invalid R module (); LOGGED FROM: int main(int, char* const*) src/cpp/server/ServerMain.cpp:812
2022-11-09T08:19:30.050217Z [rserver] WARNING User ubuntu could not be authenticated because they did not meet the minimum required user id (1000). The minimum user id is controlled by the auth-minimum-user-id rserver.conf option.; LOGGED FROM: bool rstudio::server::auth::validateUser(const string&, const string&, unsigned int, bool) src/cpp/server/auth/ServerValidateUser.cpp:94
2022-11-09T08:42:58.588563Z [rserver] ERROR Failed to validate sign-in with invalid CSRF form; LOGGED FROM: bool rstudio::server::auth::common::validateSignIn(const rstudio::core::http::Request&, rstudio::core::http::Response*) src/cpp/server/auth/ServerAuthCommon.cpp:133
6、登錄系統(tǒng)測試污抬。
訪問網(wǎng)址http://106.52.33.185:8787汞贸,輸入用戶名ubuntu/password登錄绳军,運(yùn)行一段簡單的作圖程序測試。現(xiàn)在是沒有https加密的矢腻,先用著门驾,后面再與Shiny Server一起配Nginx反向代理https加密。
三多柑、安裝Shiny等常用軟件包奶是。
1、安裝shiny及rmarkdown包竣灌,用于Rstudio IDE中開發(fā)測試聂沙。
>install.packages("shiny")
>install.packages("rmarkdown")
會從源碼下載安裝一大堆依賴的包。
運(yùn)行一個簡單的Shiny App測試帐偎,~/Rscripts/ShinyDev/shinyAppExample/app.R逐纬。
library(shiny)
# Define UI for app that draws a histogram ----
ui <- fluidPage(
# App title ----
titlePanel("Hello Shiny!"),
# Sidebar layout with input and output definitions ----
sidebarLayout(
# Sidebar panel for inputs ----
sidebarPanel(
# Input: Slider for the number of bins ----
sliderInput(inputId = "bins",
label = "Number of bins:",
min = 1,
max = 50,
value = 30)
),
# Main panel for displaying outputs ----
mainPanel(
# Output: Histogram ----
plotOutput(outputId = "distPlot")
)
)
)
# Define server logic required to draw a histogram ----
server <- function(input, output) {
# Histogram of the Old Faithful Geyser Data ----
# with requested number of bins
# This expression that generates a histogram is wrapped in a call
# to renderPlot to indicate that:
#
# 1. It is "reactive" and therefore should be automatically
# re-executed when inputs (input$bins) change
# 2. Its output type is a plot
output$distPlot <- renderPlot({
x <- faithful$waiting
bins <- seq(min(x), max(x), length.out = input$bins + 1)
hist(x, breaks = bins, col = "#75AADB", border = "white",
xlab = "Waiting time to next eruption (in mins)",
main = "Histogram of waiting times")
})
}
# Create Shiny app ----
shinyApp(ui = ui, server = server)
2、安裝reticulate削樊、tensorflow豁生、keras連接Python深度學(xué)習(xí)環(huán)境。
深度學(xué)習(xí)框架Tensorflow GPU只有Python版漫贞,Rstudio通過reticulate包連接Python調(diào)用它們甸箱,R語言的tensorflow與keras包提供了對Python版相應(yīng)包的封裝,參閱資料1迅脐,參閱資料2芍殖。
A、安裝軟件包谴蔑。用root在終端上啟動R豌骏,安裝到R的系統(tǒng)library目錄下。
>install.packages("reticulate")
>install.packages("tensorflow")
>install.packages("keras")
>install.packages("dplyr")
上面的命令會自動安裝它們依賴的其它R包隐锭。
B窃躲、設(shè)置RETICULATE_PYTHON環(huán)境變量,所有R進(jìn)程都使用Anaconda3的Python钦睡,注意用的是虛擬環(huán)境"gpu"的python蒂窒。
root@VM-0-14-ubuntu:~# conda activate gpu
(gpu) root@VM-0-14-ubuntu:~# cd /usr/lib64/R-4.2.1/lib/R/etc
(gpu) root@VM-0-14-ubuntu:/usr/lib64/R-4.2.1/lib/R/etc# vi Renviron
增加下面的環(huán)境變量。
# Added for python by Jean 2022/02/27
RETICULATE_PYTHON='/usr/local/anaconda3/envs/gpu/bin/python'
重啟當(dāng)前的Rstudio Session以使環(huán)境變量生效荞怒,運(yùn)行一個matplotlib程序畫個簡單的圖測試一下洒琢。
import numpy as np
import matplotlib.pyplot as plt
Xc = np.array([-1, 2, 3, 4])
Yc = np.array([-2, 3, 4, 5])
plt.plot(Xc,Yc)
plt.title("中文測試")
plt.show()
C、測試keras和tensorflow-gpu褐桌,testKeras.R衰抑。
library(reticulate)
library(keras)
library(dplyr)
py_discover_config()
py_config()
mnist <- dataset_mnist()
# keras的數(shù)據(jù)文件默認(rèn)放在/home/ubuntu/.keras/datasets/mnist.npz
str(mnist)
train_images <- mnist$train$x
train_label <- mnist$train$y
test_images <- mnist$test$x
test_label <- mnist$test$y
dim(train_images)
dim(test_images)
dim(train_label)
dim(test_label)
str(train_images)
glimpse(train_images)
str(test_images)
# 設(shè)計神經(jīng)網(wǎng)絡(luò)結(jié)構(gòu),由2個完全連接層處理,一個有512個神經(jīng)元荧嵌,一個有10個神經(jīng)元停士, 28*28=784 -> 512 ->10
# 用線性堆棧層構(gòu)建模型
network <- keras_model_sequential() %>%
layer_dense(units = 512, activation = "relu", input_shape = c(28 * 28)) %>%
layer_dense(units = 10, activation = "softmax") # 第二層沒有輸入格式參數(shù)挖帘,接受第一層的輸出,512維向量恋技。
# 設(shè)計算法的損失函數(shù),優(yōu)化算法和度量準(zhǔn)則
network %>% compile(
optimizer = "rmsprop", # 優(yōu)化器逻族,選擇的梯度下降算法
loss = "categorical_crossentropy", # 損失函數(shù)
metrics = c("accuracy") # 觀察指標(biāo)
)
# 數(shù)據(jù)預(yù)處理蜻底, 每張圖由二維數(shù)組變?yōu)?維數(shù)組,輸入是1維數(shù)組(1維張量)聘鳞,標(biāo)準(zhǔn)化至[0,1]薄辅,完全連接層要求二維張量
train_images <- array_reshape(train_images, c(60000, 28 * 28))
train_images <- train_images / 255
test_images <- array_reshape(test_images, c(10000, 28 * 28))
test_images <- test_images / 255
# 目標(biāo)變量因子化,變?yōu)轭愖兞?train_labels <- to_categorical(train_label)
test_labels <- to_categorical(test_label)
dim(train_images)
dim(test_images)
dim(train_labels)
dim(test_labels)
# 訓(xùn)練數(shù)據(jù)集上擬合模型
network %>% fit(train_images, train_labels, epochs = 5, batch_size = 128)
# 60000/128 =469抠璃,GPU版并行處理站楚,每批128個樣本,共469批搏嗡,書中 60000/60000是CPU版串行訓(xùn)練窿春,每次迭代要9~10s
# 測試數(shù)據(jù)集測試模型
metrics <- network %>% evaluate(test_images, test_labels, batch_size = 128)
metrics
# 10000/313=32,GPU并行處理同上采盒。evaluate()如果沒有給出 batch_size參數(shù)旧乞,默認(rèn)是32。
# 測試樣本的前10個樣本的類別預(yù)測
# 參閱 https://blog.csdn.net/yiyihuazi/article/details/122323349
# keras 2.6刪除了predict_classes()函數(shù)
#network %>% predict_classes(test_images[1:10, ])
predicts<- network %>% predict(test_images[1:10, ])
result<- rep(0,10)
for(i in 1:length(predicts[,1])){
result[i]<- which.max(predicts[i,])-1
}
cat(result)
# [1] 7 2 1 0 4 1 4 9 5 9
# 畫出這10個樣本磅氨,看看識別的效果尺栖,因為test_images經(jīng)過reshape,要引用原來載入的數(shù)據(jù)烦租。
# 第9個數(shù)字識別為5延赌,有疑問。
opar <- par(no.readonly = TRUE)
par(mfrow=c(2,5),mai=c(0.1,0.1,0.3,0.1))
for(i in 1:10){
digit<- mnist$test$x[i,,]
plot(as.raster(digit, max=255))
}
par(opar)
四叉橱、安裝Shiny Server
1挫以、安裝。
Before installing Shiny Server, you must install R and the Shiny R package.
前面已經(jīng)安裝好了赏迟,直接裝Shiny Server屡贺。
(gpu) root@VM-0-14-ubuntu:/home/ubuntu# wget https://download3.rstudio.org/ubuntu-18.04/x86_64/shiny-server-1.5.19.995-amd64.deb
(gpu) root@VM-0-14-ubuntu:/home/ubuntu# gdebi shiny-server-1.5.19.995-amd64.deb
Reading package lists... Done
Building dependency tree
Reading state information... Done
Reading state information... Done
Shiny Server
Shiny Server is a server program from RStudio, Inc. that makes Shiny applications available over the web. Shiny is a web application framework for the R statistical computation language.
您是否想安裝這個軟件包?[y/N]:y
正在選中未選擇的軟件包 shiny-server锌杀。
(正在讀取數(shù)據(jù)庫 ... 系統(tǒng)當(dāng)前共安裝有 138801 個文件和目錄甩栈。)
正準(zhǔn)備解包 shiny-server-1.5.19.995-amd64.deb ...
正在解包 shiny-server (1.5.19.995) ...
正在設(shè)置 shiny-server (1.5.19.995) ...
Creating user shiny
Adding LANG to /etc/systemd/system/shiny-server.service, setting to zh_CN.UTF-8
Created symlink /etc/systemd/system/multi-user.target.wants/shiny-server.service → /etc/systemd/system/shiny-server.service.
● shiny-server.service - ShinyServer
Loaded: loaded (/etc/systemd/system/shiny-server.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2022-11-10 10:44:54 CST; 7ms ago
Main PID: 32193 (shiny-server)
Tasks: 1 (limit: 4915)
CGroup: /system.slice/shiny-server.service
└─32193 /opt/shiny-server/ext/node/bin/shiny-server /opt/shiny-server/lib/main.js
11月 10 10:44:54 VM-0-14-ubuntu systemd[1]: Started ShinyServer.
This will install Shiny Server into /opt/shiny-server/, with the main executable in /opt/shiny-server/bin/shiny-server, and also create a new shiny user.
2、指定使用的R版本糕再。
因為我的R沒有裝在默認(rèn)的位置量没,shiny server在嘗試運(yùn)行Shiny App時沒有找到R命令,需要設(shè)置Shiny Server使用的R版本突想,參閱資料1殴蹄,參閱資料2究抓,r_path參數(shù)只有在專業(yè)版中才有。于是參考該帖子建立一個軟連接解決袭灯,不用改配置刺下。
# ln -s /usr/lib64/R-4.2.1/bin/R /usr/bin/R
3、管理Shiny Server:
# systemctl start shiny-server
# systemctl stop shiny-server
# systemctl restart shiny-server
# systemctl status shiny-server
重啟Shiny Server稽荧,訪問http://106.52.33.185:3838橘茉,安裝完成。現(xiàn)在它沒有加密連接姨丈,先用著畅卓。Rstudio Server與Shiny Server的開源免費版都不支持加密連接,后面再通過Nginx反向代理加密蟋恬。
4翁潘、配置Shiny Server為用戶發(fā)布模式,允許用戶自行發(fā)布App歼争。
這樣用戶只要把APP拷貝到Ta的home目錄下的~/ShinyApps目錄下即可拜马,比如ubuntu把/home/ubuntu/Rscripts/ShinyDev/shinyAppExample拷貝成/home/ubuntu/ShinyApps/shinyAppExample就發(fā)布了APP。具體可參閱Shiny Server Administrator’s Guide矾飞。
root@VM-0-14-ubuntu:/etc/shiny-server# vi shiny-server.conf
# Define a top-level server which will listen on a port
server {
# Instruct this server to listen on port 3838
listen 3838;
# Define the location available at the base URL
location /{
# Define the user we should use when spawning R Shiny processes
run_as shiny;
# Run this location in 'site_dir' mode, which hosts the entire directory
# tree at '/srv/shiny-server'
site_dir /srv/shiny-server;
# Define where we should put the log files for this location
log_dir /var/log/shiny-server;
# Should we list the contents of a (non-Shiny-App) directory when the user
# visits the corresponding URL?
directory_index on;
}
# Should make a directory named ShinyApps at user home directory
location /users {
run_as :HOME_USER:;
user_dirs;
}
}
5一膨、在用戶home目錄下建立ShinyApps目錄,測試好的App拷貝到該目錄下洒沦,每個App一個子目錄豹绪。
1)單文件發(fā)布,在子目錄下命名為app.R申眼,DESCRIPTION文件可選瞒津。
2)雙文件發(fā)布,在子目錄下命名為ui.R與server.R括尸,global.R可選巷蚪。
(base) ubuntu@VM-0-14-ubuntu:~$ cd ShinyApps
(base) ubuntu@VM-0-14-ubuntu:~/ShinyApps$ cp -R /home/ubuntu/Rscripts/ShinyDev/shinyAppExample shinyAppExample
(base) ubuntu@VM-0-14-ubuntu:~/ShinyApps$ ls
shinyAppExample
6、通過 http://ip:port/users/UserName/AppDirectory訪問App濒翻。比如:
http://106.52.33.185:3838/users/ubuntu/shinyAppExample
五屁柏、安裝Nginx并配置反向代理
1、安裝Nginx有送,參閱資料淌喻。
(gpu) root@VM-0-14-ubuntu:/var/log/shiny-server# apt install nginx
正在讀取軟件包列表... 完成
正在分析軟件包的依賴關(guān)系樹
正在讀取狀態(tài)信息... 完成
將會同時安裝下列軟件:
libnginx-mod-http-geoip libnginx-mod-http-image-filter libnginx-mod-http-xslt-filter libnginx-mod-mail libnginx-mod-stream
nginx-common nginx-core
建議安裝:
fcgiwrap nginx-doc ssl-cert
下列【新】軟件包將被安裝:
libnginx-mod-http-geoip libnginx-mod-http-image-filter libnginx-mod-http-xslt-filter libnginx-mod-mail libnginx-mod-stream
nginx nginx-common nginx-core
升級了 0 個軟件包,新安裝了 8 個軟件包雀摘,要卸載 0 個軟件包裸删,有 26 個軟件包未被升級。
需要下載 599 kB 的歸檔阵赠。
解壓縮后會消耗 2,120 kB 的額外空間涯塔。
您希望繼續(xù)執(zhí)行嗎肌稻? [Y/n] y
......
正在處理用于 systemd (237-3ubuntu10.56) 的觸發(fā)器 ...
(gpu) root@VM-0-14-ubuntu:/var/log/shiny-server# systemctl status nginx
● nginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2022-11-10 16:23:05 CST; 30s ago
Docs: man:nginx(8)
Main PID: 21512 (nginx)
Tasks: 9 (limit: 4915)
CGroup: /system.slice/nginx.service
├─21512 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
├─21514 nginx: worker process
├─21516 nginx: worker process
├─21518 nginx: worker process
├─21520 nginx: worker process
├─21522 nginx: worker process
├─21523 nginx: worker process
├─21524 nginx: worker process
└─21525 nginx: worker process
11月 10 16:23:05 VM-0-14-ubuntu systemd[1]: Starting A high performance web server and a reverse proxy server...
11月 10 16:23:05 VM-0-14-ubuntu systemd[1]: Started A high performance web server and a reverse proxy server.
2、管理Nginx:
#systemctl start nginx
#systemctl stop nginx
#systemctl status nginx
#systemctl restart nginx
3匕荸、配置SSL代理Rstduio Server與Shiny Server爹谭。
A、Nginx的服務(wù)器證書要把自簽CA的證書打包在一起形成完整的證書鏈每聪,否則手機(jī)上訪問SSL會有問題旦棉。有關(guān)自簽證書的生成,請參閱我的另一篇文章《GPU Linux虛擬主機(jī)GN7型安裝配置文檔》药薯。
root@VM-0-14-ubuntu:~# cd /root/cert
root@VM-0-14-ubuntu:~/cert# cat ./demoCA/cacert.pem >> server.crt
B、修改Rstudio Server配置救斑。
為了從同一個https端口分別反向代理Rstudio與Shiny Server童本,Nginx給它們加上了不同的前綴,Rstudio Server加的是/rstudio脸候,Shiny Server加的是/shiny穷娱。可以關(guān)閉防火墻的8787端口运沦,或在Rstudio Server的配置中限制8787端口只能從本機(jī)訪問泵额。
root@VM-0-14-ubuntu:/etc/rstudio# vi rserver.conf
# Server Configuration File
www-port=8787
rsession-which-r=/usr/lib64/R-4.2.1/bin/R
auth-required-user-group=rusers
auth-minimum-user-id=500
# Added by Jean for Nginx reverse proxy 2022/11/10
www-address=127.0.0.1
C、修改Nginx配置携添。
(gpu) root@VM-0-14-ubuntu:/etc/nginx# vi nginx.conf
默認(rèn)的80端口定義了一個非加密的http服務(wù)器嫁盲,提供靜態(tài)的主頁等。增加了一個SSL服務(wù)器的定義烈掠,指定了它使用的自簽服務(wù)器證書與密鑰羞秤,在443號默認(rèn)端口偵聽。然后一個location /rstudio定義是對Rstudio Server的反向代理左敌,一個location /shiny定義是對Shiny Server的反向代理瘾蛋。
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 768;
# multi_accept on;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# SSL Settings
##
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
# Support proxying of web-socket connections
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
# Server on http:80
server {
listen 80;
server_name 106.52.33.185;
location / {
root /usr/share/nginx/html;
index index.html;
autoindex on;
}
}
# Server with SSL enabled
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name 106.52.33.185;
# Change to point to your certs
ssl_certificate /root/cert/server.crt;
ssl_certificate_key /root/cert/server.key;
ssl_protocols TLSv1.2 TLSv1.1 TLSv1;
# Reverse proxy for Rstudio Server
rewrite ^/rstudio$ $scheme://$http_host/rstudio/ permanent;
location /rstudio/ {
# Needed only for prefix of /rstudio
rewrite ^/rstudio/(.*)$ /$1 break;
# Use http here when ssl-enabled=0 is set in rserver.conf
proxy_pass http://localhost:8787;
proxy_redirect http://localhost:8787/ $scheme://$http_host/rstudio/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_read_timeout 20d;
# In some cases, such as when streaming job statuses from the launcher, the default response buffering in nginx
# can be too slow for delivering real-time updates, especially when configured to use SSL.
# disabling response buffering
proxy_buffering off;
}
# Reverse proxy for Shiny server
rewrite ^/shiny$ $scheme://$http_host/shiny/ permanent;
location /shiny/ {
rewrite ^/shiny/(.*)$ /$1 break;
proxy_pass http://localhost:3838;
proxy_redirect / $scheme://$http_host/shiny/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_read_timeout 20d;
proxy_buffering off;
}
}
}
4、通過https加密協(xié)議訪問Rstudio Server與Shiny Server矫限。
重啟各個服務(wù)哺哼。
#systemctl restart rstudio-server.service
#systemctl restart shiny-server
#systemctl restart nginx
A、訪問Rstudio Server
https://106.52.33.185/rstudio/
B叼风、訪問Shiny Server取董。原來的URL中間加上/shiny即可。
https://106.52.33.185/shiny/users/ubuntu/shinyAppExample/
C咬扇、Nginx的非加密靜態(tài)網(wǎng)頁甲葬。
http://106.52.33.185
六、配置R使用OpenGL圖形系統(tǒng)
1懈贺、Ubuntu安裝OpenGL開發(fā)環(huán)境经窖。
參考資料坡垫。
root@VM-0-14-ubuntu:/home/ubuntu# apt-get install libglu1-mesa-dev
root@VM-0-14-ubuntu:/home/ubuntu# apt-get install freeglut3-dev
root@VM-0-14-ubuntu:/home/ubuntu# export DISPLAY=:2
root@VM-0-14-ubuntu:/home/ubuntu# glxinfo | grep "OpenGL version"
OpenGL version string: 3.1 Mesa 20.0.8
編一個OpenGL測試程序。
# cd /home/ubuntu
# vi test.cc
#include <GL/gl.h>
#include <GL/glut.h>
#include <stdlib.h>
void init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glOrtho(-5,5,-5,5,5,15);
glMatrixMode(GL_MODELVIEW);
gluLookAt(0,0,10,0,0,0,0,1,0);
return;
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 0, 0);
glutWireTeapot(3);
glFlush();
return;
}
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE);
glutInitWindowPosition(0,0);
glutInitWindowSize(300, 300);
glutCreateWindow("OpenGL 3D View");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
編譯測試:
root@VM-0-14-ubuntu:/home/ubuntu# gcc -o test test.cc -lGL -lGLU -lglut
root@VM-0-14-ubuntu:/home/ubuntu# ./test
VNC viewer連接遠(yuǎn)端虛擬桌面:
2画侣、R語言使用OpenGL例子冰悠。
A、安裝gdal配乱,參考資料:
root@VM-0-14-ubuntu:/home/ubuntu# apt-get install libgdal-dev libproj-dev gdal-bin -y
root@VM-0-14-ubuntu:/home/ubuntu# ogrinfo --version
GDAL 2.2.3, released 2017/11/20
B溉卓、安裝R軟件包:
>install.packages(c("rgdal","rgl","rayshader","rayrender","ambient"))
C搬泥、Rstudio中測試rgl包桑寨,最后運(yùn)行rgl.close()關(guān)閉X window窗口。
library(rgl)
open3d()
x <- sort(rnorm(1000))
y <- rnorm(1000)
z <- rnorm(1000) + atan2(x,y)
plot3d(x, y, z, col=rainbow(1000))
rgl.postscript("foo.pdf", fmt="pdf")
sessionInfo()
#rgl.close()
VNC Viewer查看:
D忿檩、測試rayshader包尉尾。
library(rgl)
library(rayshader)
library(rayrender)
library(ambient)
loadzip = tempfile()
download.file("https://tylermw.com/data/dem_01.tif.zip", loadzip)
localtif = raster::raster(unzip(loadzip, "dem_01.tif"))
unlink(loadzip)
elmat = raster_to_matrix(localtif)
montshadow = ray_shade(montereybay, zscale = 50, lambert = FALSE)
montamb = ambient_shade(montereybay, zscale = 50)
montereybay %>%
sphere_shade(zscale = 10, texture = "imhof1") %>%
add_shadow(montshadow, 0.5) %>%
add_shadow(montamb,0) %>%
plot_3d(montereybay, zscale = 50, fov = 0, theta = -100, phi = 30, windowsize = c(1000, 800), zoom = 0.6,
water = TRUE, waterdepth = 0, waterlinecolor = "white", waterlinealpha = 0.5,
wateralpha = 0.5, watercolor = "lightblue")
render_label(montereybay, x = 350, y = 160, z = 1000, zscale = 50,
text = "Moss Landing", textsize = 2, linewidth = 5)
render_label(montereybay, x = 220, y = 70, z = 7000, zscale = 50,
text = "Santa Cruz", textcolor = "darkred", linecolor = "darkred",
textsize = 2, linewidth = 5)
render_label(montereybay, x = 300, y = 270, z = 4000, zscale = 50,
text = "Monterey", dashed = TRUE, textsize = 2, linewidth = 5)
render_label(montereybay, x = 50, y = 270, z = 1000, zscale = 50, textcolor = "white", linecolor = "white",
text = "Monterey Canyon", relativez = FALSE, textsize = 2, linewidth = 5)
Sys.sleep(0.2)
render_snapshot('demo.png')
#rgl.close()
VNC Viewer查看: