本文主要解决林子雨老师“大数据案例-步骤四:利用R进行数据可视化分析”中我自己安装所出现的问题,本文是我在参考其他csdn大佬的解决办法还有网上的一些解决办法结合自己出现的问题进行解决的!!!
林子雨老师网址:https://dblab.xmu.edu.cn/blog/1130/
1.R的安装
像老师说的那样ubuntu自带的apt安装的R版本太低(版本太低会导致一些依赖无法安装)
所以我们需要换源!
请登录Linux系统,打开一个终端,然后执行下面命令(并注意保持网络连通,可以访问互联网,因为安装过程要下载各种安装文件):
如果你之前安装过R或者安装的版本太低了,需要在liunx终端来卸载R残留:
sudo apt remove r-base*
sudo apt autoremove
sudo apt update && sudo apt upgrade
为了防止出现意外需要备份源文件:
cd /etc/apt/
sudo cp sources.list sources.list-copy
安装方法一和二就是使用了不同的源,本质上没有多大区别 !!!
1.1安装新版R的方法一:
利用gedit打开/etc/apt/sources.list文件
sudo gedit /etc/apt/sources.list
删除里面所有内容
添加如下内容(我修改为阿里云的ubuntu镜像源,根据ubuntu版本的不同具体填什么内容点击此处查看):
deb https://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb https://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb https://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
# deb https://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
# deb-src https://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb https://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
添加完以上内容后,在以上内容的最后一行在添加以下内容,这个是清华大学的源,要是其他版本的ubuntu可以参考这个文档进行选择(以下内容只适用于ubuntu18.04版本!!!)
deb https://mirrors.tuna.tsinghua.edu.cn/CRAN/bin/linux/ubuntu bionic-cran40/
保存退出(点击Esc——>输入:wq)
更新源列表:
sudo apt update && sudo apt upgrade
如果更新软件源出现由于没有公钥,无法验证下列签名的错误,请执行如下命令: sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 51716619E084DAB9
执行安装
sudo apt install r-base
终端直接打开R
R
# 在R命令行下查看版本
version
版本信息如下:
如果换源成功安装的R是4.x的版本,如果换源失败R的版本还是3.X,用方法二
1.2安装新版R方法二:
如果你此时安装的版本还是3.2并且以上操作都做过了那么利用gedit打开/etc/apt/sources.list文件:
sudo gedit /etc/apt/sources.list
注释掉清华的源:
deb https://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb https://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb https://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
# deb https://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
# deb-src https://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb https://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
#deb https://mirrors.tuna.tsinghua.edu.cn/CRAN/bin/linux/ubuntu bionic-cran40/
,用以下命令在liunx终端输入:
# update indices
sudo apt update -qq
# install two helper packages we need
sudo apt install --no-install-recommends software-properties-common dirmngr
# add the signing key (by Michael Rutter) for these repos
# To verify key, run gpg --show-keys /etc/apt/trusted.gpg.d/cran_ubuntu_key.asc
# Fingerprint: E298A3A825C0D65DFD57CBB651716619E084DAB9
wget -qO- https://cloud.r-project.org/bin/linux/ubuntu/marutter_pubkey.asc | sudo tee -a /etc/apt/trusted.gpg.d/cran_ubuntu_key.asc
# add the R 4.0 repo from CRAN -- adjust 'focal' to 'groovy' or 'bionic' as needed
sudo add-apt-repository "deb https://cloud.r-project.org/bin/linux/ubuntu $(lsb_release -cs)-cran40/"
执行安装:
sudo apt install --no-install-recommends r-base
R会把当前目录当做R的工作目录,经量在当前用户下打开R,如果在非当前用户打开R,当要保存退出时就会报错,也就是说如果你的用户为hadoop用户,但是你在root目录下打开了R,退出时就会报错。
安装完之后将注释的清华的源取消注释就是用sudo gedit /etc/apt/sources.list打开文档将文档的内容修改为:
deb https://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb https://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb https://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
# deb https://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
# deb-src https://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb https://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/CRAN/bin/linux/ubuntu bionic-cran40/
最后在查看R的版本
1.3R的安装出现错误
如果你在使用“sudo apt-get install r-base”出现如下错误:
正在读取软件包列表... 完成
正在分析软件包的依赖关系树
正在读取状态信息... 完成
有一些软件包无法被安装。如果您用的是 unstable 发行版,
这也许是因为系统无法达到您要求的状态造成的。
该版本中可能会有一些您需要的软件
包尚未被创建或是它们已被从新到(Incoming)目录移出。
下列信息可能会对解决问题有所帮助:
下列软件包有未满足的依赖关系: r-base : 依赖: r-recommended (= 3.4.4-1ubuntu1) 但是它将不会被安装
E: 无法修正错误,因为您要求某些软件包保持现状,就是它们破坏了软件包间的依赖关系。
那就是你的源没有弄好,修改的源文件“sudo gedit /etc/apt/sources.list”
2.安装ggplot2
2.1安装
使用这条,先安装ggplot2
install.packages("ggplot2",type="source",repos=c("http://yihui.name/xran","http://cran.rstudio.com"))
如果不行选择执行这条:
install.packages("ggplot2",dependencies=TRUE)
安装成功最后应该是这样的:
我只截取了最后的输出日志,并且我的输出日志里不含有“ERROR”和"ANTICONF"报错
2.2出现错误
2.2.1如果你的安装输出日志里包含“ERROR”:
如果出现以下‘ERROR’:
ERROR: dependency ‘xml2’ is not available for package ‘roxygen2’
你需要安装libxml2
和libxml2-dev
。在终端中输入以下命令:
sudo apt-get install libxml2
sudo apt-get install libxml2-dev
如果出现以下‘ERROR’:
ERROR: dependency ‘gert’ is not available for package ‘usethis’
那么首先,你需要安装gert
包。在R环境中输入以下命令:
install.packages("gert")
如果没有解决,你可以尝试使用conda
来安装gert
包。在终端中输入以下命令:
conda install r-gert
如果出现以下“ERROR”:
ERROR: dependencies ‘systemfonts’, ‘textshaping’ are not available for package ‘ragg’
那么你需要在liunx终端输入以下命令:
sudo apt-get install libfontconfig1-dev
sudo apt install libharfbuzz-dev libfribidi-dev
然后你需要安装systemfonts
和textshaping
包。在R环境中输入以下命令:
install.packages("systemfonts")
install.packages("textshaping")
如果出现如下错误:
ERROR: dependencies ‘ragg’, ‘xml2’ are not available for package ‘pkgdown’
那么你可能需要安装libfontconfig1-dev
和libxml2-dev
。在终端中输入以下命令:
sudo apt-get install libfontconfig1-dev
sudo apt-get install libxml2-dev
然后你需要安装ragg
和xml2
包。在R环境中输入以下命令:
install.packages("ragg")
install.packages("xml2")
如果出现如下错误:
ERROR: configuration failed for package ‘sf’
首先,你需要安装libudunits2-dev
,libgdal-dev
和libgeos-dev
。在终端中输入以下命令:
sudo apt-get install libudunits2-dev libgdal-dev libgeos-dev
然后,你可以尝试再次在R环境中安装sf
包:
options(repos = c(CRAN = "https://mirrors.tuna.tsinghua.edu.cn/CRAN/"))
install.packages("sf", dependencies = TRUE)
果上述步骤不能解决问题,你可以尝试使用conda
来安装sf
包。在liunx终端中输入以下命令:
conda install -c conda-forge r-sf
2.2.2如果出现以下错误(以下问题为一类问题):
出现Error in download.file(url, destfile, method, mode = "wb", ...) :
download from 'https://cloud.r-project.org/src/contrib/ragg_1.2.7.tar.gz' failed
此外: Warning messages:
1: In download.file(url, destfile, method, mode = "wb", ...) :
downloaded length 0 != reported length 0
2: In download.file(url, destfile, method, mode = "wb", ...) :
URL 'https://cloud.r-project.org/src/contrib/ragg_1.2.7.tar.gz': Timeout of 60 seconds was reached
Warning in download.packages(pkgs, destdir = tmpd, available = available, :
下载程序包‘ragg’时出了问题
解决办法:
在R环境中输入以下命令
options(repos = c(CRAN = "https://mirrors.tuna.tsinghua.edu.cn/CRAN/"))
install.packages("ragg")
问题:
Error in download.file(url, destfile, method, mode = "wb", ...) :
download from 'https://cloud.r-project.org/src/contrib/devtools_2.4.5.tar.gz' failed
此外: Warning messages:
1: In download.file(url, destfile, method, mode = "wb", ...) :
downloaded length 0 != reported length 0
2: In download.file(url, destfile, method, mode = "wb", ...) :
URL 'https://cloud.r-project.org/src/contrib/devtools_2.4.5.tar.gz': Timeout of 60 seconds was reached
Warning in download.packages(pkgs, destdir = tmpd, available = available, :
下载程序包‘devtools’时出了问题
解决:
在R环境中输入以下命令:
options(repos = c(CRAN = "https://mirrors.tuna.tsinghua.edu.cn/CRAN/"))
install.packages("devtools")
2.2.3如果出现“ANTICONF”
如果你的输出日志出现了:
-------------------------------------【ANTICONF】----------------------------------------------
可以查看这个大佬博客!!!原理就是deb里面含有什么就在liunx终端安装什么,注意我们的系统是ubuntu18.04和大佬的ubuntu16.04在一些方面存在差异,所以不要直接复制粘贴,要看自己的日志输出里面缺少什么,就安装什么
3.安装devtools
3.1安装
在R环境输入:
install.packages("devtools",type="source",repos=c("http://yihui.name/xran","http://cran.rstudio.com"))
安装成功应该是这样:
我只截取了输出日志的最后一部分,并且所有输出日志中不含有“ERROR”和“ANTICONF”错误才算成功。
3.2错误:
出现如下错误:
configuration failed for package ‘ragg’
解决办法:单独安装“ragg”
options(repos = c(CRAN = "https://mirrors.tuna.tsinghua.edu.cn/CRAN/"))
install.packages("ragg")
出现如下报错:
ERROR: dependency ‘pkgdown’ is not available for package ‘devtools’
解决办法:单独安装“pkgdown”
options(repos = c(CRAN = "https://mirrors.tuna.tsinghua.edu.cn/CRAN/"))
install.packages("pkgdown")
出现如下错误:
Warning: 无法在貯藏處http://cran.rstudio.com/src/contrib中读写索引:
download from 'http://cran.rstudio.com/src/contrib/PACKAGES' failed
Warning message:
package ‘devtools’ is not available for this version of R
A version of this package for your version of R might be available elsewhere,
see the ideas at
https://cran.r-project.org/doc/manuals/r-patched/R-admin.html#Installing-packages
解决办法:
options(repos = c(CRAN = "https://mirrors.tuna.tsinghua.edu.cn/CRAN/"))
install.packages("devtools")
如果还出现其他错误,解决办法跟安装ggplot2的一样!!!需要注意的是学习这个大佬的解决问题的办法而不是直接复制粘贴,因为这个大佬用的是ubuntu16.04而不是ubuntu18.04在一些方面存在差异!!!
4.安装RMySQL
4.1安装:
在R环境输入:
install.packages("RMySQL",dependencies=TRUE)
安装成功应该是这样的:
我只放入了最后成功的一部分输出日志,并且全部日志中不包含“ERROR”和“ANTICONF”错误
4.2错误:
你在尝试安装 RMySQL 包时遇到了问题,错误信息显示 /usr/bin/ld: 找不到 -lssl 和 /usr/bin/ld: 找不到 -lcrypto。这表明你的系统缺少 libssl 和 libcrypto 这两个库。
你可以通过以下命令来安装这两个库:
sudo apt-get install libssl-dev
sudo apt-get install libcrypto++-dev
错误:/usr/lib/R/bin/R: 行 142: /usr/lib/R/etc/ldpaths: 没有那个文件或目录 /usr/lib/R/bin/R: 行 238: /usr/lib/R/etc/ldpaths: 没有那个文件或目录
打开出现错误的文件路径查看文件是否存在:
情况一:我们发现文件是存在的,在终端输入ll查看 /usr/lib/R/etc/目录文件的详细信息,发现是连接文件,指向/etc/R下的文件,当我打开/etc/R目录时发现确实不存在这些文件。
情况二:/usr/lib/R/etc/不存在连接文件,但是/etc/R存在被指向的文件
情况三:两个路径中都不存在所要求文件
所有情况解决办法:在其他ubuntu18.04系统安装R 将/etc/R中缺少的文件导入到原来的ubuntu18.04系统。
5.安装taiyun/recharts
因为我不会使用虚拟机访问github网站,所以我选择用离线下载的方式安装包在这里。
或者你用自己可以访问的方式访问下载:https://github.com/taiyun/recharts
将下载的文件放入到你的ubuntu系统中(我放入到/home/hadoop/Downloads中)解压:
cd /home/hadoop/Downloads
unzip recharts-master.zip -d /home/hadoop/R
在R环境输入:
install.packages("/home/hadoop/R/recharts-master", repos = NULL, type = "source")
“ /home/hadoop/R/recharts-master”是你自己解压的路径
会出现如下报错:
ERROR: dependency ‘webshot’ is not available for package ‘recharts’
* removing ‘/home/hadoop/R/x86_64-pc-linux-gnu-library/4.3/recharts’
Warning message:
In install.packages("/home/hadoop/R/recharts-master", repos = NULL, :
安装程序包‘/home/hadoop/R/recharts-master’时退出狀態的值不是0
需要在R环境中安装webshot:
install.packages("webshot")
在R环境中执行:
install.packages("/home/hadoop/R/recharts-master", repos = NULL, type = "source")
就成功了!!!