Gre*_*reg 7 error-handling r try-catch
我想要做的是从本地目录加载数据文件.如果不存在,则从网络服务器下载.目前我正在使用嵌套的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")
})
})
Run Code Online (Sandbox Code Playgroud)