Skip to content

Commit 24d1c60

Browse files
authored
chore(pkg-r): Use cli inline markup (#156)
* chore: use cli inline-markup * chore: test in parallel
1 parent f9ead22 commit 24d1c60

File tree

9 files changed

+61
-50
lines changed

9 files changed

+61
-50
lines changed

pkg-r/DESCRIPTION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ Suggests:
4141
Remotes:
4242
posit-dev/shinychat/pkg-r
4343
Config/testthat/edition: 3
44+
Config/testthat/parallel: true
4445
Encoding: UTF-8
4546
Roxygen: list(markdown = TRUE)
4647
RoxygenNote: 7.3.3

pkg-r/R/DataSource.R

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ DataSource <- R6::R6Class(
1919
#' @return A string describing the database type (e.g., "DuckDB", "SQLite")
2020
get_db_type = function() {
2121
cli::cli_abort(
22-
"get_db_type() must be implemented by subclass",
22+
"{.fn get_db_type} must be implemented by subclass",
2323
class = "not_implemented_error"
2424
)
2525
},
@@ -32,7 +32,7 @@ DataSource <- R6::R6Class(
3232
#' @return A string containing schema information formatted for LLM prompts
3333
get_schema = function(categorical_threshold = 20) {
3434
cli::cli_abort(
35-
"get_schema() must be implemented by subclass",
35+
"{.fn get_schema} must be implemented by subclass",
3636
class = "not_implemented_error"
3737
)
3838
},
@@ -44,7 +44,7 @@ DataSource <- R6::R6Class(
4444
#' @return A data frame containing query results
4545
execute_query = function(query) {
4646
cli::cli_abort(
47-
"execute_query() must be implemented by subclass",
47+
"{.fn execute_query} must be implemented by subclass",
4848
class = "not_implemented_error"
4949
)
5050
},
@@ -56,7 +56,7 @@ DataSource <- R6::R6Class(
5656
#' @return A data frame containing one row of results (or empty if no matches)
5757
test_query = function(query) {
5858
cli::cli_abort(
59-
"test_query() must be implemented by subclass",
59+
"{.fn test_query} must be implemented by subclass",
6060
class = "not_implemented_error"
6161
)
6262
},
@@ -67,7 +67,7 @@ DataSource <- R6::R6Class(
6767
#' @return A data frame containing all data from the table
6868
get_data = function() {
6969
cli::cli_abort(
70-
"get_data() must be implemented by subclass",
70+
"{.fn get_data} must be implemented by subclass",
7171
class = "not_implemented_error"
7272
)
7373
},
@@ -78,7 +78,7 @@ DataSource <- R6::R6Class(
7878
#' @return NULL (invisibly)
7979
cleanup = function() {
8080
cli::cli_abort(
81-
"cleanup() must be implemented by subclass",
81+
"{.fn cleanup} must be implemented by subclass",
8282
class = "not_implemented_error"
8383
)
8484
}
@@ -269,7 +269,9 @@ DBISource <- R6::R6Class(
269269
#' }
270270
initialize = function(conn, table_name) {
271271
if (!inherits(conn, "DBIConnection")) {
272-
cli::cli_abort("`conn` must be a DBI connection")
272+
cli::cli_abort(
273+
"{.arg conn} must be a {.cls DBIConnection}, not {.obj_type_friendly {conn}}"
274+
)
273275
}
274276

275277
# Validate table_name type
@@ -279,15 +281,15 @@ DBISource <- R6::R6Class(
279281
# Character string - keep as is
280282
} else {
281283
cli::cli_abort(
282-
"`table_name` must be a single character string or a DBI::Id object"
284+
"{.arg table_name} must be a single character string or a {.fn DBI::Id} object"
283285
)
284286
}
285287

286288
# Check if table exists
287289
if (!DBI::dbExistsTable(conn, table_name)) {
288290
cli::cli_abort(c(
289-
"Table {DBI::dbQuoteIdentifier(x, table_name)} not found in database.",
290-
"i" = "If you're using a table in a catalog or schema, pass a DBI::Id object to `table_name`"
291+
"Table {.val {DBI::dbQuoteIdentifier(conn, table_name)}} not found in database",
292+
"i" = "If you're using a table in a catalog or schema, pass a {.fn DBI::Id} object to {.arg table_name}"
291293
))
292294
}
293295

@@ -587,7 +589,9 @@ assemble_system_prompt <- function(
587589
prompt_template = NULL
588590
) {
589591
if (!is_data_source(source)) {
590-
cli::cli_abort("`source` must be a DataSource object")
592+
cli::cli_abort(
593+
"{.arg source} must be a {.cls DataSource} object, not {.obj_type_friendly {source}}"
594+
)
591595
}
592596

593597
prompt_text <- read_text(

pkg-r/R/QueryChat.R

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ QueryChat <- R6::R6Class(
470470
) {
471471
if (is.null(session)) {
472472
cli::cli_abort(
473-
"$server() must be called within a Shiny server function."
473+
"{.fn $server} must be called within a Shiny server function"
474474
)
475475
}
476476

@@ -706,10 +706,6 @@ normalize_data_source <- function(data_source, table_name) {
706706
}
707707

708708
cli::cli_abort(
709-
paste0(
710-
"`data_source` must be a DataSource, data.frame, or DBIConnection. ",
711-
"Got: ",
712-
class(data_source)[1]
713-
)
709+
"{.arg data_source} must be a {.cls DataSource}, {.cls data.frame}, or {.cls DBIConnection}, not {.obj_type_friendly {data_source}}."
714710
)
715711
}

pkg-r/R/querychat_module.R

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,9 @@ mod_server <- function(
7272
greeting
7373
} else {
7474
cli::cli_warn(c(
75-
"No greeting provided to `QueryChat()`. Using the LLM `client` to generate one now.",
76-
"i" = "For faster startup, lower cost, and determinism, consider providing a greeting to `QueryChat()` and `$generate_greeting()` to generate one beforehand."
75+
"No {.arg greeting} provided to {.fn QueryChat}. Using the LLM {.arg client} to generate one now.",
76+
"i" = "For faster startup, lower cost, and determinism, consider providing a {.arg greeting} to {.fn QueryChat}.",
77+
"i" = "You can use your {.help querychat::QueryChat} object's {.fn $generate_greeting} method to generate a greeting."
7778
))
7879
chat$stream_async(GREETING_PROMPT)
7980
}

pkg-r/R/querychat_tools.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ querychat_tool_details_option <- function() {
126126

127127
if (!setting %in% valid_settings) {
128128
cli::cli_warn(c(
129-
"Invalid value for {.code querychat.tool_details} or {.envvar QUERYCHAT_TOOL_DETAILS}: {.val {setting}}",
129+
"Invalid value for {.code querychat.tool_details} option or {.envvar QUERYCHAT_TOOL_DETAILS} environment variable: {.val {setting}}",
130130
"i" = "Must be one of: {.or {.val {valid_settings}}}"
131131
))
132132
return(NULL)

pkg-r/R/utils-check.R

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,11 @@ check_sql_table_name <- function(
3434
# Then validate SQL table name pattern
3535
if (!grepl("^[a-zA-Z][a-zA-Z0-9_]*$", x)) {
3636
cli::cli_abort(
37-
"{.arg {arg}} must be a valid SQL table name.",
38-
"i" = "Table names must begin with a letter and contain only letters, numbers, and underscores.",
39-
"x" = "You provided: {.val {x}}",
37+
c(
38+
"{.arg {arg}} must be a valid SQL table name",
39+
"i" = "Table names must begin with a letter and contain only letters, numbers, and underscores",
40+
"x" = "You provided: {.val {x}}"
41+
),
4042
call = call
4143
)
4244
}

pkg-r/R/utils-ellmer.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ as_querychat_client <- function(client = NULL) {
3737

3838
if (!inherits(client, "Chat")) {
3939
cli::cli_abort(
40-
"`client` must be an {ellmer} Chat object or a function that returns one."
40+
"{.arg client} must be an {.pkg ellmer} {.cls Chat} object or a function that returns one"
4141
)
4242
}
4343

pkg-r/tests/testthat/_snaps/DataSource.md

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,47 +4,47 @@
44
base_source$get_db_type()
55
Condition
66
Error in `base_source$get_db_type()`:
7-
! get_db_type() must be implemented by subclass
7+
! `get_db_type()` must be implemented by subclass
88

99
---
1010

1111
Code
1212
base_source$get_schema()
1313
Condition
1414
Error in `base_source$get_schema()`:
15-
! get_schema() must be implemented by subclass
15+
! `get_schema()` must be implemented by subclass
1616

1717
---
1818

1919
Code
2020
base_source$execute_query("SELECT * FROM test")
2121
Condition
2222
Error in `base_source$execute_query()`:
23-
! execute_query() must be implemented by subclass
23+
! `execute_query()` must be implemented by subclass
2424

2525
---
2626

2727
Code
2828
base_source$test_query("SELECT * FROM test LIMIT 1")
2929
Condition
3030
Error in `base_source$test_query()`:
31-
! test_query() must be implemented by subclass
31+
! `test_query()` must be implemented by subclass
3232

3333
---
3434

3535
Code
3636
base_source$get_data()
3737
Condition
3838
Error in `base_source$get_data()`:
39-
! get_data() must be implemented by subclass
39+
! `get_data()` must be implemented by subclass
4040

4141
---
4242

4343
Code
4444
base_source$cleanup()
4545
Condition
4646
Error in `base_source$cleanup()`:
47-
! cleanup() must be implemented by subclass
47+
! `cleanup()` must be implemented by subclass
4848

4949
# DataFrameSource$new() / errors with non-data.frame input
5050

@@ -76,22 +76,30 @@
7676
DataFrameSource$new(test_df, "123_invalid")
7777
Condition
7878
Error in `initialize()`:
79-
! `table_name` must be a valid SQL table name.
79+
! `table_name` must be a valid SQL table name
80+
i Table names must begin with a letter and contain only letters, numbers, and underscores
81+
x You provided: "123_invalid"
8082
Code
8183
DataFrameSource$new(test_df, "table-name")
8284
Condition
8385
Error in `initialize()`:
84-
! `table_name` must be a valid SQL table name.
86+
! `table_name` must be a valid SQL table name
87+
i Table names must begin with a letter and contain only letters, numbers, and underscores
88+
x You provided: "table-name"
8589
Code
8690
DataFrameSource$new(test_df, "table name")
8791
Condition
8892
Error in `initialize()`:
89-
! `table_name` must be a valid SQL table name.
93+
! `table_name` must be a valid SQL table name
94+
i Table names must begin with a letter and contain only letters, numbers, and underscores
95+
x You provided: "table name"
9096
Code
9197
DataFrameSource$new(test_df, "")
9298
Condition
9399
Error in `initialize()`:
94-
! `table_name` must be a valid SQL table name.
100+
! `table_name` must be a valid SQL table name
101+
i Table names must begin with a letter and contain only letters, numbers, and underscores
102+
x You provided: ""
95103
Code
96104
DataFrameSource$new(test_df, NULL)
97105
Condition
@@ -104,71 +112,70 @@
104112
DBISource$new(list(fake = "connection"), "test_table")
105113
Condition
106114
Error in `initialize()`:
107-
! `conn` must be a DBI connection
115+
! `conn` must be a <DBIConnection>, not a list
108116

109117
---
110118

111119
Code
112120
DBISource$new(NULL, "test_table")
113121
Condition
114122
Error in `initialize()`:
115-
! `conn` must be a DBI connection
123+
! `conn` must be a <DBIConnection>, not NULL
116124

117125
---
118126

119127
Code
120128
DBISource$new("not a connection", "test_table")
121129
Condition
122130
Error in `initialize()`:
123-
! `conn` must be a DBI connection
131+
! `conn` must be a <DBIConnection>, not a string
124132

125133
# DBISource$new() / errors with invalid table_name types
126134

127135
Code
128136
DBISource$new(db$conn, 123)
129137
Condition
130138
Error in `initialize()`:
131-
! `table_name` must be a single character string or a DBI::Id object
139+
! `table_name` must be a single character string or a `DBI::Id()` object
132140

133141
---
134142

135143
Code
136144
DBISource$new(db$conn, c("table1", "table2"))
137145
Condition
138146
Error in `initialize()`:
139-
! `table_name` must be a single character string or a DBI::Id object
147+
! `table_name` must be a single character string or a `DBI::Id()` object
140148

141149
---
142150

143151
Code
144152
DBISource$new(db$conn, list(name = "table"))
145153
Condition
146154
Error in `initialize()`:
147-
! `table_name` must be a single character string or a DBI::Id object
155+
! `table_name` must be a single character string or a `DBI::Id()` object
148156

149157
# DBISource$new() / errors when table does not exist
150158

151159
Code
152160
DBISource$new(db$conn, "non_existent_table")
153161
Condition
154-
Error:
155-
! ! Could not evaluate cli `{}` expression: `DBI::dbQuoteIdent...`.
156-
Caused by error in `h(simpleError(msg, call))`:
157-
! error in evaluating the argument 'conn' in selecting a method for function 'dbQuoteIdentifier': object 'x' not found
162+
Error in `initialize()`:
163+
! Table "`non_existent_table`" not found in database
164+
i If you're using a table in a catalog or schema, pass a `DBI::Id()` object to `table_name`
158165

159166
# assemble_system_prompt() / errors with non-DataSource input
160167

161168
Code
162169
assemble_system_prompt(list(not = "a data source"), data_description = "Test")
163170
Condition
164171
Error in `assemble_system_prompt()`:
165-
! `source` must be a DataSource object
172+
! `source` must be a <DataSource> object, not a list
166173

167174
---
168175

169176
Code
170177
assemble_system_prompt(data.frame(x = 1:3), data_description = "Test")
171178
Condition
172179
Error in `assemble_system_prompt()`:
173-
! `source` must be a DataSource object
180+
! `source` must be a <DataSource> object, not a data frame
174181

pkg-r/tests/testthat/_snaps/QueryChat.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,29 +36,29 @@
3636
qc$server()
3737
Condition
3838
Error in `qc$server()`:
39-
! $server() must be called within a Shiny server function.
39+
! `$server()` must be called within a Shiny server function
4040

4141
# normalize_data_source() / errors with invalid data source types
4242

4343
Code
4444
normalize_data_source("not_a_data_source", "table_name")
4545
Condition
4646
Error in `normalize_data_source()`:
47-
! `data_source` must be a DataSource, data.frame, or DBIConnection. Got: character
47+
! `data_source` must be a <DataSource>, <data.frame>, or <DBIConnection>, not a string.
4848

4949
---
5050

5151
Code
5252
normalize_data_source(list(a = 1, b = 2), "table_name")
5353
Condition
5454
Error in `normalize_data_source()`:
55-
! `data_source` must be a DataSource, data.frame, or DBIConnection. Got: list
55+
! `data_source` must be a <DataSource>, <data.frame>, or <DBIConnection>, not a list.
5656

5757
---
5858

5959
Code
6060
normalize_data_source(NULL, "table_name")
6161
Condition
6262
Error in `normalize_data_source()`:
63-
! `data_source` must be a DataSource, data.frame, or DBIConnection. Got: NULL
63+
! `data_source` must be a <DataSource>, <data.frame>, or <DBIConnection>, not NULL.
6464

0 commit comments

Comments
 (0)