5
我想要做的是從本地目錄加載數據文件。如果它不存在,請從網絡服務器下載。目前我正在使用嵌套tryCatch,它似乎工作。這是嘗試在R中完成此任務的正確方法嗎?使用tryCatch在R中加載數據文件
tryCatch(
{
#attempt to read file from current directory
# use assign so you can access the variable outside of the function
assign("installations", read.csv('data.csv'), envir=.GlobalEnv)
print("Loaded installation data from local storage")
},
warning = function(w)
{
print()# dummy warning function to suppress the output of warnings
},
error = function(err)
{
print("Could not read data from current directory, attempting download...")
#attempt to read from website
tryCatch(
{
# use assign so you can access the variable outside of the function
assign("installations", read.csv('http://somewhere/data.csv'), envir=.GlobalEnv)
print("Loaded installation data from website")
},
warning = function(w)
{
print()# dummy warning function to suppress the output of warnings
},
error = function(err)
{
print("Could not load training data from website!! Exiting Program")
})
})