Bene allora: benvenuti nel mondo R ;-)
Ecco qui
Impostazione del codice
urls <- c(
"http://stat.ethz.ch/R-manual/R-devel/library/base/html/connections.html",
"http://en.wikipedia.org/wiki/Xz",
"xxxxx"
)
readUrl <- function(url) {
out <- tryCatch(
{
# Just to highlight: if you want to use more than one
# R expression in the "try" part then you'll have to
# use curly brackets.
# 'tryCatch()' will return the last evaluated expression
# in case the "try" part was completed successfully
message("This is the 'try' part")
readLines(con=url, warn=FALSE)
# The return value of `readLines()` is the actual value
# that will be returned in case there is no condition
# (e.g. warning or error).
# You don't need to state the return value via `return()` as code
# in the "try" part is not wrapped insided a function (unlike that
# for the condition handlers for warnings and error below)
},
error=function(cond) {
message(paste("URL does not seem to exist:", url))
message("Here's the original error message:")
message(cond)
# Choose a return value in case of error
return(NA)
},
warning=function(cond) {
message(paste("URL caused a warning:", url))
message("Here's the original warning message:")
message(cond)
# Choose a return value in case of warning
return(NULL)
},
finally={
# NOTE:
# Here goes everything that should be executed at the end,
# regardless of success or error.
# If you want more than one expression to be executed, then you
# need to wrap them in curly brackets ({...}); otherwise you could
# just have written 'finally=<expression>'
message(paste("Processed URL:", url))
message("Some other message at the end")
}
)
return(out)
}
Applicazione del codice
> y <- lapply(urls, readUrl)
Processed URL: http://stat.ethz.ch/R-manual/R-devel/library/base/html/connections.html
Some other message at the end
Processed URL: http://en.wikipedia.org/wiki/Xz
Some other message at the end
URL does not seem to exist: xxxxx
Here's the original error message:
cannot open the connection
Processed URL: xxxxx
Some other message at the end
Warning message:
In file(con, "r") : cannot open file 'xxxxx': No such file or directory
Indagare sull'output
> head(y[[1]])
[1] "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"
[2] "<html><head><title>R: Functions to Manipulate Connections</title>"
[3] "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">"
[4] "<link rel=\"stylesheet\" type=\"text/css\" href=\"R.css\">"
[5] "</head><body>"
[6] ""
> length(y)
[1] 3
> y[[3]]
[1] NA
Osservazioni aggiuntive
prova a prendere
tryCatchrestituisce il valore associato all'esecuzione a exprmeno che non ci sia un errore o un avviso. In questo caso, è possibile specificare valori di ritorno specifici (vedere return(NA)sopra) fornendo una rispettiva funzione del gestore (vedere argomenti errore warningin ?tryCatch). Queste possono essere funzioni già esistenti, ma puoi anche definirle all'interno tryCatch()(come ho fatto sopra).
Le implicazioni della scelta di valori di ritorno specifici delle funzioni del gestore
Come abbiamo specificato che NAdovrebbe essere restituito in caso di errore, il terzo elemento yè NA. Se avessimo scelto NULLdi essere il valore di ritorno, la lunghezza di ysarebbe stata 2invece di 3come lapply()semplicemente "ignoreremo" i valori di ritorno che sono NULL. Si noti inoltre che se non si specifica un valore di ritorno esplicito tramite return(), verranno restituite le funzioni del gestore NULL(ovvero in caso di errore o condizione di avviso).
Messaggio di avviso "indesiderato"
Dato warn=FALSEche non sembra avere alcun effetto, un modo alternativo per sopprimere l'avvertimento (che in questo caso non è davvero interessante) è usare
suppressWarnings(readLines(con=url))
invece di
readLines(con=url, warn=FALSE)
Espressioni multiple
Nota che puoi anche inserire più espressioni nella "parte delle espressioni effettive" (argomento exprdi tryCatch()) se le avvolgi tra parentesi graffe (proprio come ho illustrato nella finallyparte).
pastefunzioni termina con uno spazio, perché non omettere lo spazio e ilsep=""?