在Ubuntu18上编译安装R语言环境 定稿

一、编译安装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:~# 

  参阅资料,依次执行下面的命令升级。

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图形界面
  详细介绍见我的另一篇文章《在Rstudio Linux Server上使用X window OpenGL图形系统》,以及该参考资料1参考资料2

# apt install xvfb x11vnc
# Xvfb :2 -screen 0 1280x960x24 2>/dev/null &
# export DISPLAY=:2

  配置开机自动启动这两个服务,该镜像已经激活了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配置一个开机启动服务:

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

  激活服务并重启系统:

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部分后,系统中装了好几个版本的libicu:libicu60.2(ubuntu18.04),libicu58(Anaconda3),libicu56(CUDA 11)。参考下面的资料,从源码重新编译安装libicu58,把ubuntu18中libicu的版本降下来。参考资料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
......

  用系统默认的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使环境变量生效。

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是自由软件,不带任何担保。
在某些条件下你可以将其自由散布。
用'license()'或'licence()'来看散布的详细条件。

R是个合作计划,有许多人为之做出了贡献.
用'contributors()'来看合作者的详细情况
用'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进程指定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、执行安装,会下载安装依赖的几个软件包,然后安装程序要启动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。
(正在读取数据库 ... 系统当前共安装有 134749 个文件和目录。)
正准备解包 rstudio-server-2022.07.2-576-amd64.deb  ...
正在解包 rstudio-server (2022.07.2+576) ...
正在设置 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默认uid 1000以下的用户(系统用户)不能登录,要查询用户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、服务器配置。

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)全局选项,否则作图会出错,因为服务器上没有物理的X11图形设备。这里是正常的作图,显示在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".
或者配置服务器的全局选项,参阅:参考资料

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进程的图形设备都用cairo,否则后面运行Shiny App时会报错:“无法打开链结到X11显示,无法打开PNG设备”,因为ubuntu上它默认的图形设备是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。
先让服务器成为系统守护进程,开机自启动。

#systemctl enable rstudio-server.service

管理服务:

#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、登录系统测试。
访问网址http://106.52.33.185:8787,输入用户名ubuntu/password登录,运行一段简单的作图程序测试。现在是没有https加密的,先用着,后面再与Shiny Server一起配Nginx反向代理https加密。


Rstudio Server初步装好

三、安装Shiny等常用软件包。
1、安装shiny及rmarkdown包,用于Rstudio IDE中开发测试。

>install.packages("shiny")
>install.packages("rmarkdown")

会从源码下载安装一大堆依赖的包。
运行一个简单的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)
Rstudio-installation-2.PNG

2、安装reticulate、tensorflow、keras连接Python深度学习环境。
深度学习框架Tensorflow GPU只有Python版,Rstudio通过reticulate包连接Python调用它们,R语言的tensorflow与keras包提供了对Python版相应包的封装,参阅资料1参阅资料2
A、安装软件包。用root在终端上启动R,安装到R的系统library目录下。

>install.packages("reticulate")
>install.packages("tensorflow")
>install.packages("keras")
>install.packages("dplyr")

上面的命令会自动安装它们依赖的其它R包。
B、设置RETICULATE_PYTHON环境变量,所有R进程都使用Anaconda3的Python,注意用的是虚拟环境"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

增加下面的环境变量。

# Added for python by Jean 2022/02/27
RETICULATE_PYTHON='/usr/local/anaconda3/envs/gpu/bin/python'

重启当前的Rstudio Session以使环境变量生效,运行一个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()
在Rstudio中运行Python程序

C、测试keras和tensorflow-gpu,testKeras.R。

library(reticulate)
library(keras)
library(dplyr)

py_discover_config()
py_config()

mnist <- dataset_mnist()
# keras的数据文件默认放在/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)

# 设计神经网络结构,由2个完全连接层处理,一个有512个神经元,一个有10个神经元, 28*28=784 -> 512 ->10
# 用线性堆栈层构建模型
network <- keras_model_sequential() %>% 
  layer_dense(units = 512, activation = "relu", input_shape = c(28 * 28)) %>% 
  layer_dense(units = 10, activation = "softmax")  # 第二层没有输入格式参数,接受第一层的输出,512维向量。

# 设计算法的损失函数,优化算法和度量准则
network %>% compile(
  optimizer = "rmsprop",                 # 优化器,选择的梯度下降算法
  loss = "categorical_crossentropy",     # 损失函数
  metrics = c("accuracy")                # 观察指标
)

# 数据预处理, 每张图由二维数组变为1维数组,输入是1维数组(1维张量),标准化至[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
# 目标变量因子化,变为类变量
train_labels <- to_categorical(train_label)
test_labels <- to_categorical(test_label)

dim(train_images)
dim(test_images)
dim(train_labels)
dim(test_labels)

# 训练数据集上拟合模型
network %>% fit(train_images, train_labels, epochs = 5, batch_size = 128)
# 60000/128 =469,GPU版并行处理,每批128个样本,共469批,书中 60000/60000是CPU版串行训练,每次迭代要9~10s

# 测试数据集测试模型
metrics <- network %>% evaluate(test_images, test_labels, batch_size = 128)
metrics
# 10000/313=32,GPU并行处理同上。evaluate()如果没有给出 batch_size参数,默认是32。

# 测试样本的前10个样本的类别预测
# 参阅 https://blog.csdn.net/yiyihuazi/article/details/122323349
# keras 2.6删除了predict_classes()函数
#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经过reshape,要引用原来载入的数据。
# 第9个数字识别为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.
前面已经安装好了,直接装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。
(正在读取数据库 ... 系统当前共安装有 138801 个文件和目录。)
正准备解包 shiny-server-1.5.19.995-amd64.deb  ...
正在解包 shiny-server (1.5.19.995) ...
正在设置 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没有装在默认的位置,shiny server在尝试运行Shiny App时没有找到R命令,需要设置Shiny Server使用的R版本,参阅资料1参阅资料2,r_path参数只有在专业版中才有。于是参考该帖子建立一个软连接解决,不用改配置。

# 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,安装完成。现在它没有加密连接,先用着。Rstudio Server与Shiny Server的开源免费版都不支持加密连接,后面再通过Nginx反向代理加密。

验证Shiny Server安装

4、配置Shiny Server为用户发布模式,允许用户自行发布App。
这样用户只要把APP拷贝到Ta的home目录下的~/ShinyApps目录下即可,比如ubuntu把/home/ubuntu/Rscripts/ShinyDev/shinyAppExample拷贝成/home/ubuntu/ShinyApps/shinyAppExample就发布了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)单文件发布,在子目录下命名为app.R,DESCRIPTION文件可选。
2)双文件发布,在子目录下命名为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

访问用户自行发布的Shiny App

五、安装Nginx并配置反向代理
1、安装Nginx,参阅资料

(gpu) root@VM-0-14-ubuntu:/var/log/shiny-server# apt install nginx
正在读取软件包列表... 完成
正在分析软件包的依赖关系树       
正在读取状态信息... 完成       
将会同时安装下列软件:
  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 的额外空间。
您希望继续执行吗? [Y/n] y
......
正在处理用于 systemd (237-3ubuntu10.56) 的触发器 ...
(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的服务器证书要把自签CA的证书打包在一起形成完整的证书链,否则手机上访问SSL会有问题。有关自签证书的生成,请参阅我的另一篇文章《GPU Linux虚拟主机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。可以关闭防火墙的8787端口,或在Rstudio Server的配置中限制8787端口只能从本机访问。

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

默认的80端口定义了一个非加密的http服务器,提供静态的主页等。增加了一个SSL服务器的定义,指定了它使用的自签服务器证书与密钥,在443号默认端口侦听。然后一个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加密协议访问Rstudio Server与Shiny Server。
重启各个服务。

#systemctl restart rstudio-server.service
#systemctl restart shiny-server
#systemctl restart nginx

A、访问Rstudio Server
https://106.52.33.185/rstudio/

通过https加密协议访问Rstudio Server

B、访问Shiny Server。原来的URL中间加上/shiny即可。
https://106.52.33.185/shiny/users/ubuntu/shinyAppExample/
通过https加密协议访问Shiny Server

C、Nginx的非加密静态网页。
http://106.52.33.185
Nginx的非加密通道可以提供一些不包含敏感信息的静态网页

六、配置R使用OpenGL图形系统
1、Ubuntu安装OpenGL开发环境。
参考资料

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连接远端虚拟桌面:

OpenGL开发环境测试

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包,最后运行rgl.close()关闭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查看:


测试rgl包正确运行

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查看:


测试rayshader包正确运行,远程桌面打开了OpenGL 3D X Window作图
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容