Skip to content

Commit

Permalink
Merge pull request #20 from atusy/addin
Browse files Browse the repository at this point in the history
fix: broken rstudio addin
  • Loading branch information
atusy authored Mar 29, 2024
2 parents 34b0c66 + c3fd358 commit 7be4f7b
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 21 deletions.
4 changes: 2 additions & 2 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# felp 0.3.0.9999

- Support `fuzzyhelp` to run Shiny App in background without blocking user terminal.
The new behavior is enabled by default and can be disabled by passing `FALSE` to the `background` argument or to the `fuzzyhelp.background` option.
The new behavior is enabled by default and can be disabled by passing `FALSE` to the `background` argument or to the `fuzzyhelp.background` option (#18, #20).
- Fixed wrong behaviors of anchors in the HTML help in the UI of the `fuzzyhelp` function.
A click on a anchor should not cause nesting of the UI when href of the anchor is an ID.
Instead, the click should scroll the window to show the element with the corresponding ID.
Instead, the click should scroll the window to show the element with the corresponding ID (#17).

# felp 0.3.0

Expand Down
73 changes: 61 additions & 12 deletions R/fuzzyhelp.R
Original file line number Diff line number Diff line change
Expand Up @@ -379,14 +379,17 @@ create_server <- function(method = c("fzf", "lv")) {
#' @param background Whether to run a shiny gadget in a background process.
#' The default value is `TRUE` and can be changed by
#' `option(fuzzyhelp.background = FALSE)`.
#' @inheritParams shiny::runGadget
#'
#' @note
#' The default fuzzy match algorithm is a simplified version of
#' <https://github.com/junegunn/fzf>. The implementation in this package
#' excludes bonuses from relationship with matched characters and their
#' previous characters.
#'
#' @return NULL
#' @return
#' If the `background` argument is `TRUE`, then the return value inherits from
#' `callr::r_bg()`. Otherwise, `NULL` is returned.
#'
#' @examples
#' if (FALSE) {
Expand All @@ -397,13 +400,14 @@ create_server <- function(method = c("fzf", "lv")) {
fuzzyhelp <- function(
query = "",
method = getOption("fuzzyhelp.method", "fzf"),
background = getOption("fuzzyhelp.background", TRUE)) {
background = getOption("fuzzyhelp.background", TRUE),
viewer = shiny::paneViewer()) {
app <- create_ui(query)
server <- create_server(method)

# Create new gadget on foreground
if (!background) {
shiny::runGadget(app, server)
shiny::runGadget(app, server, viewer = viewer)
return(invisible(NULL))
}

Expand All @@ -412,32 +416,77 @@ fuzzyhelp <- function(
.env$fuzzyhelp_url <- tempfile()
}

# Re-use existing gadget
# View existing gadget
if (fuzzyhelp_bg_view(viewer)) {
return(.env$fuzzyhelp)
}

# Create new gadget on background
if (rstudioapi::isAvailable()) {
# Just start the UI without viewer because RStudio's viewer
# is not available fro the background process.
.env$fuzzyhelp <- fuzzyhelp_bg_start(app, server, identity)

# Wait and view UI in the main process.
min_seed <- 1L
for (i in c(rep(min_seed, 10L), seq(min_seed + 1, 10))) {
if (fuzzyhelp_bg_view(viewer)) {
return(.env$fuzzyhelp)
} else {
# Wait with exponential backoff
t <- max((i**2) / 10, 0.5)
if (i > min_seed) {
# Don't be too noisy
message("Failed to open fuzzyhelp UI. Will retry in ", t, " seconds")
}
Sys.sleep(t)
}
}
stop("Failed to open fuzzyhelp UI. Try using fuzzyhelp(background = FALSE)")
}

.env$fuzzyhelp <- fuzzyhelp_bg_start(app, server, viewer)
return(.env$fuzzyhelp)
}

fuzzyhelp_bg_view <- function(viewer) {
if (
!is.null(.env$fuzzyhelp) &&
is.null(.env$fuzzyhelp$get_exit_status()) &&
file.exists(.env$fuzzyhelp_url)
) {
url <- readLines(.env$fuzzyhelp_url)[1L]
if (url != "") {
shiny::paneViewer()(url)
return(.env$fuzzyhelp)
viewer(url)
return(TRUE)
}
}
return(FALSE)
}

# Create new gadget on background
.env$fuzzyhelp <- callr::r_bg(
function(..., .env) {
fuzzyhelp_bg_start <- function(app, server, viewer) {
writeLines("", .env$fuzzyhelp_url) # Ensure content is empty
callr::r_bg(
function(..., .env, base_viewer, base_options) {
do.call(options, base_options)
viewer <- function(url) {
writeLines(url, .env$fuzzyhelp_url)
shiny::paneViewer()(url)
base_viewer(url)
}
shiny::runGadget(..., viewer = viewer)
},
args = list(app = app, server = server, .env = .env),
args = list(
app = app,
server = server,
.env = .env,
base_viewer = viewer,
base_options = options()
),
env = Sys.getenv(),
package = TRUE
)
}

return(.env$fuzzyhelp)
fuzzyhelp_addin <- function() {
fuzzyhelp(background = TRUE)
}
2 changes: 1 addition & 1 deletion docs/articles/felp.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions docs/news/index.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ pkgdown: 2.0.7
pkgdown_sha: ~
articles:
felp: felp.html
last_built: 2024-03-28T00:10Z
last_built: 2024-03-29T00:01Z

16 changes: 15 additions & 1 deletion docs/reference/fuzzyhelp.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion inst/rstudio/addins.dcf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Name: Fuzzy Search on R Help
Description: Search and view help files with fuzzy queries.
Binding: fuzzyhelp
Binding: fuzzyhelp_addin
Interactive: true
11 changes: 10 additions & 1 deletion man/fuzzyhelp.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7be4f7b

Please sign in to comment.