Skip to content

New behavior when running script in package directory?

7 messages · Jan van der Laan, Dominick Samperi, Bill Dunlap +1 more

#
When I run a script foo.R containing some trivial code in my home
directory, via Emacs/ESS, everything works as expected: R
starts, and a setwd() command to set the working directory is
run automatically before the code in the script is run.

But if I copy foo.R to some package/R directory strange
things happen. When I use Emacs/ESS to run the script
in its new location, R starts, and setwd() is called to set
the working directory, but then one or more libraries that the
package depends on are loaded, even though I am using no
libraries in foo.R.

Now consider foo.R that contains the following trivial code:
secsToRDateTime <- function(secs) {
  day2sec <- 60*60*24
  days <- secs/day2sec
}

When I try to run this from package/R I get...

Error in ss(file, echo = visibly, local = local, print.eval = output,  :
  /tmp/gpstime.R!CuSewT:2:0: unexpected end of input
1: secsToRDateTime <- function(secs) {
   ^

As I said, there are no problems when the script is run from my
home directory. This suggests that test scripts can no longer be
tested in a package's R directory?

Is this true?

Thanks,
Dominick
#
Could there be a .Rprofile or .RData file in the directory where you 
start R? These are run/loaded when you start R when present (with the 
default arguments when starting R).

HTH,
Jan
On 20-06-2023 23:38, Dominick Samperi wrote:
#
Thanks, I checked for .Rprofile and .RData files. They are not present.
I also tried renaming my .emacs.d file in case the problem is due to
my Emacs configuration, but this didn't change anything.

If I go to package/R, start R, and use source('foo.R'), there is no problem.

But if I use Emacs/ESS to run the script foo.R (with no .emacs.d) I get
an error referring to a temp file /tmp/foo.R!djSwRn, namely

Error in ss(file, echo = visibly, local = local, print.eval = output,  :
  /tmp/foo.R!djSwRn:2:0: unexpected end of input
1: foo <- function(secs) {

Here are the contents of foo.R:
foo <- function(secs) {
  return(secs)
}

What seems to be happening is this. After reading the script into Emacs and
using Ctrl-n to start emacs, R starts, setwd(<path to packageA/R>) is run,
and then the signal package is attached (a dependency of packageA), and
I get the error message. There is no problem when Emacs is used to run
the script in my home directory.

This raises a couple of questions:
1. Why does Emacs/ESS behave differently depending on the
current working directory?
2. Why is the signal package loaded automatically?
3. Why is that temporary file /tmp/foo.R!djSwRn created?
4. What is that function ss() in the error message referring to?
5. Could this be a virus? Under Ubuntu?

Thanks,
Dominick
On Wed, Jun 21, 2023 at 2:17?AM Jan van der Laan <rhelp at eoos.dds.nl> wrote:

            

  
  
#
If ./Rprofile is not present and ~/.Rprofile is present then R will run the
latter at startup.  Do you have a ~/.Rprofile that defines a ss() function?

-Bill

On Wed, Jun 21, 2023 at 8:50?AM Dominick Samperi <djsamperi at gmail.com>
wrote:

  
  
#
Thanks, there is no ~/.Rprofile, and I have no idea where ss() is defined.

On Wed, Jun 21, 2023 at 12:01?PM Bill Dunlap <williamwdunlap at gmail.com>
wrote:

  
  
#
On Wed, 21 Jun 2023 11:49:24 -0400
Dominick Samperi <djsamperi at gmail.com> wrote:

            
There's a "package development mode" in ESS:
https://github.com/emacs-ess/ESS/blob/master/lisp/ess-r-package.el

I think that ESS automatically detects that you're inside a package
directory and adjusts its behaviour. This could be related to
"namespaced evaluation"
<https://ess.r-project.org/Manual/ess.html#Namespaced-Evaluation>
("evaluate code in the context of the package being developed", sounds
useful), but I'm not sure about that.
It's probably some part of ESS's mechanism of running R code. I don't
see where the temporary file name is constructed (maybe a built-in
Emacs function?), but it probably comes from
<https://github.com/emacs-ess/ESS/blob/master/lisp/ess-r-mode.el>.
(Look for calls to .ess.source or .ess.ns_source.)
`ss` is a function defined by ESS in order to call R's source() in a
way portable between a very wide range of R versions. (A comment in the
same file says "Should work on *all* versions of R. Do not use _ in
names, nor :: , nor 1L", which includes versions of R considered very
old by now.):

https://github.com/emacs-ess/ESS/blob/master/etc/ESSR/R/.basic.R#L178
A virus has little reason to be doing this to you. There's much more
money to be made from malicious VSCode extensions than Emacs add-on
packages.

Since you've determined ESS to be causing the difference in the
behaviour, perhaps
<https://ess.r-project.org/index.php?Section=getting%20help> is the way
to go? Unfortunately, I'm not a user of ESS or Emacs, so I'm going off
what information I found on the Web, which may be outdated.
#
Thanks Ivan, looks like bug in ESS, but I only noticed this recently, so it
may have been triggered by changes in recent R versions.
On Wed, Jun 21, 2023 at 12:26?PM Ivan Krylov <krylov.r00t at gmail.com> wrote: