Shinyアプリケーションを cloudflare tunnel で配信する。

RにはShinyというWebAppを作成することができるpackageがあります。この記事では、shinyで作成したWebAppをCloudflareTunnelで配信する方法を解説します。

## 環境
OS: ubuntu22.04.2

## 環境構築
まずは、Rのインストールを行い、shinyとshiny-serverの2つをインストールする。
※作成したWebAppに他のpackageを使用している場合は、そのpackageもインストールしてください。これで環境構築は完了です。
```shell
#Rのインストール
$ sudo apt update
$ sudo apt upgrade
$ sudo apt install r-base
#Rのターミナルの起動
$ sudo -i R
```
Rのターミナルで各種packageをインストール
```r
> install.packages("shiny")
```
shiny serverのインストール
```shell
$ sudo apt-get install gdebi-core
$ wget https://download3.rstudio.org/ubuntu-18.04/x86_64/shiny-server-1.5.20.1002-amd64.deb
$ sudo gdebi shiny-server-1.5.20.1002-amd64.deb
```
※上記のコマンドは、2023/8/11時点でposit社のサイトに記載されていたものです。最新のコマンドは、以下のposit社のサイトからご確認ください。
[posit社のリンク]

https://posit.co/products/open-source/shinyserver/

## Rのpackageのインストールでつまずきがちなポイント
#### ポイント1: インストールエラーの解決
各種必要なpackageをインストールしていく際に、私が実際につまずいたことを書いておきます。
packageのインストールを行っていくと、インストールエラーを起こすpackageがあります。私の場合は、devtoolsで起きました。
以下、エラーメッセージです。長いので折りたたんでいます。(devtoolsのインストール時に起きたエラー)
<details><summary>1回目のエラーメッセージ</summary>

```shell
> install.packages("devtools")
Installing package into ‘/usr/local/lib/R/site-library’
(as ‘lib’ is unspecified)
Warning in install.packages("devtools") :
  'lib = "/usr/local/lib/R/site-library"' is not writable
Would you like to use a personal library instead? (yes/No/cancel) yes
Would you like to create a personal library
‘~/R/x86_64-pc-linux-gnu-library/4.1’
to install packages into? (yes/No/cancel) yes
also installing the dependencies ‘credentials’, ‘openssl’, ‘httr2’, ‘systemfonts’, ‘textshaping’, ‘curl’, ‘gert’, ‘gh’, ‘httr’, ‘ragg’, ‘xml2’, ‘usethis’, ‘pkgdown’, ‘rcmdcheck’, ‘roxygen2’, ‘rversions’, ‘urlchecker’
(中略)
* installing *source* package ‘openssl’ ...
** package ‘openssl’ successfully unpacked and MD5 sums checked
** using staged installation
Using PKG_CFLAGS=
--------------------------- [ANTICONF] --------------------------------
Configuration failed because openssl was not found. Try installing:
 * deb: libssl-dev (Debian, Ubuntu, etc)
 * rpm: openssl-devel (Fedora, CentOS, RHEL)
 * csw: libssl_dev (Solaris)
 * brew: openssl (Mac OSX)
If openssl is already installed, check that 'pkg-config' is in your
PATH and PKG_CONFIG_PATH contains a openssl.pc file. If pkg-config
is unavailable you can set INCLUDE_DIR and LIB_DIR manually via:
R CMD INSTALL --configure-vars='INCLUDE_DIR=... LIB_DIR=...'
-------------------------- [ERROR MESSAGE] ---------------------------
tools/version.c:1:10: fatal error: openssl/opensslv.h: No such file or directory
    1 | #include <openssl/opensslv.h>
      |          ^~~~~~~~~~~~~~~~~~~~
compilation terminated.
--------------------------------------------------------------------
ERROR: configuration failed for package ‘openssl’
* removing ‘/home/hello/R/x86_64-pc-linux-gnu-library/4.1/openssl’
* installing *source* package ‘systemfonts’ ...
** package ‘systemfonts’ successfully unpacked and MD5 sums checked
** using staged installation
Package fontconfig was not found in the pkg-config search path.
Perhaps you should add the directory containing `fontconfig.pc'
to the PKG_CONFIG_PATH environment variable
No package 'fontconfig' found
Package freetype2 was not found in the pkg-config search path.
Perhaps you should add the directory containing `freetype2.pc'
to the PKG_CONFIG_PATH environment variable
No package 'freetype2' found
Using PKG_CFLAGS=
Using PKG_LIBS=-lfontconfig -lfreetype
--------------------------- [ANTICONF] --------------------------------
Configuration failed to find the fontconfig freetype2 library. Try installing:
 * deb: libfontconfig1-dev (Debian, Ubuntu, etc)
 * rpm: fontconfig-devel (Fedora, EPEL)
 * csw: fontconfig_dev (Solaris)
 * brew: freetype (OSX)
If fontconfig freetype2 is already installed, check that 'pkg-config' is in your
PATH and PKG_CONFIG_PATH contains a fontconfig freetype2.pc file. If pkg-config
is unavailable you can set INCLUDE_DIR and LIB_DIR manually via:
R CMD INSTALL --configure-vars='INCLUDE_DIR=... LIB_DIR=...'
-------------------------- [ERROR MESSAGE] ---------------------------
<stdin>:1:10: fatal error: fontconfig/fontconfig.h: No such file or directory
compilation terminated.
--------------------------------------------------------------------
ERROR: configuration failed for package ‘systemfonts’
* removing ‘/home/hello/R/x86_64-pc-linux-gnu-library/4.1/systemfonts’
* installing *source* package ‘curl’ ...
** package ‘curl’ successfully unpacked and MD5 sums checked
** using staged installation
Package libcurl was not found in the pkg-config search path.
Perhaps you should add the directory containing `libcurl.pc'
to the PKG_CONFIG_PATH environment variable
No package 'libcurl' found
Package libcurl was not found in the pkg-config search path.
Perhaps you should add the directory containing `libcurl.pc'
to the PKG_CONFIG_PATH environment variable
No package 'libcurl' found
Using PKG_CFLAGS=
Using PKG_LIBS=-lcurl
--------------------------- [ANTICONF] --------------------------------
Configuration failed because libcurl was not found. Try installing:
 * deb: libcurl4-openssl-dev (Debian, Ubuntu, etc)
 * rpm: libcurl-devel (Fedora, CentOS, RHEL)
If libcurl is already installed, check that 'pkg-config' is in your
PATH and PKG_CONFIG_PATH contains a libcurl.pc file. If pkg-config
is unavailable you can set INCLUDE_DIR and LIB_DIR manually via:
R CMD INSTALL --configure-vars='INCLUDE_DIR=... LIB_DIR=...'
-------------------------- [ERROR MESSAGE] ---------------------------
<stdin>:1:10: fatal error: curl/curl.h: No such file or directory
compilation terminated.
--------------------------------------------------------------------
ERROR: configuration failed for package ‘curl’
* removing ‘/home/hello/R/x86_64-pc-linux-gnu-library/4.1/curl’
* installing *source* package ‘xml2’ ...
** package ‘xml2’ successfully unpacked and MD5 sums checked
** using staged installation
Package libxml-2.0 was not found in the pkg-config search path.
Perhaps you should add the directory containing `libxml-2.0.pc'
to the PKG_CONFIG_PATH environment variable
No package 'libxml-2.0' found
Package libxml-2.0 was not found in the pkg-config search path.
Perhaps you should add the directory containing `libxml-2.0.pc'
to the PKG_CONFIG_PATH environment variable
No package 'libxml-2.0' found
Using PKG_CFLAGS=
Using PKG_LIBS=-lxml2
------------------------- ANTICONF ERROR ---------------------------
Configuration failed because libxml-2.0 was not found. Try installing:
 * deb: libxml2-dev (Debian, Ubuntu, etc)
 * rpm: libxml2-devel (Fedora, CentOS, RHEL)
 * csw: libxml2_dev (Solaris)
If libxml-2.0 is already installed, check that 'pkg-config' is in your
PATH and PKG_CONFIG_PATH contains a libxml-2.0.pc file. If pkg-config
is unavailable you can set INCLUDE_DIR and LIB_DIR manually via:
R CMD INSTALL --configure-vars='INCLUDE_DIR=... LIB_DIR=...'
--------------------------------------------------------------------
ERROR: configuration failed for package ‘xml2’
* removing ‘/home/hello/R/x86_64-pc-linux-gnu-library/4.1/xml2’
ERROR: dependencies ‘openssl’, ‘curl’ are not available for package ‘credentials’
* removing ‘/home/hello/R/x86_64-pc-linux-gnu-library/4.1/credentials’
ERROR: dependencies ‘curl’, ‘openssl’ are not available for package ‘httr2’
* removing ‘/home/hello/R/x86_64-pc-linux-gnu-library/4.1/httr2’
ERROR: dependency ‘systemfonts’ is not available for package ‘textshaping’
* removing ‘/home/hello/R/x86_64-pc-linux-gnu-library/4.1/textshaping’
ERROR: dependencies ‘curl’, ‘openssl’ are not available for package ‘httr’
* removing ‘/home/hello/R/x86_64-pc-linux-gnu-library/4.1/httr’
ERROR: dependency ‘curl’ is not available for package ‘rcmdcheck’
* removing ‘/home/hello/R/x86_64-pc-linux-gnu-library/4.1/rcmdcheck’
ERROR: dependency ‘xml2’ is not available for package ‘roxygen2’
* removing ‘/home/hello/R/x86_64-pc-linux-gnu-library/4.1/roxygen2’
ERROR: dependencies ‘curl’, ‘xml2’ are not available for package ‘rversions’
* removing ‘/home/hello/R/x86_64-pc-linux-gnu-library/4.1/rversions’
ERROR: dependencies ‘curl’, ‘xml2’ are not available for package ‘urlchecker’
* removing ‘/home/hello/R/x86_64-pc-linux-gnu-library/4.1/urlchecker’
ERROR: dependencies ‘credentials’, ‘openssl’ are not available for package ‘gert’
* removing ‘/home/hello/R/x86_64-pc-linux-gnu-library/4.1/gert’
ERROR: dependency ‘httr2’ is not available for package ‘gh’
* removing ‘/home/hello/R/x86_64-pc-linux-gnu-library/4.1/gh’
ERROR: dependencies ‘systemfonts’, ‘textshaping’ are not available for package ‘ragg’
* removing ‘/home/hello/R/x86_64-pc-linux-gnu-library/4.1/ragg’
ERROR: dependencies ‘curl’, ‘gert’, ‘gh’ are not available for package ‘usethis’
* removing ‘/home/hello/R/x86_64-pc-linux-gnu-library/4.1/usethis’
ERROR: dependencies ‘httr’, ‘ragg’, ‘xml2’ are not available for package ‘pkgdown’
* removing ‘/home/hello/R/x86_64-pc-linux-gnu-library/4.1/pkgdown’
ERROR: dependencies ‘usethis’, ‘pkgdown’, ‘rcmdcheck’, ‘roxygen2’, ‘rversions’, ‘urlchecker’ are not available for package ‘devtools’
* removing ‘/home/hello/R/x86_64-pc-linux-gnu-library/4.1/devtools’

The downloaded source packages are in
	‘/tmp/Rtmp9vMEe8/downloaded_packages’
There were 18 warnings (use warnings() to see them)
```
</details>

<details><summary>2回目のエラーメッセージ</summary>

```shell
(前略)
--------------------------- [ANTICONF] --------------------------------
Configuration failed to find the harfbuzz freetype2 fribidi library. Try installing:
 * deb: libharfbuzz-dev libfribidi-dev (Debian, Ubuntu, etc)
 * rpm: harfbuzz-devel fribidi-devel (Fedora, EPEL)
 * csw: libharfbuzz_dev libfribidi_dev (Solaris)
 * brew: harfbuzz fribidi (OSX)
If harfbuzz freetype2 fribidi is already installed, check that 'pkg-config' is in your
PATH and PKG_CONFIG_PATH contains a harfbuzz freetype2 fribidi.pc file. If pkg-config
is unavailable you can set INCLUDE_DIR and LIB_DIR manually via:
R CMD INSTALL --configure-vars='INCLUDE_DIR=... LIB_DIR=...'
-------------------------- [ERROR MESSAGE] ---------------------------
<stdin>:1:10: fatal error: hb-ft.h: No such file or directory
compilation terminated.
--------------------------------------------------------------------
ERROR: configuration failed for package ‘textshaping’
* removing ‘/usr/local/lib/R/site-library/textshaping’
* installing *source* package ‘httr’ ...
** package ‘httr’ successfully unpacked and MD5 sums checked
** using staged installation
** R
** demo
** inst
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
** building package indices
** installing vignettes
** testing if installed package can be loaded from temporary location
** testing if installed package can be loaded from final location
** testing if installed package keeps a record of temporary installation path
* DONE (httr)
* installing *source* package ‘rcmdcheck’ ...
** package ‘rcmdcheck’ successfully unpacked and MD5 sums checked
** using staged installation
** R
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded from temporary location
** testing if installed package can be loaded from final location
** testing if installed package keeps a record of temporary installation path
* DONE (rcmdcheck)
* installing *source* package ‘roxygen2’ ...
** package ‘roxygen2’ successfully unpacked and MD5 sums checked
** using staged installation
** libs
g++ -std=gnu++11 -I"/usr/share/R/include" -DNDEBUG  -I'/usr/local/lib/R/site-library/cpp11/include'    -fpic  -g -O2 -ffile-prefix-map=/build/r-base-4A2Reg/r-base-4.1.2=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g  -c cpp11.cpp -o cpp11.o
g++ -std=gnu++11 -I"/usr/share/R/include" -DNDEBUG  -I'/usr/local/lib/R/site-library/cpp11/include'    -fpic  -g -O2 -ffile-prefix-map=/build/r-base-4A2Reg/r-base-4.1.2=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g  -c escapeExamples.cpp -o escapeExamples.o
g++ -std=gnu++11 -I"/usr/share/R/include" -DNDEBUG  -I'/usr/local/lib/R/site-library/cpp11/include'    -fpic  -g -O2 -ffile-prefix-map=/build/r-base-4A2Reg/r-base-4.1.2=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g  -c isComplete.cpp -o isComplete.o
g++ -std=gnu++11 -I"/usr/share/R/include" -DNDEBUG  -I'/usr/local/lib/R/site-library/cpp11/include'    -fpic  -g -O2 -ffile-prefix-map=/build/r-base-4A2Reg/r-base-4.1.2=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g  -c leadingSpaces.cpp -o leadingSpaces.o
g++ -std=gnu++11 -I"/usr/share/R/include" -DNDEBUG  -I'/usr/local/lib/R/site-library/cpp11/include'    -fpic  -g -O2 -ffile-prefix-map=/build/r-base-4A2Reg/r-base-4.1.2=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g  -c parser2.cpp -o parser2.o
g++ -std=gnu++11 -I"/usr/share/R/include" -DNDEBUG  -I'/usr/local/lib/R/site-library/cpp11/include'    -fpic  -g -O2 -ffile-prefix-map=/build/r-base-4A2Reg/r-base-4.1.2=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g  -c wrapUsage.cpp -o wrapUsage.o
g++ -std=gnu++11 -shared -L/usr/lib/R/lib -Wl,-Bsymbolic-functions -flto=auto -ffat-lto-objects -flto=auto -Wl,-z,relro -o roxygen2.so cpp11.o escapeExamples.o isComplete.o leadingSpaces.o parser2.o wrapUsage.o -L/usr/lib/R/lib -lR
installing to /usr/local/lib/R/site-library/00LOCK-roxygen2/00new/roxygen2/libs
** R
** inst
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
*** copying figures
** building package indices
** installing vignettes
** testing if installed package can be loaded from temporary location
** checking absolute paths in shared objects and dynamic libraries
** testing if installed package can be loaded from final location
** testing if installed package keeps a record of temporary installation path
* DONE (roxygen2)
* installing *source* package ‘rversions’ ...
** package ‘rversions’ successfully unpacked and MD5 sums checked
** using staged installation
** R
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
*** copying figures
** building package indices
** testing if installed package can be loaded from temporary location
** testing if installed package can be loaded from final location
** testing if installed package keeps a record of temporary installation path
* DONE (rversions)
* installing *source* package ‘urlchecker’ ...
** package ‘urlchecker’ successfully unpacked and MD5 sums checked
** using staged installation
** R
** inst
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded from temporary location
** testing if installed package can be loaded from final location
** testing if installed package keeps a record of temporary installation path
* DONE (urlchecker)
* installing *source* package ‘gert’ ...
** package ‘gert’ successfully unpacked and MD5 sums checked
** using staged installation
Found OpenSSL3
Using static libgit2 for Linux x86_64
Using PKG_CFLAGS=-DSTATIC_LIBGIT2 -I/tmp/RtmpqKhLaL/R.INSTALL8eba3a281f1c/gert/libgit2-1.4.2-x86_64_linux/include
Using PKG_LIBS=-L/tmp/RtmpqKhLaL/R.INSTALL8eba3a281f1c/gert/libgit2-1.4.2-x86_64_linux/lib -lstatgit2 -lrt -lpthread -lstatssh2 -lstatssl -lstatcrypto -ldl
Configuration OK!
** libs
rm -f gert.so branch.o clone.o commit.o config.o conflicts.o files.o ignore.o init.o merge.o rebase.o stash.o submodules.o tag.o utils.o version.o
gcc -I"/usr/share/R/include" -DNDEBUG -DSTATIC_LIBGIT2 -I/tmp/RtmpqKhLaL/R.INSTALL8eba3a281f1c/gert/libgit2-1.4.2-x86_64_linux/include -DR_NO_REMAP -DSTRICT_R_HEADERS    -fvisibility=hidden -fpic  -g -O2 -ffile-prefix-map=/build/r-base-4A2Reg/r-base-4.1.2=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g  -c branch.c -o branch.o
gcc -I"/usr/share/R/include" -DNDEBUG -DSTATIC_LIBGIT2 -I/tmp/RtmpqKhLaL/R.INSTALL8eba3a281f1c/gert/libgit2-1.4.2-x86_64_linux/include -DR_NO_REMAP -DSTRICT_R_HEADERS    -fvisibility=hidden -fpic  -g -O2 -ffile-prefix-map=/build/r-base-4A2Reg/r-base-4.1.2=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g  -c clone.c -o clone.o
gcc -I"/usr/share/R/include" -DNDEBUG -DSTATIC_LIBGIT2 -I/tmp/RtmpqKhLaL/R.INSTALL8eba3a281f1c/gert/libgit2-1.4.2-x86_64_linux/include -DR_NO_REMAP -DSTRICT_R_HEADERS    -fvisibility=hidden -fpic  -g -O2 -ffile-prefix-map=/build/r-base-4A2Reg/r-base-4.1.2=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g  -c commit.c -o commit.o
gcc -I"/usr/share/R/include" -DNDEBUG -DSTATIC_LIBGIT2 -I/tmp/RtmpqKhLaL/R.INSTALL8eba3a281f1c/gert/libgit2-1.4.2-x86_64_linux/include -DR_NO_REMAP -DSTRICT_R_HEADERS    -fvisibility=hidden -fpic  -g -O2 -ffile-prefix-map=/build/r-base-4A2Reg/r-base-4.1.2=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g  -c config.c -o config.o
gcc -I"/usr/share/R/include" -DNDEBUG -DSTATIC_LIBGIT2 -I/tmp/RtmpqKhLaL/R.INSTALL8eba3a281f1c/gert/libgit2-1.4.2-x86_64_linux/include -DR_NO_REMAP -DSTRICT_R_HEADERS    -fvisibility=hidden -fpic  -g -O2 -ffile-prefix-map=/build/r-base-4A2Reg/r-base-4.1.2=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g  -c conflicts.c -o conflicts.o
gcc -I"/usr/share/R/include" -DNDEBUG -DSTATIC_LIBGIT2 -I/tmp/RtmpqKhLaL/R.INSTALL8eba3a281f1c/gert/libgit2-1.4.2-x86_64_linux/include -DR_NO_REMAP -DSTRICT_R_HEADERS    -fvisibility=hidden -fpic  -g -O2 -ffile-prefix-map=/build/r-base-4A2Reg/r-base-4.1.2=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g  -c files.c -o files.o
gcc -I"/usr/share/R/include" -DNDEBUG -DSTATIC_LIBGIT2 -I/tmp/RtmpqKhLaL/R.INSTALL8eba3a281f1c/gert/libgit2-1.4.2-x86_64_linux/include -DR_NO_REMAP -DSTRICT_R_HEADERS    -fvisibility=hidden -fpic  -g -O2 -ffile-prefix-map=/build/r-base-4A2Reg/r-base-4.1.2=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g  -c ignore.c -o ignore.o
gcc -I"/usr/share/R/include" -DNDEBUG -DSTATIC_LIBGIT2 -I/tmp/RtmpqKhLaL/R.INSTALL8eba3a281f1c/gert/libgit2-1.4.2-x86_64_linux/include -DR_NO_REMAP -DSTRICT_R_HEADERS    -fvisibility=hidden -fpic  -g -O2 -ffile-prefix-map=/build/r-base-4A2Reg/r-base-4.1.2=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g  -c init.c -o init.o
gcc -I"/usr/share/R/include" -DNDEBUG -DSTATIC_LIBGIT2 -I/tmp/RtmpqKhLaL/R.INSTALL8eba3a281f1c/gert/libgit2-1.4.2-x86_64_linux/include -DR_NO_REMAP -DSTRICT_R_HEADERS    -fvisibility=hidden -fpic  -g -O2 -ffile-prefix-map=/build/r-base-4A2Reg/r-base-4.1.2=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g  -c merge.c -o merge.o
gcc -I"/usr/share/R/include" -DNDEBUG -DSTATIC_LIBGIT2 -I/tmp/RtmpqKhLaL/R.INSTALL8eba3a281f1c/gert/libgit2-1.4.2-x86_64_linux/include -DR_NO_REMAP -DSTRICT_R_HEADERS    -fvisibility=hidden -fpic  -g -O2 -ffile-prefix-map=/build/r-base-4A2Reg/r-base-4.1.2=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g  -c rebase.c -o rebase.o
gcc -I"/usr/share/R/include" -DNDEBUG -DSTATIC_LIBGIT2 -I/tmp/RtmpqKhLaL/R.INSTALL8eba3a281f1c/gert/libgit2-1.4.2-x86_64_linux/include -DR_NO_REMAP -DSTRICT_R_HEADERS    -fvisibility=hidden -fpic  -g -O2 -ffile-prefix-map=/build/r-base-4A2Reg/r-base-4.1.2=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g  -c stash.c -o stash.o
gcc -I"/usr/share/R/include" -DNDEBUG -DSTATIC_LIBGIT2 -I/tmp/RtmpqKhLaL/R.INSTALL8eba3a281f1c/gert/libgit2-1.4.2-x86_64_linux/include -DR_NO_REMAP -DSTRICT_R_HEADERS    -fvisibility=hidden -fpic  -g -O2 -ffile-prefix-map=/build/r-base-4A2Reg/r-base-4.1.2=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g  -c submodules.c -o submodules.o
gcc -I"/usr/share/R/include" -DNDEBUG -DSTATIC_LIBGIT2 -I/tmp/RtmpqKhLaL/R.INSTALL8eba3a281f1c/gert/libgit2-1.4.2-x86_64_linux/include -DR_NO_REMAP -DSTRICT_R_HEADERS    -fvisibility=hidden -fpic  -g -O2 -ffile-prefix-map=/build/r-base-4A2Reg/r-base-4.1.2=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g  -c tag.c -o tag.o
gcc -I"/usr/share/R/include" -DNDEBUG -DSTATIC_LIBGIT2 -I/tmp/RtmpqKhLaL/R.INSTALL8eba3a281f1c/gert/libgit2-1.4.2-x86_64_linux/include -DR_NO_REMAP -DSTRICT_R_HEADERS    -fvisibility=hidden -fpic  -g -O2 -ffile-prefix-map=/build/r-base-4A2Reg/r-base-4.1.2=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g  -c utils.c -o utils.o
gcc -I"/usr/share/R/include" -DNDEBUG -DSTATIC_LIBGIT2 -I/tmp/RtmpqKhLaL/R.INSTALL8eba3a281f1c/gert/libgit2-1.4.2-x86_64_linux/include -DR_NO_REMAP -DSTRICT_R_HEADERS    -fvisibility=hidden -fpic  -g -O2 -ffile-prefix-map=/build/r-base-4A2Reg/r-base-4.1.2=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g  -c version.c -o version.o
gcc -shared -L/usr/lib/R/lib -Wl,-Bsymbolic-functions -flto=auto -ffat-lto-objects -flto=auto -Wl,-z,relro -o gert.so branch.o clone.o commit.o config.o conflicts.o files.o ignore.o init.o merge.o rebase.o stash.o submodules.o tag.o utils.o version.o -L/tmp/RtmpqKhLaL/R.INSTALL8eba3a281f1c/gert/libgit2-1.4.2-x86_64_linux/lib -lstatgit2 -lrt -lpthread -lstatssh2 -lstatssl -lstatcrypto -ldl -L/usr/lib/R/lib -lR
installing to /usr/local/lib/R/site-library/00LOCK-gert/00new/gert/libs
** R
** inst
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
*** copying figures
** building package indices
** installing vignettes
** testing if installed package can be loaded from temporary location
** checking absolute paths in shared objects and dynamic libraries
** testing if installed package can be loaded from final location
** testing if installed package keeps a record of temporary installation path
* DONE (gert)
* installing *source* package ‘gh’ ...
** package ‘gh’ successfully unpacked and MD5 sums checked
** using staged installation
** R
** inst
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
** building package indices
** installing vignettes
** testing if installed package can be loaded from temporary location
** testing if installed package can be loaded from final location
** testing if installed package keeps a record of temporary installation path
* DONE (gh)
ERROR: dependency ‘textshaping’ is not available for package ‘ragg’
* removing ‘/usr/local/lib/R/site-library/ragg’
* installing *source* package ‘usethis’ ...
** package ‘usethis’ successfully unpacked and MD5 sums checked
** using staged installation
** R
** inst
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
*** copying figures
** building package indices
** testing if installed package can be loaded from temporary location
** testing if installed package can be loaded from final location
** testing if installed package keeps a record of temporary installation path
* DONE (usethis)
ERROR: dependency ‘ragg’ is not available for package ‘pkgdown’
* removing ‘/usr/local/lib/R/site-library/pkgdown’
ERROR: dependency ‘pkgdown’ is not available for package ‘devtools’
* removing ‘/usr/local/lib/R/site-library/devtools’

The downloaded source packages are in
	‘/tmp/RtmpH2i1vg/downloaded_packages’
Warning messages:
1: In install.packages("devtools") :
  installation of package ‘textshaping’ had non-zero exit status
2: In install.packages("devtools") :
  installation of package ‘ragg’ had non-zero exit status
3: In install.packages("devtools") :
  installation of package ‘pkgdown’ had non-zero exit status
4: In install.packages("devtools") :
  installation of package ‘devtools’ had non-zero exit status
```
</details>
<details><summary>3回目のエラーメッセージ</summary>

```shell
(前略)
-----------------------------[ ANTICONF ]-------------------------------
Configuration failed to find one of freetype2 libpng libtiff-4 libjpeg. Try installing:
 * deb: libfreetype6-dev libpng-dev libtiff5-dev libjpeg-dev (Debian, Ubuntu, etc)
 * rpm: freetype-devel libpng-devel libtiff-devel libjpeg-turbo-devel (Fedora, CentOS, RHEL)
 * csw: libfreetype_dev libpng16_dev libtiff_dev libjpeg_dev (Solaris)
If freetype2 libpng libtiff-4 libjpeg is already installed, check that 'pkg-config' is in your
PATH and PKG_CONFIG_PATH contains a freetype2 libpng libtiff-4 libjpeg.pc file. If pkg-config
is unavailable you can set INCLUDE_DIR and LIB_DIR manually via:
R CMD INSTALL --configure-vars='INCLUDE_DIR=... LIB_DIR=...'
-------------------------- [ERROR MESSAGE] ---------------------------
<stdin>:1:10: fatal error: ft2build.h: No such file or directory
compilation terminated.
--------------------------------------------------------------------
ERROR: configuration failed for package ‘ragg’
* removing ‘/usr/local/lib/R/site-library/ragg’
ERROR: dependency ‘ragg’ is not available for package ‘pkgdown’
* removing ‘/usr/local/lib/R/site-library/pkgdown’
ERROR: dependency ‘pkgdown’ is not available for package ‘devtools’
* removing ‘/usr/local/lib/R/site-library/devtools’

The downloaded source packages are in
	‘/tmp/RtmpZz5q9A/downloaded_packages’
Warning messages:
1: In install.packages("devtools") :
  installation of package ‘ragg’ had non-zero exit status
2: In install.packages("devtools") :
  installation of package ‘pkgdown’ had non-zero exit status
3: In install.packages("devtools") :
  installation of package ‘devtools’ had non-zero exit status
```
</details>
エラー文の中で注目すべき部分は、以下の[ANTICONF]部分です。
各OSごとにインストールすべきソフトウェアを示してくれています。指示に従ってインストールしましょう。

```shell
--------------------------- [ANTICONF] --------------------------------
Configuration failed because openssl was not found. Try installing:
 * deb: libssl-dev (Debian, Ubuntu, etc)
 * rpm: openssl-devel (Fedora, CentOS, RHEL)
 * csw: libssl_dev (Solaris)
 * brew: openssl (Mac OSX)
If openssl is already installed, check that 'pkg-config' is in your
PATH and PKG_CONFIG_PATH contains a openssl.pc file. If pkg-config
is unavailable you can set INCLUDE_DIR and LIB_DIR manually via:
R CMD INSTALL --configure-vars='INCLUDE_DIR=... LIB_DIR=...'
```
※上記の[ANICONF]の表示が複数出ているので、足りていないソフトウェアをすべて入れます。

実は、丁寧に解決策の案を提示してくれています。解決策の案に従って、足りていないソフトウェアをインストールしましょう。これを何度か繰り返せば問題なくインストールできました。
以下、devtoolsのインストールエラーを解決するためにインストールした時のコマンドです。
```shell
$ sudo apt install libssl-dev libfontconfig1-dev libcurl4-openssl-dev libxml2-dev
$ sudo apt install libharfbuzz-dev libfribidi-dev
$ sudo apt install libfreetype6-dev libpng-dev libtiff5-dev libjpeg-dev
```

#### ポイント2: CRANから消えたpackageをアーカイブからインストールする方法
以前までは、install.packages("~") でインストールできたのに、CRANから消えてしまっていて、インストールできない状態になってしまったpackageをインストールする方法です。以下のコードは、Yoshi NishikawaさんのHatena Blogからの引用になります。インストールしているpackage例は、pca3dです。

**Step1 目的のpackageのCRANのページを検索して探す。**
archiveされているpackageの場合、CRANのページが以下のように表示されます。
![Screenshot 2023-08-12 at 15-34-55 CRAN - Package pca3d.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3052981/dc5b15bd-969c-5cf4-91f3-94c7fe9c6484.png)
archiveのリンクをクリックすると、以下のようにarchive化されているpackageのリンク一覧が出るので、目的のバージョンのpackageのリンクを保存しておきます。
![Screenshot 2023-08-12 at 15-35-20 Index of _src_contrib_Archive_pca3d.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3052981/ad680ba1-c59b-ea40-5e28-54ca65d277f2.png)

**Step2 以下のコードをRのターミナル上で実行**
```r
# Download package tarball from CRAN archive
#ここに先ほど保存したリンクを貼ります。
> url <- "https://cran.r-project.org/src/contrib/Archive/pca3d/pca3d_0.10.2.tar.gz"
> pkgFile <- "pca3d_0.10.tar.gz"
> download.file(url = url, destfile = pkgFile)
# Install dependencies
#pca3dの場合は、以下の2つのpackageに依存していたために追加でインストールしています。
#依存関係にあるpackageがある場合は、目的のpackageのインストール時にエラー文で教えてくれます。
> install.packages(c("rgl","ellipse"))
# Install package
> install.packages(pkgs=pkgFile, type="source", repos=NULL)
# Delete package
> unlink(pkgFile)
```
ポイント2の解説は以上になります。

## Shiny Serverの操作
shiny serverがインストールされると、shiny serverは自動的に起動します。
以下に、shiny serverの基本的な制御コマンドを記載しておきます。

**起動コマンド**
```shell
$ sudo systemctl start shiny-server
```
**停止コマンド**
```shell
$ sudo systemctl stop shiny-server
```
**再起動コマンド**
```
$ sudo systemctl restart shiny-server
```
**statusコマンド**
```
$ sudo systemctl status shiny-server
```
より詳細については、以下の公式ドキュメントを参照してください。

https://docs.posit.co/shiny-server/#stopping-and-starting

## Shiny Serverの起動テスト
http://localhost:3838/ にアクセスしてみて正常に表示されたら、環境構築が成功しています。

http://localhost:3838/

環境構築に成功していると、以下のページが表示されます。
![Screenshot_2023-08-12_at_16-28-58_Welcome_to_Shiny_Server.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3052981/41d967f3-66d4-8b07-61d4-643fc445fda4.png)


## ポートの変更(行いたい人のみ)
/etc/shiny-server/shiny-server.conf の litenの番号がポート番号になっているので、目的のポート番号に変更する。
```shiny-server.conf
# Instruct Shiny Server to run applications as the user "shiny"
run_as shiny;

# Define a server that listens on port 3838
server {
  #ここがポート番号の設定となっている
  #デフォルトは、3838となっています。
  listen 3838;

  # Define a location at the base URL
  location / {

    # Host the directory of Shiny Apps stored in this directory
    site_dir /srv/shiny-server;

    # Log all Shiny output to files in this directory
    log_dir /var/log/shiny-server;

    # When a user visits the base URL rather than a particular application,
    # an index of the applications available in this directory will be shown.
    directory_index on;
  }
}
```

## 作成したWebAppを実装する。
/srv/shiny-server/ のディレクトリに作成したWebAppをプロジェクトディレクトリごと入れます。(shiny-serverディレクトリ直下にあるファイル群は、すべて消します。)
GitHub経由でプロジェクトをcloneしてくる方法が一番楽でした。
結果として、こんな感じのディレクトリ構成になっていればOKです。
```shell
.
└── srv 
    └── shiny-server 
        └── my_app
            ├── app.R
            ├── renv
            │   ├── activate.R
            │   └── settings.json
            ├── renv.lock
            └── my_app.Rproj
```
複数のAppをホストしたいなら、shiny-serverディレクトリに追加で置いておけばOKです。
以下のようなディレクトリ構成になっておればOKです。
```shell
.
└── srv 
    └──shiny-server
       ├── my_app1
       │   ├── app.R
       │   ├── renv
       │   │   ├── activate.R
       │   │   └── settings.json
       │   ├── renv.lock
       │   └── my_app2.Rproj
       ├── my_app2
       │   ├── app.R
       │   ├── renv
       │   │   ├── activate.R
       │   │   └── settings.json
       │   ├── renv.lock
       │   └── my_app2.Rproj
       │
      ・・・
```


## ローカル上でWebAppが配信出来ているかの確認を行う

http://localhost:3838/App名/ にアクセスして、正常に動作するか確認を行います。
※/srv/shiny-server/直下にアプリを展開した場合は、http://localhost:3838/ にアクセスします。

## Cloudflare Tunnelの作成
Zero Trust > Access > Tunnels にアクセスし、「create a tunnel」をクリックして新しいトンネルを作成する。

**Step1 トンネル名を決める**
![Screenshot 2023-08-12 at 18-17-26 Cloudflare Zero Trust.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3052981/757464cf-a0d2-d9b8-e1f1-986ab526eec2.png)

**Step2 AppをホストするPCに指定のコマンドを張り付ける**
DebianとPCのアーキテクチャを選択して、画像下方の左側のコマンドをコピーし、ホストするPCのターミナルに張り付けます。
![画像0.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3052981/db4b84e1-e80c-23b8-618d-8f913849bcee.png)

**Step3 ドメインの指定**
使用するドメインとサブドメインを決めます。そして、Serviceの部分を http://localhost:3838 に設定します。(ポート番号を変更した人は、適宜変更してください。)
![画像2.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3052981/5b483d1b-35bc-97f2-c184-8cbc1d20128c.png)


**Step4 完成**
![画像1.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3052981/502c417b-2d90-2f26-7a40-d09631f64f93.png)


## WebAppが配信出来ているかの確認を行う
先程、cloudflareで設定したURLにアクセスして正常に表示されているかの確認を行います。
URLは、設定したサブドメイン名.ドメイン名/App名/ でアクセスできます。
ex) shiny-app.example.com/my_app/

## 完成
ここまで記事を読んでいただきありがとうございます。
これですべての作業が終了しました。皆さんの快適なRライフの一助となれたらうれしいです。
では、また他の記事でお会いしましょう。
バイバイ (@^^)/~~~

### 参考文献
https://posit.co/

https://www.cloudflare.com/ja-jp/

https://qiita.com/masaki_kubota/items/bc7b65e9d511d55a2a24

https://yoshi-nishikawa.hatenablog.com/entry/2017/10/27/143615

https://cran.r-project.org/web/packages/pca3d/index.html

https://docs.posit.co/shiny-server/#stopping-and-starting