Skip to content

Commit

Permalink
factor out some code from #1918
Browse files Browse the repository at this point in the history
  • Loading branch information
yihui committed Jan 29, 2021
1 parent 20f8571 commit 9973982
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions R/plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -520,18 +520,17 @@ runtime_shiny = function() {

webshot_available = local({
res = NULL # cache the availability of webshot2/Chrome and webshot/PhantomJS
test = function(pkg, fun) {
tryCatch(
file.exists(getFromNamespace(fun, pkg)()),
error = function(e) FALSE
)
}
function() {
if (is.null(res))
res <<- c(
webshot2 = (
loadable('webshot2') &&
tryCatch(
file.exists(getFromNamespace('find_chrome', 'chromote')()),
error = function(e) FALSE
)
),
webshot = loadable('webshot') && !is.null(getFromNamespace('find_phantom', 'webshot')())
)
if (is.null(res)) res <<- c(
webshot2 = test('webshot2', 'find_chrome'),
webshot = test('webshot', 'find_phantom')
)
res
}
})
Expand Down

5 comments on commit 9973982

@yihui
Copy link
Owner Author

@yihui yihui commented on 9973982 Jan 29, 2021

Choose a reason for hiding this comment

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

@atusy Does this change look okay to you?

@atusy
Copy link
Collaborator

@atusy atusy commented on 9973982 Jan 29, 2021

Choose a reason for hiding this comment

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

@yihui Yes. This is much much better! Thank you!!

@yihui
Copy link
Owner Author

@yihui yihui commented on 9973982 Jan 29, 2021

Choose a reason for hiding this comment

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

Great. Perhaps you can collect these examples, and someday write a book titled "Writing Shorter R Code (with A Thousand Practical Examples)" :)

@atusy
Copy link
Collaborator

@atusy atusy commented on 9973982 Jan 29, 2021

Choose a reason for hiding this comment

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

LOL

Sorry, I found a bug with your refactoring.
find_chrome is implemented by chromote, not by webshot2.
This means the function always catches the error.

9973982#diff-cebed083352aaa9f9cf564e06b3e28047ff8da73972705e9e64bd69d8e999478L529

@yihui
Copy link
Owner Author

@yihui yihui commented on 9973982 Jan 29, 2021

Choose a reason for hiding this comment

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

Oh good catch! Amended in d83e8de. Thanks!

BTW, the book should be titled "Writing Shorter But Buggy R Code" now...

Please sign in to comment.