Closed dylanbeaudette closed 6 years ago
I had to deal with this once before for a script that generates grouped profile plots for all MUs in the survey. Incidentally, people were using illegal characters e.g. "1031?" in MUSYM field.
Something like this would do the trick. I wanted to make it clear that illegal characters were being used, and also distinguish those from things actually called e.g. "1031", so I made the replacement something other than ""
. For a file name foo
:
gsub(foo, pattern='([/\\|<>:\\*?\"])', replacement="BadChar")
RE: cd599ce
When you wrap the writeOGR/CSV calls in try, but don't catch exceptions, you may not get intended effect. While I've never had the problem raised here in #75 for MUSum report, I do know that I've had issues related to concurrent modification.
If you have the shapefile open in an Arc session, will the script notify you that it was unable to write to file? (did not test this at the time, just a question)
EDIT: quick test. is try() having the intended effect here?
> badname <- "yourfilename?.csv"
> goodname <- "yourfilename.csv"
> yourdata <- c(2,2,2)
Filename with illegal character returns error upon writing:
> write.csv(yourdata,file=badname)
Error in file(file, ifelse(append, "a", "w")) :
cannot open the connection
In addition: Warning message:
In file(file, ifelse(append, "a", "w")) :
cannot open file 'yourfilename?.csv': Invalid argument
Without illegal character, it works:
> write.csv(yourdata,file=goodname)
Also works wrapped with try():
> try(write.csv(yourdata,file=goodname))
Filename with illegal character DOESN'T work, even when wrapped with try.
> try(write.csv(yourdata,file=badname))
Error in file(file, ifelse(append, "a", "w")) :
cannot open the connection
In addition: Warning message:
In file(file, ifelse(append, "a", "w")) :
cannot open file 'yourfilename?.csv': Invalid argument
Now, for concurrent modification:
> getwd()
[1] "S:/NRCS/Archive_Andrew_Brown/Scripts"
[OPENED FILE S:/NRCS/Archive_Andrew_Brown/Scripts/yourfilename.csv in EXCEL]
When file is open in another program, error occurs even without illegal character. Even when statement is wrapped in try()
.
> try(write.csv(yourdata,file=goodname))
Error in file(file, ifelse(append, "a", "w")) :
cannot open the connection
In addition: Warning message:
In file(file, ifelse(append, "a", "w")) :
cannot open file 'yourfilename.csv': Permission denied
Looking at some of my code from the same case I mentioned in the previous comment, I did something like this.
if(typeof(try( { expression } )) == "try-error")
#say something informative
Thanks for the testing and examples. I think that the solution might be a combination of documentation and sanity checks that prevent the report from stopping before completion. Over the last couple of years, that has been one of the major sources of confusion: "why didn't the report finish?".
Like this:
My thoughts on that:
also, perhaps a check at the head of the report to ensure that the file names and paths are all valid (before trying to sample rasters etc etc.) might be worthwhile. it's sad to wait for a report to run, only to have I/O fail right as it finishes all the hard work.
I like that idea: check at the beginning for things that will stop the report from running 80% of the way through.
d98f41d
This can happen when applying the region2/mu-comparison report to other data such as LRU concepts that have more expressive labels.
TODO: