2

I've created very simple package, using RStudio, to demonstrate my problems, the steps follow.

  1. Create package with only one function
write <- function(text) {
      print(text)
      if (is.numeric(text))
        stop("Text cannot be numeric.")
}
  1. Build -> Install and Restart

  2. In a new R Script, write following code

    pckgname::write(1)
  1. Result in Console:

    tempPackage::write(1)
    [1] 1
    Error in tempPackage::write(1) : Text cannot be numeric.
    Called from: tempPackage::write(1)
    Browse[1]>

  2. Problems
    5a) I don't want to see "Called from: tempPackage::write(1)".
    5b) More importantly, I don't want to end with "Browse[1]> " meaning debug mode is still open (also buttons like "Continue" and "Stop" are visible) so now I need to click on button "Continue" to finish debug mode to end on line with ">" but I want to end this debug mode without clicking on button "Continue" meaning when I run that one line code "tempPackage::write(1)" I want to see result in Console with finished debug mode.

Additional information:

  • When I use this stop in function directly in the same R Script (not called from a package), it works like I want.
  • Solution using following method also doesn't help with previous problems.
    opt <- base::options(show.error.messages = F)
    on.exit(base::options(opt))

Thank you for any help in advance.

1 Answers1

2

I just did exactly as you instructed and I see the expected result:

pckgname::write(1)
[1] 1
Error in pckgname::write(1) : Text cannot be numeric.

I'm not sure what's wrong when you try it, but would recommend starting a fresh package in case something is not working correctly. You could even try reinstalling R and RStudio, as it worked first time for me and I can't think of why it wouldn't for anyone else.

For reference, here's exactly what I did:

  • I started the package from RStudio -> File -> New Project -> New Directory -> New Package

  • The only file I changed was hello.R - I added the code in your question exactly as-is.

  • I built the package with RStudio -> Build -> Build from source

  • And then installed it with install.packages("../pckgname_0.1.0.tar.gz", repos = NULL, type="source")

  • And loaded with library(pckgname)

stevec
  • 41,291
  • 27
  • 223
  • 311
  • 1
    Hello stevec, thank you, the only difference between your and my steps was in a way of installation. When I installed it using previously built source package (.tar.gz file) as you did, it worked as per my expectations. – LearnUseZone Nov 25 '20 at 13:32
  • I don't understand why there's such difference between those 2 ways of package installations. Can you please advise, if you have any idea? – LearnUseZone Nov 25 '20 at 13:40
  • @LearnUseZone I'm not sure what went wrong, but you're not alone in finding package installation from source a little tricky. I look up [this answer](https://stackoverflow.com/a/1474125/5783745) approximately monthly, and judging by the upvotes, so do a lot of others. I use that method always (unless installing directly from CRAN), so I haven't any experience trying any other ways – stevec Nov 25 '20 at 13:42