Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: Use rlang::check_installed() internally to install missing suggested packages on the fly #2040

Merged
merged 6 commits into from
Oct 18, 2023

Conversation

olivroy
Copy link
Contributor

@olivroy olivroy commented Oct 13, 2023

Close #2039 . (possibly enough for #1735 and #1348 imo)
follow-up to #2036

Use only loop in testing.

Also, for dm_gui(), handle suggested at once will improve the UX significantly.

# Now
> dm_gui(dm = dm)
ℹ The packages "colourpicker", "shinyAce", and "shinydashboard" are required to use `dm_gui()`.Would you like to install them?

1: Yes
2: No

I also return TRUE early if all packages are installed. Is that correct?

@olivroy

This comment was marked as off-topic.

@olivroy olivroy changed the title chore: Improve suggested packages handling chore: Improve suggested packages handling + fix CRAN note (curly brackets) Oct 13, 2023
@krlmlr
Copy link
Collaborator

krlmlr commented Oct 15, 2023

Thank you for your help. Can you please split this PR into two?

https://code-review.tidyverse.org/author/focused.html

@olivroy

This comment was marked as resolved.

@krlmlr
Copy link
Collaborator

krlmlr commented Oct 15, 2023

Locally:

git checkout main
git pull
git checkout -b b-fix-ci
git cherry-pick suggests-follow

This cherry-picks the last commit on top of the main branch, this is the one I'd like to merge first. Then, open a new pull request.

After that:

git checkout suggests-follow
git reset --hard HEAD^

This erases the last commit.

For more complex splitting, interactive rebase is helpful.

@olivroy

This comment was marked as resolved.

@olivroy

This comment was marked as off-topic.

@olivroy olivroy changed the title chore: Improve suggested packages handling + fix CRAN note (curly brackets) chore: Improve suggested packages handling Oct 16, 2023
@olivroy
Copy link
Contributor Author

olivroy commented Oct 16, 2023

From my testing, I verified #1348. Indeed, check_suggested() can work with multiple packages.

Copy link
Collaborator

@krlmlr krlmlr left a comment

Choose a reason for hiding this comment

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

Thanks for following up! I think we might be able to get rid of the for loops entirely.

R/check_suggested.R Outdated Show resolved Hide resolved
R/check_suggested.R Outdated Show resolved Hide resolved
@olivroy
Copy link
Contributor Author

olivroy commented Oct 16, 2023

There may be some code duplication, but this seems to work as intended now. getting rid of version, and message argument.

I removed the install.packages() bit because some version may be attached to it, which would create invalid code.

I just added a test.

@olivroy olivroy requested a review from krlmlr October 16, 2023 17:17
Copy link
Collaborator

@krlmlr krlmlr left a comment

Choose a reason for hiding this comment

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

Thanks, cool!

b6f5382 adds a lot of noise here, can you please send this as a separate PR?

Comment on lines 22 to 24
installed <- map_lgl(packages, function(pkg) {
is_installed(pkg)
})
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
installed <- map_lgl(packages, function(pkg) {
is_installed(pkg)
})
installed <- map_lgl(packages, is_installed)

Can this move even higher, outside if (!use) ? We could return immediately if all packages are installed, in all cases.

R/check_suggested.R Outdated Show resolved Hide resolved
R/check_suggested.R Outdated Show resolved Hide resolved
Comment on lines 48 to 50
installed <- map_lgl(packages, function(pkg) {
is_installed(pkg)
})
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
installed <- map_lgl(packages, function(pkg) {
is_installed(pkg)
})

Redundant now?

Comment on lines 40 to 44
# Return early if all packages are installed.
if (rlang::is_installed(packages)) {
return(TRUE)
}

Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
# Return early if all packages are installed.
if (rlang::is_installed(packages)) {
return(TRUE)
}

Not needed if we do an early return?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried playing with the order without chaning test results, but it didn't work.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Possibly in a follow-up because this PR aims not to change the default behavior. (except gor removing the version and message argument from check_suggested

tests/testthat/_snaps/check_suggested.md Outdated Show resolved Hide resolved
tests/testthat/_snaps/check_suggested.md Show resolved Hide resolved
@olivroy
Copy link
Contributor Author

olivroy commented Oct 16, 2023

Reverted b6f5382 + added cli pluralization. Not sure about changing the order as it may result in different results, depending on which packages are installed on the system.

@krlmlr krlmlr force-pushed the suggests-follow branch 2 times, most recently from 4ced9c0 to 7fdd892 Compare October 16, 2023 22:06
@krlmlr
Copy link
Collaborator

krlmlr commented Oct 16, 2023

I force-pushed, if needed you can use

git fetch
git reset --hard origin/suggests-follow

to sync locally.

Thanks for recognizing a real shortcoming in our approach to handling dependencies and for pushing through!

@krlmlr krlmlr changed the title chore: Improve suggested packages handling docs: Use rlang::check_installed() internally to install missing suggested packages on the fly Oct 16, 2023
@olivroy
Copy link
Contributor Author

olivroy commented Oct 16, 2023

Glad you like it and that you validated what I did. Always enriching. Tests still pass locally for me after changes you made.

@krlmlr
Copy link
Collaborator

krlmlr commented Oct 16, 2023

Looking into ?check_installed, I wonder if we could make do with one single try_catch(check_installed(...), ...) call. I suspect this won't make the code easier to understand, to debug, or to enhance -- I suggest calling it a pull request and see how this works in practice before changing further.

@olivroy
Copy link
Contributor Author

olivroy commented Oct 16, 2023

Looking into ?check_installed, I wonder if we could make do with one single try_catch(check_installed(...), ...) call. I suspect this won't make the code easier to understand, to debug, or to enhance -- I suggest calling it a pull request and see how this works in practice before changing further.

check_suggest <- function(pkg, call = rlang::caller_env()) {
  rlang::local_interactive(FALSE) # To avoid the prompt
  
  rlang::try_fetch(
    rlang::check_installed(pkg, reason = "to have an enhanced functionality.", call = call),
    error = function(e) {rlang::inform("The function is enhanced by the following package, ", parent = e, call = call)})
  invisible()
}
g <- function(x) {
  check_suggest(c("ggplot2 (>= 3.5.0)", "rsvg", "sss"))
  x**2
}
g(2)
#> The function is enhanced by the following package, 
#> Caused by error in `g()`:
#> ! The packages "ggplot2" (>= 3.5.0) and "sss" are required to have an enhanced functionality.
[1] 4

An example I shared earlier that could be what you were thinking about? But as I pointed out, the message shared by check_installed() is not 100% correct for the suggested enhancements.

@krlmlr
Copy link
Collaborator

krlmlr commented Oct 16, 2023

Thanks for the example, I missed that. The condition object e has elements that contain the packages and versions that were missing, so in theory we could put together the same message, but I don't think it's worth the effort at this time.

@olivroy

This comment was marked as resolved.

@krlmlr krlmlr merged commit c48fab1 into cynkra:main Oct 18, 2023
2 checks passed
@krlmlr
Copy link
Collaborator

krlmlr commented Oct 18, 2023

Thanks!

@olivroy olivroy deleted the suggests-follow branch October 18, 2023 13:26
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 19, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Remove message argument to check_suggested()
2 participants