Re: [R] File > Save As...

From: Gene Leynes <gleynes+r_at_gmail.com>
Date: Wed, 13 Apr 2011 17:05:40 -0500

That's very interesting. It's not what I was thinking about or expecting, but I'm glad to know about it (and it will probably be useful at some point).

Initially, I was asking for a method to find out the name of the current script. I mean, the current script that you're editing / running.

However, this is useful. Just by putting a few lines of code, you can change the working directory with a simple drag and drop. Put the following lines into a script, and drop it into the R GUI.

    curpath = dirname(getSrcFilename(function(){}, full=TRUE))     setwd(curpath)
    cat('Now in ', curpath, '\n')

What I was looking for is almost accomplished by this:

    filepath = getSrcFilename(function(){}, full=TRUE)     setwd(dirname(filepath))
    browser()

Of course, you wouldn't want to stay in browser mode forever...

It would be nice to get the name of your current script with something like this.file(). Of course, I don't know how that would work if someone typed it into the console.

I'm genuinely excited to explore the new updates in 2.13. Either it's a good update, or I'm just now learning enough to appreciate the updates.

Thanks

Gene

On Wed, Apr 13, 2011 at 4:33 PM, Duncan Murdoch <murdoch.duncan_at_gmail.com>wrote:

> On 13/04/2011 5:20 PM, Gene Leynes wrote:
>
>> as of right now
>> x = function(a) print(a)
>> attr(x, "srcref")
>> returns NULL in 2.13, am I doing something wrong?
>>
>
> There's a limitation to the debug information: it can't be attached to a
> function whose body consists of a single simple expression like print(a).
> If you put braces around print(a), it will be attached to the body of x:
>
> Put this line into a file, and source it:
>
>
> x <- function(a) { print(a) }
>
> source("test.R")
> attr(body(x), "srcref")
>
> In case you're interested, the reason for this limitation is that there are
> some objects in R (NULL is the most obvious one) which can be a function
> body but which can't hold attributes. At the time the debug info is added,
> the function hasn't been created, but its body has, so the attribute has to
> go there.
>
> Duncan Murdoch
>
>
>> (also, should I post this to a new thread, or the development thread?)
>>
>> About me: I like long walks on the beach, and this is my current version
>> of R:
>>
>>> t(as.data.frame(R.Version()))

>>>
>> [,1]
>> platform "i386-pc-mingw32"
>> arch "i386"
>> os "mingw32"
>> system "i386, mingw32"
>> status "RC"
>> major "2"
>> minor "13.0"
>> year "2011"
>> month "04"
>> day "11"
>> svn.rev "55409"
>> language "R"
>> version.string "R version 2.13.0 RC (2011-04-11 r55409)"
>>
>>>
>>>
>>
>> On Wed, Mar 16, 2011 at 2:44 PM, Gene Leynes <gleynes+r_at_gmail.com
>> <mailto:gleynes%2Br_at_gmail.com>> wrote:
>>
>>
>> Thanks for showing me the link to the code / your response / your
>> work in general.
>>
>> It seems that the real magic is happening in the call to the
>> function attributes, via the line
>> attr(x, "srcref")
>> I'm guessing that attributes must be defined somewhere deep inside
>> the R machinery (since I didn't find it as a file in base)... And
>> there's probably not much benefit for me to know more beyond that.
>>
>> So, I'll be looking forward to 2.13!
>>
>>
>>
>> On Tue, Mar 15, 2011 at 3:37 PM, Duncan Murdoch
>> <murdoch.duncan_at_gmail.com <mailto:murdoch.duncan_at_gmail.com>> wrote:
>>
>> On 15/03/2011 2:56 PM, Gene Leynes wrote:
>>
>> The "getSrcFilename" function is exactly what I was trying
>> to describe, and
>> I'm excited to know that it's on it way!
>>
>> I have tried to create that type of function, but I didn't
>> think it was
>> possible with currently available functions. I would be
>> interested in
>> seeing how the new function works, maybe I'll check it out
>> using the google
>> code search
>> tool<
>> http://www.google.com/codesearch?hl=en&lr=&q=lang%3Ar+&sbtn=Search
>> <
>> http://www.google.com/codesearch?hl=en&lr=&q=lang%3Ar+&sbtn=Search
>> >>(although
>>
>> I usually have a hard time making sense of that code).
>>
>>
>> The source is available in
>>
>>
>> https://svn.r-project.org/R/trunk/src/library/utils/R/sourceutils.R
>>
>> Duncan Murdoch
>>
>>
>> Please let me briefly clarify this part:
>>
>> > But it can. If you open a script and choose save, it
>> will be saved to the
>> > same place.
>> >
>>
>> I just mean that when you do "save as..." R doesn't seem to
>> use the same
>> information that it uses during a normal save (the directory
>> or script
>> name). In other applications like Microsoft Word, or
>> Python's IDLE (screen
>> shot attached) the user is shown a dialogue box with the
>> file name in the
>> current directory of that file.
>>
>> This is a very minor annoyance though. I only brought it up
>> because I
>> thought it would be easier to explain than asking about a
>> function that
>> would do the job of "getSrcFilename", which is really what I
>> was after.
>>
>> I rarely upgrade my R versions, but this will definitely be
>> an occasion when
>> I do!
>>
>> This makes me want to go back and look at the past release
>> notes to see what
>> other goodies I've been overlooking.
>>
>> Thanks again,
>>
>> Gene
>>
>>
>>
>> On Mon, Mar 14, 2011 at 8:17 PM, Duncan
>> Murdoch<murdoch.duncan_at_gmail.com
>> <mailto:murdoch.duncan_at_gmail.com>>wrote:
>>
>> > On 11-03-14 8:12 PM, Gene Leynes wrote:
>> >
>> >> Yes, I understand. Normally I use Eclipse, which does
>> what I want for
>> >> "save as..."
>> >>
>> >> The bigger issue is that R can't tell the location of
>> an open script,
>> >> which makes it harder to create new versions of
>> existing work....
>> >>
>> >
>> > But it can. If you open a script and choose save, it
>> will be saved to the
>> > same place. Or do you mean an executing script? There
>> are indirect ways to
>> > find the name of the executing script. For example,
>> > in R-devel (to become 2.13.0 next month), you can do this:
>> >
>> >
>> > cat("This file is ", getSrcFilename(function(){},
>> full=TRUE), "\n")
>> >
>> > The getSrcFilename() function will be new in 2.13.0.
>> You can do the same
>> > in earlier versions, but you need to program it yourself.
>> >
>> > Duncan Murdoch
>> >
>> >
>> >
>> >
>> >
>> >> Say you have some great analysis going in "Research
>> 2011-01-01" with a
>> >> folder for data, code, gui, other languages, excel,
>> whatever else.
>> >>
>> >> Then you want to make a new folder for Research
>> 2011-01-02" with the
>> >> same structure.
>> >>
>> >> Making sure that you're setting the right directory in
>> the new version
>> >> adds a layer of complexity which would be nice to
>> avoid. It would be
>> >> really nice to just copy the folders, and let the
>> script detect it's own
>> >> location so that it could read from the data
>> sub-directory, the gui's
>> >> sub-directory, etc. What I want to be able to do is
>> copy the tools I
>> >> create into different client folders so that anyone can
>> just use the
>> >> tool without changing the source code.... which is
>> simple for you or I,
>> >> but prevents newcomers from using the tools.
>> >>
>> >> Apparently I'm in the minority on the next part: but
>> the "save as" thing
>> >> I was describing before seems very strange to me. As I
>> teach new people
>> >> R it's always kind of a hangup that requires a little
>> more explanation,
>> >> but maybe it's the way I'm looking at it.
>> >>
>> >> Thanks for your reply Duncan.
>> >>
>> >>
>> >> On Mon, Mar 14, 2011 at 6:57 PM, Duncan Murdoch
>> >> <murdoch.duncan_at_gmail.com
>> <mailto:murdoch.duncan_at_gmail.com><mailto:
>> murdoch.duncan_at_gmail.com
>>
>> <mailto:murdoch.duncan_at_gmail.com>>> wrote:
>> >>
>> >> On 11-03-14 5:03 PM, Gene Leynes wrote:
>> >>
>> >> As much as I love R, there are still the
>> occasional shortcomings.
>> >>
>> >> I would love to find a solution to the "save
>> as..." problem.
>> >>
>> >> Steps to reproduce the problem:
>> >>
>> >> 1. Open any version of he R GUI in Windows
>> >> 2. Choose "File> Open" from the menu
>> >> 3. Open a script that is in a different
>> directory
>> >> 4. Choose "File> Save As" from the menu
>> >>
>> >>
>> >> In just about any other application you would be
>> presented with
>> >> a GUI
>> >> showing the current folder of the script and the
>> current name of
>> >> the
>> >> script. You could then edit the name and save.
>> >>
>> >> In R, you have to find the directory of the
>> script and the
>> >> script name. It
>> >> can be annoying from a GUI perspective,
>> especially if you
>> >> typically work in
>> >> one directory and store your scripts somewhere
>> else, and the
>> >> problem is
>> >> worse when you have many script versions or
>> directories.
>> >>
>> >> I know there are many ways to work around this
>> issue.
>> >> I also know about the batch file project.
>> >>
>> >> However, it seems like this is something that
>> users shouldn't
>> >> *need* to work
>> >> around.
>> >>
>> >> I think this is related to the fact that R has
>> no way of knowing
>> >> the
>> >> location of any open script file.
>> >> (Please correct me if I'm wrong about that last
>> part! )
>> >>
>> >> I think this has come up before, but I couldn't
>> find it in my
>> >> searches.
>> >> Is this a problem for other users as well?
>> >> Should I post this to the development list?
>> >> Is this change known to be possible / impossible?
>> >> Can I help make the change?
>> >>
>> >>
>> >> I think it's simply a matter of different working
>> patterns. R
>> >> offers the current working directory to save into.
>> For some people,
>> >> that's fine. Others (like you) want some other
>> default.
>> >>
>> >> I don't think we want to make the editor so
>> elaborate that it caters
>> >> to every taste. If you want a better editor, there
>> are lots out
>> >> there, including several that have good integration
>> with R (Emacs,
>> >> Eclipse, WinEdt, TinnR, etc.) I think it would be
>> best to use one
>> >> of those editors, customized to suit your needs.
>> >>
>> >> Duncan Murdoch
>> >>
>> >>
>> >> Thanks
>> >>
>> >> Gene
>> >>
>> >> [[alternative HTML version deleted]]
>> >>
>> >> ______________________________________________
>> >> R-help_at_r-project.org
>> <mailto:R-help_at_r-project.org><mailto:R-help_at_r-project.org
>>
>> <mailto: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.
>> >>
>> >>
>> >>
>> >>
>> >
>>
>>
>>
>>
>>
>

        [[alternative HTML version deleted]]



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. Received on Wed 13 Apr 2011 - 22:22:57 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 13 Apr 2011 - 22:30:30 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive