Hi, I have been trying to get the sub function to work but can't for the life of me figure it out. I tried looking at ?sub but I think there is something that I am just not understanding. Example failed attempts> sub("\\","/","G:\Compensation Audits_Reports\Step Audit\Steps off Step.accdb")Error: '\C' is an unrecognized escape in character string starting "G:\C"> sub("\\","/","G:\Compensation Audits_Reports\Step Audit\Steps off Step.accdb",fixed=TRUE)Error: '\C' is an unrecognized escape in character string starting "G:\C"> sub("\\","/","G:\Compensation Audits_Reports\Step Audit\Steps off Step.accdb",fixed=FALSE)Error: '\C' is an unrecognized escape in character string starting "G:\C")> sub("/","\","G:\Compensation Audits_Reports\Step Audit\Steps off Step.accdb")Error: unexpected symbol in "sub("/","\","G" Thanks. Dan
On 12-06-14 6:23 PM, Lopez, Dan wrote:> Hi, > > I have been trying to get the sub function to work but can't for the life of me figure it out. I tried looking at ?sub but I think there is something that I am just not understanding. > > Example failed attempts >> sub("\\","/","G:\Compensation Audits_Reports\Step Audit\Steps off Step.accdb") > Error: '\C' is an unrecognized escape in character string starting "G:\C"This isn't anything to do with sub(), it's the third string that's the problem. When you say "G:\Comp..." it thinks you've escaped the C, but there's no such escape. Duncan Murdoch> >> sub("\\","/","G:\Compensation Audits_Reports\Step Audit\Steps off Step.accdb",fixed=TRUE) > Error: '\C' is an unrecognized escape in character string starting "G:\C" > >> sub("\\","/","G:\Compensation Audits_Reports\Step Audit\Steps off Step.accdb",fixed=FALSE) > Error: '\C' is an unrecognized escape in character string starting "G:\C") > >> sub("/","\","G:\Compensation Audits_Reports\Step Audit\Steps off Step.accdb") > Error: unexpected symbol in "sub("/","\","G" > > Thanks. > Dan > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
Possibly overkill, I use a Windows batch file that calls a perl script.
Both the bat and pl files need to be in the executable path. Open a
command prompt, type `pwd ' and then drag the file or folder from the
Windows Explorer to the cmd. The script will return the file with
backslashes converted to forward slashes. Then I copy and paste from the
cmd window into my R script.
c:\>more c:\bin\batch\pwd.BAT
@echo off
c:\bin\batch\pwd.pl %1
c:\>more c:\bin\batch\pwd.pl
#! /usr/bin/perl -w
# prints pwd with forward slashes for R
use File::Basename;
#print $#ARGV;
if ($#ARGV == -1) {
$pwd = `cd`;
chomp $pwd;
}
else {
$pwd = $ARGV[0];
}
$pwd =~ s|\\|\/|g;
print "$pwd";
-P.
**************************************************************
Philip M. Hurvitz, PhD | Research Assistant Professor | UW-CBE
Urban Form Lab | 1107 NE 45th Street, Suite 535 | Box 354802
University of Washington, Seattle, Washington 98195-4802, USA
phurvitz at u.washington.edu | http://gis.washington.edu/phurvitz
"What is essential is invisible to the eye." -de Saint-Exup?ry