View() error when utils::menu() is run in RProfile

In order to encourage myself to review vocab words more often, I wrote a function that calls utils::menu() and placed it in my RProfile. That way, I would be asked about vocabulary every time I restart my R session.

However, this seems to lead to a weird error with View() (related GitHub issue at https://github.com/isteves/flashcards/issues/1):

> View(mtcars)
Error in hist(hist_vals, plot = FALSE) : could not find function "hist"

I suspect that by running utils::menu(), I'm somehow blocking some start-up processes that normally run.

Any insight into this problem would be great!


To reproduce this problem:

  1. usethis::edit_r_profile() to open your RProfile
  2. Comment out whatever is already there and add:
if(interactive()) {
  utils::menu(c("apple", "orange", "carambola"), title = "pick a fruit")
}
  1. Restart R
  2. Run View(mtcars)

I tried a few variations of this to see what happened, and I had a different experience than what you describe here.
Adding the utils::menu() call in my .Rprofile has the effect of completely crashing my Rstudio on restarts, i get compains about missing graphics package etc. I have to clear the .Rprofile to get Rstudio working again. Curiously, Rstudio will restart and behaviour normally if I add the graphics = TRUE flag to the utils::menu() call.
Having succesfully put the 'graphic' style utils::menu in my Rstudio on restart, I hesitantly tried to View(mtcars). It came up no problem...

very puzzling.

Interesting! I'll try graphics = TRUE and see if that solves the issue.

I can reproduce this as well (hist not found when calling View). Would you mind filing this as a bug report at https://github.com/rstudio/rstudio/issues?

As an alternative, you could use an RStudio session init hook instead for RStudio:

setHook("rstudio.sessionInit", function(...) {
    utils::menu(...)
})

and this would ensure that this code runs after RStudio has finished its regular initialization and should work as normal.

1 Like

Sounds good. I filed an issue (https://github.com/rstudio/rstudio/issues/6377). I'll also try the init hook. :+1: The graphics method seemed to work but crashed RStudio every time I tried to close XQuartz afterwards.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.