问题描述
在外壳程序脚本(在Windows 10上)中运行Rscript并在Rstudio中运行它时,我得到不同的结果,我相信这是库路径问题。这是我的shell脚本。将脚本并行运行的次数作为参数,并使用nohup并行运行脚本(如Tom Kelly here的建议):
#!/bin/bash
# error checking for the commandline argument
if [ $# -lt 1 ]; then
echo "Too few arguments! Usage:"
echo "script.sh [number of times to run Rscript in parallel]"
read -p "Press [Enter] key to quit..."
elif ! [[ "$1" =~ ^[0-9]*(\.[0-9]+)?$ ]]; then
echo "$1 is not a number."
echo "Check the value and try again."
read -p "Press [Enter] key to quit..."
else
# runs the test.R script the number of times specified by the commandline argument
# using nohup (puts the process in the background)
END=$1
for ((i=1;i<=END;i++)); do
nohup Rscript test.R &
done
wait
read -p "Press [Enter] key to finish..."
fi
这是test.R.的内容。它会打印出我的库中安装的软件包数量,检查是否已安装tidyverse,并尝试不成功地更改软件包库的默认路径:
#prints the number of packages installed
print("Number of packages installed in library:")
packages <- installed.packages()
print(length(packages))
#tests whether tidyverse needs to be installed
my_packages <- c("tidyverse")
not_installed <- my_packages[!(my_packages %in% installed.packages()[,"Package"])]
print("Packages needing to be installed:")
print(not_installed)
#gives the current library path
print("Currently will install at:")
print(.libPaths())
#my attempt to change the default library path- doesn't seem to do anything
print(paste("Changing the path to: ",Sys.getenv('R_LIBS_USER')))
.libPaths(Sys.getenv('R_LIBS_USER'))
#the new library path
print("Now will install at:")
print(.libPaths())
以下是脚本的输出,首先是通过RStudio运行,然后是通过我的Shell脚本运行:
[1] "Number of packages installed in library:"
[1] 4304
[1] "Packages needing to be installed:"
character(0)
[1] "Currently will install at:"
[1] "C:/Users/*my name*/Documents/R/win-library/4.0"
[2] "C:/Program Files/R/R-4.0.2/library"
[1] "Changing the path to: C:/Users/*my name*/Documents/R/win-library/4.0"
[1] "Now will install at:"
[1] "C:/Users/*my name*/Documents/R/win-library/4.0"
[2] "C:/Program Files/R/R-4.0.2/library"
Shell脚本输出:
[1] "Number of packages installed in library:"
[1] 1920
[1] "Packages needing to be installed:"
[1] "tidyverse"
[1] "Currently will install at:"
[1] "C:/Program Files/R/R-4.0.2/library"
[1] "Changing the path to: C:\\Users\\*my name*/R/win-library/4.0"
[1] "Now will install at:"
[1] "C:/Program Files/R/R-4.0.2/library"
如所示,调用脚本的两种方式之间的路径以及在每个路径上安装的软件包数均不同。如何确保通过外壳脚本运行脚本时,它使用与在Rstudio中运行脚本时相同的默认路径?我的目标是使它成为可以从任何人的计算机上运行的脚本,该脚本可以下载需要自动运行的软件包。
我们将不胜感激!
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)