Skip to content

New install + restart behavior for R packages - #1099

Merged
juliasilge merged 9 commits into
mainfrom
package-build-task
Sep 8, 2023
Merged

juliasilge merged 9 commits into
mainfrom
package-build-task

Conversation

@juliasilge

Copy link
Copy Markdown
Member

Addresses #1091

This doesn't quite work yet because it does not actually wait for the runtime to restart.

},
{
"command": "r.packageBuild",
"command": "r.packageInstall",

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I kept the r.packageBuild command as is and instead we now map the r.packageInstall command to the Cmd Shift B keybinding.

@jjallaire
jjallaire marked this pull request as ready for review August 25, 2023 16:30
@jjallaire
jjallaire marked this pull request as draft August 25, 2023 16:33
@jennybc

jennybc commented Aug 28, 2023

Copy link
Copy Markdown
Member

For lack of a better place to note/discuss this: RStudio actually preserves the global workspace when you do Build > Install, even if you are a person who normally does not save/load the global workspace when restarting R. I've always had mixed feelings about this and can definitely get on board with Positron not preserving the workspace (which is how this PR will behave), as part of its more general posture towards encouraging workspace discipline and hygiene.

But we should all note that this is going to be a difference that people probably notice. Hopefully they'll like it!

@jjallaire

Copy link
Copy Markdown
Contributor

I'd expect that for some users that are using a test dataset to iterate on a package (e.g. keep plotting the same thing over and over) that this would be quite annoying as they can't just arrow up to re-execute the plot (as they'd need to first load/transform the data). That said, I think those users would be well served by devtools::load_all(), correct? In that case I think this limitation is fine (and as you said a net plus for some workflows).

@kevinushey

Copy link
Copy Markdown
Contributor

FWIW the next release of RStudio will allow users to control whether RStudio saves and restores the workspace on Install:

Screenshot 2023-08-28 at 2 49 09 PM

return new Promise(resolve => { setTimeout(resolve, ms); });
};

if (oldTimestamp === null) {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the case where the package hasn't been installed yet at all.

while (!fs.existsSync(libraryPath)) {
await wait(1000);
}
} else {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the more typical re-installation case.

@juliasilge

juliasilge commented Sep 7, 2023 •

Copy link
Copy Markdown
Member Author

The last piece we needed here was to wait while devtools::install() finished, and there are some limitations on what we can do right now either through the executeCode API (no way to wait) or checking if the runtime is busy (R extension only has metadata on the running runtime and can't check it). So what I did here was to poll for a side effect of the installation, i.e. the status of files in the runtime's library. Thanks for the push in that direction @jmcphers!

I tested this out for packages that have already been installed and never been installed, and things seem to be working OK:

install-restart.mov

You can see that the package is in fact attached after it's done.

Some differences from the RStudio behavior include:

  • the restart empties your global workspace
  • this is happening in the console, not separately (not like the RStudio Build pane)

I could be convinced both of these are neutral to improvements, but am open of course to what works out best later for these issues.

@juliasilge
juliasilge marked this pull request as ready for review September 7, 2023 02:42
@juliasilge
juliasilge requested a review from jmcphers September 7, 2023 02:42
@juliasilge

Copy link
Copy Markdown
Member Author

If devtools::install() errors, this seems to be OK. The error is surfaced to the user nicely and at least as far as I can tell, we break out of the command altogether (i.e. we do not get stuck in one of those while() loops forever). Definitely worth another set of eyes on that, though.

case positron.RuntimeClientState.Closing:
case positron.RuntimeClientState.Closed: {
out.reject(new Error( `Comm ${this._id} closed before connecting`));
out.reject(new Error(`Comm ${this._id} closed before connecting`));

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The whitespace change here and in utils.ts happened because I just opened the files. I just spent some time trying to undo these without much success. OK to leave in, since they are our files?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The linting is AGGRESSIVE! 😆

Comment thread extensions/positron-r/src/commands.ts Resolved
const runningRuntimes = await positron.runtime.getRunningRuntimes('r');
// For now, there will be only one running R runtime:
const runtimePath = runningRuntimes[0].runtimePath;
const originalTimeStamp = getPackageDescriptionTimestamp(runtimePath, packageName);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we guaranteed that the runtime path is also the root of the library folder? (wondering if we need to check e.g. .libPaths())

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am hesitant to guarantee anything given the wild world of people's R installations. However, I don't know how we would call .libPaths() from the R extension currently. We could call it via executeCode in the console (but wouldn't be able to capture the output) or via R -e in the terminal (definitely not guaranteed to be the right runtime at all); neither of those is what we want. This is like something @jennybc has mentioned a few times, that she believes we'll eventually need a way to run a tiny bit of R code in a way hidden to the user, in a separate/clean process, but in the right R runtime.

Do you think we should pursue some way of doing this now?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To step back a moment, the challenge is how to wait while some code finishes, when we don't currently have a great option for that. This option of checking for a file on disk changing is also not great, but what do we want to do about it right now? If this isn't robust enough for our next steps, some options I can think of are:

  • create a way for the R extension to query whether a runtime is busy/idle/etc
  • change what executeCode returns to fill this need
  • make a way for the extension to call the right R runtime in a way hidden from the user and get return values back into TypeScript

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, I'm really more interested in your assessment of how often this simplifying assumption of "runtime path = library path" will hold. If it's the > 90% case then we can merge it as is but file an issue to deal with later.

It's true we need a way to execute R and get results, but we definitely don't want to try to invent that for this PR.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my experience with teaching and such, I see non-default (or "personal") libraries much more often for Windows R users. If we are talking about Mac users, I would estimate that very few would have problems with runtimePath also being where to find the library, and >90% seems right. I do think we'll need to change this by the time we have Windows users. I outlined next steps for this in #1279.

Comment thread extensions/positron-r/src/commands.ts Resolved

@jmcphers jmcphers left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This LGTM presuming you don't think the runtime/library path issue is going to be a problem for most folks!

@juliasilge

Copy link
Copy Markdown
Member Author

Thanks again! I outlined the limitations of this approach and possible next steps in #1279, and that would be a great place for other clever ideas of how to improve this.

@juliasilge
juliasilge merged commit 5a7a5c2 into main Sep 8, 2023
@juliasilge
juliasilge deleted the package-build-task branch September 8, 2023 15:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants