WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit c34db69

Browse files
authored
fix(pkg-r): Directly expose id rather than ask for ns() function (#173)
* refactor: Allow directly providing `id` * chore: Also `$sidebar()` and update docstrings
1 parent fe79fd1 commit c34db69

File tree

2 files changed

+46
-29
lines changed

2 files changed

+46
-29
lines changed

pkg-r/R/QueryChat.R

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -474,8 +474,10 @@ QueryChat <- R6::R6Class(
474474
#' @param width Width of the sidebar in pixels. Default is 400.
475475
#' @param height Height of the sidebar. Default is "100%".
476476
#' @param fillable Whether the sidebar should be fillable. Default is `TRUE`.
477-
#' @param ns A Shiny namespacing (i.e., [shiny::NS()]) function.
478-
#' Only needed when calling this method within a module UI function.
477+
#' @param id Optional ID for the QueryChat instance. If not provided,
478+
#' will use the ID provided at initialization. If using `$sidebar()` in
479+
#' a Shiny module, you'll need to provide `id = ns("your_id")` where `ns`
480+
#' is the namespacing function from [shiny::NS()].
479481
#'
480482
#' @return A [bslib::sidebar()] UI component.
481483
#'
@@ -493,15 +495,15 @@ QueryChat <- R6::R6Class(
493495
width = 400,
494496
height = "100%",
495497
fillable = TRUE,
496-
ns = NULL
498+
id = NULL
497499
) {
498500
bslib::sidebar(
499501
width = width,
500502
height = height,
501503
fillable = fillable,
502504
class = "querychat-sidebar",
503505
...,
504-
self$ui(ns = ns)
506+
self$ui(id = id)
505507
)
506508
},
507509

@@ -512,8 +514,10 @@ QueryChat <- R6::R6Class(
512514
#' `$sidebar()` instead, which wraps this in a sidebar layout.
513515
#'
514516
#' @param ... Additional arguments passed to [shinychat::chat_ui()].
515-
#' @param ns A Shiny namespacing (i.e., [shiny::NS()]) function.
516-
#' Only needed when calling this method within a module UI function.
517+
#' @param id Optional ID for the QueryChat instance. If not provided,
518+
#' will use the ID provided at initialization. If using `$ui()` in a Shiny
519+
#' module, you'll need to provide `id = ns("your_id")` where `ns` is the
520+
#' namespacing function from [shiny::NS()].
517521
#'
518522
#' @return A UI component containing the chat interface.
519523
#'
@@ -525,27 +529,16 @@ QueryChat <- R6::R6Class(
525529
#' qc$ui()
526530
#' )
527531
#' }
528-
ui = function(..., ns = NULL) {
529-
check_function(ns, allow_null = TRUE)
532+
ui = function(..., id = NULL) {
533+
check_string(id, allow_null = TRUE, allow_empty = FALSE)
530534

531535
# If called within another module, the UI id needs to be namespaced
532536
# by that "parent" module. If called in a module *server* context, we
533537
# can infer the namespace from the session, but if not, the user
534538
# will need to provide it.
535-
# NOTE: this isn't a problem for Python since id namespacing is handled implicitly
536-
# by UI functions like shinychat.chat_ui().
537-
id <- self$id
538-
id <- if (is.null(ns)) namespaced_id(id) else ns(id)
539-
540-
# Provide a helpful error if the user tries to set id directly
541-
if ("id" %in% names(list2(...))) {
542-
cli::cli_abort(
543-
c(
544-
"Not allowed to set {.arg id} to {.fn $ui()} (or {.fn $sidebar()}).",
545-
"i" = "Use the {.arg ns} argument instead to namespace the UI id."
546-
)
547-
)
548-
}
539+
# NOTE: this isn't a problem for Python since id namespacing is handled
540+
# implicitly by UI functions like shinychat.chat_ui().
541+
id <- id %||% namespaced_id(self$id)
549542

550543
mod_ui(id, ...)
551544
},
@@ -563,6 +556,12 @@ QueryChat <- R6::R6Class(
563556
#' with Shiny bookmarks. This requires that the Shiny app has bookmarking
564557
#' enabled via `shiny::enableBookmarking()` or the `enableBookmarking`
565558
#' parameter of `shiny::shinyApp()`.
559+
#' @param ... Ignored.
560+
#' @param id Optional module ID for the QueryChat instance. If not provided,
561+
#' will use the ID provided at initialization. When used in Shiny modules,
562+
#' this `id` should match the `id` used in the corresponding UI function
563+
#' (i.e., `qc$ui(id = ns("your_id"))` pairs with `qc$server(id =
564+
#' "your_id")`).
566565
#' @param session The Shiny session object.
567566
#'
568567
#' @return A list containing session-specific reactive values and the chat
@@ -586,16 +585,21 @@ QueryChat <- R6::R6Class(
586585
#' }
587586
server = function(
588587
enable_bookmarking = FALSE,
588+
...,
589+
id = NULL,
589590
session = shiny::getDefaultReactiveDomain()
590591
) {
592+
check_string(id, allow_null = TRUE, allow_empty = FALSE)
593+
check_dots_empty()
594+
591595
if (is.null(session)) {
592596
cli::cli_abort(
593597
"{.fn $server} must be called within a Shiny server function"
594598
)
595599
}
596600

597601
mod_server(
598-
self$id,
602+
id %||% self$id,
599603
data_source = private$.data_source,
600604
greeting = self$greeting,
601605
client = self$client,

pkg-r/man/QueryChat.Rd

Lines changed: 19 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)