Description Usage Arguments Details Value Functions Examples
A check function test something about its first (required) parameter and returns either an empty string if the check succeeds or a non-empty string if the check fails, even when a test fails due to an error. The check functions described here test file-system related properties.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | checkIsPath(path)
checkIsNotPath(path)
checkIsFile(path, path.info = NULL)
checkIsNotFile(path, path.info = NULL)
checkIsDir(path, path.info = NULL)
checkIsNotDir(path, path.info = NULL)
checkIsLink(path, okBadLink = FALSE)
checkIsNotLink(path)
|
path |
A file system path to check. |
path.info |
The data frame returned by the call |
okBadLink |
By default this is |
Since file systems change asynchronously with respect to running R code, success or failure of a test does not guarantee that even the next line of code will work if that code depends on the previously checked file system state. This is called a race condition. However, checking up front will in practice be highly correlated with the state of the file system for some reasonable time frame, and it is simpler to handle errors caught early when there is a lot less code in progress that needs to be unwound. As a pre-flight check, these are very useful. Just don't count on the results once the plane is in the air.
All functions return either an empty string if the check succeeds or a non-empty string if the check fails (including if checking causes an error.)
checkIsPath
: Checks if the path exists on the file system.
This is a simple wrapper for file.exists
. Will follow links
with the result based on the final target. Returns one of:
"" (empty string)
Check succeeded as path
exists on the file system.
"No such path."
Check failed as path
was not found on the file system.
"Checking for a path failed with the following error: ..."
Check failed unexpectedly with an error.
checkIsNotPath
: Checks that the path does not exist on the file
system. This is a simple wrapper for file.exists
. Will
follow links with the result based on the final target.
"" (empty string)
Check succeeded as no such path
exists on the file system.
"No such path."
Check failed as path
was found on the file system.
"Checking for a path failed with the following error: ..."
Check failed unexpectedly with an error.
checkIsFile
: Checks that the path exists and that it is a file
or a link to a file. If the path fails to exist, is a directory, or is a
link to a directory, this fails. Will follow links with the result based on
the final target.
"" (empty string)
Check succeeded as path
exists on the file system and is a file
or a link to a file (not a directory).
"No such path."
Check failed as path
was not found on the file system.
"Not a file."
Check failed as path
was found on the file system but was
neither a file nor a link to file. It probably was a directory.
"Checking for a file failed with the following error: ..."
Check failed unexpectedly with an error.
checkIsNotFile
: Checks that the path exists and if so ensures it is
neither a file nor a link to a file. If the path fails to exist, is a file,
or is a link to a file, this fails. Will follow links with the result based
on the final target.
"" (empty string)
Check succeeded as path
exists on the file system but was
neither a file nor a link to a file (it was probably a directory).
"No such path."
Check failed as path
was not found on the file system.
"Is a file."
Check failed as path
was found on the file system and was either
a file or a link to file.
"Checking for a file failed with the following error: ..."
Check failed unexpectedly with an error.
checkIsDir
: Checks that the path exists and that it is a
directory or a link to a directory If the path fails to exist, is a file,
or is a link to a file, this fails. Will follow links with the result based
on the final target.
"" (empty string)
Check succeeded as path
exists on the file system and was either
a directory or a link to a directory.
"No such path."
Check failed as path
was not found on the file system.
"Not a directory."
Check failed as path
was found on the file system but was
neither a directory nor a link to directory. (it was probably a file).
"Checking for a directory failed with the following error: ..."
Check failed unexpectedly with an error.
checkIsNotDir
: Checks that the path exists and if so ensures it is
neither a directory nor a link to a directory If the path fails to exist,
is a file, or is a link to a file, this fails. Will follow links with the
result based on the final target.
"" (empty string)
Check succeeded as path
exists on the file system but was neither
a directory nor a link to a directory. It was probably a file.
"No such path."
Check failed as path
was not found on the file system.
"Is a directory."
Check failed as path
was found on the file system but was
either a directory or a link to directory.
"Checking for a directory failed with the following error: ..."
Check failed unexpectedly with an error.
checkIsLink
: Checks a path to see if it is a symlink (and
hence exists). By default, also follows link to ensure it points to a real
file or directory, unless okBadLink
is set TRUE
.
"" (empty string)
Check succeeded as path
exists on the file system and was a
link. If okBadLink is FALSE, it must also point, eventually, to
a real file or directory.
"No such path."
Check failed as the path
was not found on the file system.
"Not a link."
Check failed as path
was found on the file system but was
not a link. (It is probably a real file or directory.)
"Bad link."
Check failed as path
was found on the file system and it was
a link, but okBadLink = FALSE
, and the link (eventually) ends
with a link to nowhere.
"Unspecified error occurred checking if existing path was a link."
A weird condition occurred where the path exists but the check for a
link with Sys.readlink
returned NA
.
"Checking for a link failed with the following error: ..."
Check failed unexpectedly with an error.
checkIsNotLink
: Ensures that the path exists and is a real file or
directory. If it is a symbolic link, this fails. The status of a link
target is not checked, as links with or without targets fail.
"" (empty string)
Check succeeded as path
exists on the file system and is a real
file or directory (not a link).
"No such path."
Check failed as the path
was not found on the file system.
"Not a link."
Check failed as path
was found on the file system but it was
a symbolic link.
"Unspecified error occurred checking if existing path was a link."
A weird condition occurred where the path exists but the check for a
link with Sys.readlink
returned NA
.
"Checking for a link failed with the following error: ..."
Check failed unexpectedly with an error.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 | # Create file system objects for examples.
emptyFile <- tempfile()
emptyDir <- tempfile()
noSuchFile <- tempfile()
file.create( emptyFile )
dir.create( emptyDir )
linkToEmptyFile <- tempfile()
linkToEmptyDir <- tempfile()
linkToNowhere <- tempfile()
linkToLinkToEmptyFile <- tempfile()
linkToLinkToNowhere <- tempfile()
okLink <- file.symlink( emptyFile, linkToEmptyFile ) &&
file.symlink( emptyDir, linkToEmptyDir ) &&
file.symlink( noSuchFile, linkToNowhere ) &&
file.symlink( linkToEmptyFile, linkToLinkToEmptyFile ) &&
file.symlink( linkToNowhere, linkToLinkToNowhere )
checkIsPath( emptyFile )
#=> [1] ""
checkIsPath( emptyDir )
#=> [1] ""
checkIsPath( noSuchFile )
#=> [1] "No such path."
checkIsPath( NA ) # file.exists(NA) is an error
#=> [1] "Checking for a path failed with the following error:
#=> invalid 'file' argument"
if (okLink) {
checkIsPath( linkToEmptyFile )
#=> [1] ""
checkIsPath( linkToEmptyDir )
#=> [1] ""
checkIsPath( linkToNowhere )
#=> [1] "No such path."
checkIsPath( linkToLinkToEmptyFile )
#=> [1] ""
checkIsPath( linkToLinkToNowhere )
#=> [1] "No such path."
}
checkIsNotPath( emptyFile )
#=> [1] "Path exists."
checkIsNotPath( emptyDir )
#=> [1] "Path exists."
checkIsNotPath( noSuchFile )
#=> [1] ""
checkIsNotPath( NA ) # file.exists(NA) is an error
#=> [1] "Checking for a path failed with the following error:
#=> invalid 'file' argument"
if (okLink) {
checkIsNotPath( linkToEmptyFile )
#=> [1] "Path exists."
checkIsNotPath( linkToEmptyDir )
#=> [1] "Path exists."
checkIsNotPath( linkToNowhere )
#=> [1] ""
checkIsNotPath( linkToLinkToEmptyFile )
#=> [1] "Path exists."
checkIsNotPath( linkToLinkToNowhere )
#=> [1] ""
}
checkIsFile( emptyFile )
#=> [1] ""
checkIsFile( emptyDir )
#=> [1] "Not a file."
checkIsFile( noSuchFile )
#=> [1] "No such path."
checkIsFile( NA ) # file.exists(NA) is an error
#=> [1] "Checking for a file failed with the following error:
#=> invalid 'file' argument"
checkIsFile( emptyFile, path.info= file.info( emptyFile ))
#=> [1] ""
checkIsFile( emptyFile, path.info= file.info( emptyDir )) # Oops.
#=> [1] "Not a file." # Not true!
if (okLink) {
checkIsFile( linkToEmptyFile )
#=> [1] ""
checkIsFile( linkToEmptyDir )
#=> [1] "Not a file."
checkIsFile( linkToNowhere )
#=> [1] "No such path."
checkIsFile( linkToLinkToEmptyFile )
#=> [1] ""
checkIsFile( linkToLinkToNowhere )
#=> [1] "No such path."
}
checkIsNotFile( emptyFile )
#=> [1] "Is a file."
checkIsNotFile( emptyDir )
#=> [1] ""
checkIsNotFile( noSuchFile )
#=> [1] "No such path."
checkIsNotFile( NA ) # file.exists(NA) is an error
#=> [1] "Checking for a file failed with the following error:
#=> invalid 'file' argument"
checkIsNotFile( emptyFile, path.info= file.info( emptyFile ))
#=> [1] "Is a file."
checkIsNotFile( emptyFile, path.info= file.info( emptyDir )) # Oops.
#=> [1] "" # Not true!
if (okLink) {
checkIsNotFile( linkToEmptyFile )
#=> [1] "Is a file."
checkIsNotFile( linkToEmptyDir )
#=> [1] ""
checkIsNotFile( linkToNowhere )
#=> [1] "No such path."
checkIsNotFile( linkToLinkToEmptyFile )
#=> [1] "Is a file."
checkIsNotFile( linkToLinkToNowhere )
#=> [1] "No such path."
}
checkIsDir( emptyFile )
#=> [1] "Not a directory."
checkIsDir( emptyDir )
#=> [1] ""
checkIsDir( noSuchFile )
#=> [1] "No such path."
checkIsDir( NA ) # file.exists(NA) is an error
#=> [1] "Checking for a directory failed with the following error:
#=> invalid 'file' argument"
checkIsDir( emptyFile, path.info= file.info( emptyFile ))
#=> [1] "Not a directory."
checkIsDir( emptyFile, path.info= file.info( emptyDir )) # Oops.
#=> [1] "" # Not true!
if (okLink) {
checkIsDir( linkToEmptyFile )
#=> [1] "Not a directory."
checkIsDir( linkToEmptyDir )
#=> [1] ""
checkIsDir( linkToNowhere )
#=> [1] "No such path."
checkIsDir( linkToLinkToEmptyFile )
#=> [1] "Not a directory."
checkIsDir( linkToLinkToNowhere )
#=> [1] "No such path."
}
checkIsNotDir( emptyFile )
#=> [1] ""
checkIsNotDir( emptyDir )
#=> [1] "Is a directory."
checkIsNotDir( noSuchFile )
#=> [1] "No such path."
checkIsNotDir( NA ) # file.exists(NA) is an error
#=> [1] "Checking for a directory failed with the following error:
#=> invalid 'file' argument"
checkIsNotDir( emptyFile, path.info= file.info( emptyFile ))
#=> [1] ""
checkIsNotDir( emptyFile, path.info= file.info( emptyDir )) # Oops.
#=> [1] "Is a directory." # Not true!
if (okLink) {
checkIsNotDir( linkToEmptyFile )
#=> [1] ""
checkIsNotDir( linkToEmptyDir )
#=> [1] "Is a directory."
checkIsNotDir( linkToNowhere )
#=> [1] "No such path."
checkIsNotDir( linkToLinkToEmptyFile )
#=> [1] ""
checkIsNotDir( linkToLinkToNowhere )
#=> [1] "No such path."
}
checkIsLink( emptyFile )
#=> [1] "Not a link"
checkIsLink( emptyDir )
#=> [1] "Not a link"
checkIsLink( noSuchFile )
#=> [1] "No such path."
checkIsLink( NA ) # file.exists(NA) is an error
#=> [1] "Checking for a link failed with the following error:
#=> invalid 'file' argument"
if (okLink) {
checkIsLink( linkToEmptyFile )
#=> [1] ""
checkIsLink( linkToEmptyDir )
#=> [1] ""
checkIsLink( linkToNowhere )
#=> [1] "Bad link."
checkIsLink( linkToNowhere, okBadLink= TRUE )
#=> [1] ""
checkIsLink( linkToLinkToEmptyFile )
#=> [1] ""
checkIsLink( linkToLinkToNowhere )
#=> [1] "Bad link."
checkIsLink( linkToLinkToNowhere, okBadLink= TRUE )
#=> [1] ""
}
checkIsNotLink( emptyFile )
#=> [1] ""
checkIsNotLink( emptyDir )
#=> [1] ""
checkIsNotLink( noSuchFile )
#=> [1] "No such path."
checkIsNotLink( NA ) # file.exists(NA) is an error
#=> [1] "Checking for a link failed with the following error:
#=> invalid 'file' argument"
if (okLink) {
checkIsNotLink( linkToEmptyFile )
#=> [1] "Is a link."
checkIsNotLink( linkToEmptyDir )
#=> [1] "Is a link."
checkIsNotLink( linkToNowhere )
#=> [1] "No such path"
checkIsNotLink( linkToLinkToEmptyFile )
#=> [1] "Is a link."
checkIsNotLink( linkToLinkToNowhere )
#=> [1] "No such path."
}
# cleanup
unlink( emptyFile )
unlink ( emptyDir, recursive= TRUE )
unlink( linkToEmptyFile )
unlink( linkToEmptyDir )
unlink( linkToNowhere )
unlink( linkToLinkToEmptyFile )
unlink( linkToLinkToNowhere )
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.