问题
I have built a package for shiny that allows the user to interact with reactive objects in their global environment. I think it's a game changer for troubleshooting. However, I know that CRAN will reject this due to the manipulation of the global environment. I see answers directing users to create a new environment but I don't see how to access the objects of that environment in the environment pane, I just see the environment name.
If I run something like this from this example: Global variable in a package - which approach is more recommended?
e <- new.env()
assign("a", "xyz123", envir = e)
e$b <- 1
I see this, and clicking on e
will call View(e)
I want e
to be something the user can see on the right-hand side like it is when the user is in their global environment or when debugging a function:
A similar question was asked here but did not address changing how the user sees objects in the IDE:
CRAN policy on use of global variables
This stuff is new territory so I hope my question makes sense.
回答1:
If you run attach(e)
, then e
will be available to be chosen in the environment pane. You should pair this with detach(e)
so that you don't permanently mess up the user's search list.
If the user chooses to look at e
when it is attached, it won't disappear when it is detached, but it won't be available to select again after being detached if the user views a different environment.
I don't know if there's an RStudio API way to automatically select it.
Edited to add: The attach()
function creates a new environment copying the value of e
at the time of attaching. That's probably not what you want. There is a way to get a live one though:
attach(NULL, name = "Viewable")
e <- as.environment("Viewable")
# Now somehow get the user to view it in the Environment pane
# Clean up the search list
detach("Viewable")
You then have a live environment, things like
e$a <- 123
will show up there.
来源:https://stackoverflow.com/questions/57760473/package-environment-manipulation-and-submitting-to-cran