Skip to content

Commit 787eeed

Browse files
feat: add functions to convert content lists to data frames (#470)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent 690bb8c commit 787eeed

File tree

6 files changed

+144
-3
lines changed

6 files changed

+144
-3
lines changed

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ S3method("[",connect_tag_tree)
55
S3method("[[",connect_tag_tree)
66
S3method(api_build,op_base_connect)
77
S3method(api_build,op_head)
8+
S3method(as.data.frame,connect_content_list)
89
S3method(as.data.frame,connect_integration_list)
910
S3method(as.data.frame,connect_list_hits)
1011
S3method(as.data.frame,tbl_connect)
12+
S3method(as_tibble,connect_content_list)
1113
S3method(as_tibble,connect_integration_list)
1214
S3method(as_tibble,connect_list_hits)
1315
S3method(connect_vars,op_base)

R/content.R

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1522,7 +1522,7 @@ get_content_packages <- function(content) {
15221522
#' usually not what you want.
15231523
#'
15241524
#' @return
1525-
#' A list of [Content] objects.
1525+
#' A list of [Content] objects, of class "connect_content_list"
15261526
#'
15271527
#' @details
15281528
#' Please see https://docs.posit.co/connect/api/#get-/v1/search/content for more
@@ -1566,9 +1566,12 @@ search_content <- function(
15661566
limit = limit
15671567
)
15681568

1569-
purrr::map(res, function(x) {
1569+
content_list <- purrr::map(res, function(x) {
15701570
Content$new(client, x)
15711571
})
1572+
1573+
class(content_list) <- c("connect_content_list", class(content_list))
1574+
content_list
15721575
}
15731576

15741577
.search_content <- function(
@@ -1591,3 +1594,45 @@ search_content <- function(
15911594

15921595
client$GET(path, query = query)
15931596
}
1597+
1598+
#' Convert content list to a data frame
1599+
#'
1600+
#' @description
1601+
#' Converts a list returned by [search_content()] into a data frame.
1602+
#'
1603+
#' @param x A `connect_content_list` object (from [search_content()]).
1604+
#' @param row.names Passed to [base::as.data.frame()].
1605+
#' @param optional Passed to [base::as.data.frame()].
1606+
#' @param ... Passed to [base::as.data.frame()].
1607+
#'
1608+
#' @return A `data.frame` with one row per content item.
1609+
#' @export
1610+
as.data.frame.connect_content_list <- function(
1611+
x,
1612+
row.names = NULL, # nolint
1613+
optional = FALSE,
1614+
...
1615+
) {
1616+
content_tbl <- as_tibble(x)
1617+
as.data.frame(
1618+
content_tbl,
1619+
row.names = row.names,
1620+
optional = optional,
1621+
...
1622+
)
1623+
}
1624+
1625+
#' Convert integration list to a tibble
1626+
#'
1627+
#' @description
1628+
#' Converts a list returned by [search_content()] to a tibble.
1629+
#'
1630+
#' @param x A `connect_content_list` object.
1631+
#' @param ... Unused.
1632+
#'
1633+
#' @return A tibble with one row per content item.
1634+
#' @export
1635+
as_tibble.connect_content_list <- function(x, ...) {
1636+
content_data <- purrr::map(x, "content")
1637+
parse_connectapi_typed(content_data, connectapi_ptypes$content)
1638+
}

man/as.data.frame.connect_content_list.Rd

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

man/as_tibble.connect_content_list.Rd

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

man/search_content.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-content.R

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,58 @@ with_mock_dir("2025.09.0", {
536536
)
537537
)
538538
})
539+
540+
test_that("search_content() can be converted to a data frame correctly", {
541+
content_df <- search_content(client, q = "sea bream") |>
542+
as_tibble()
543+
expect_named(
544+
content_df,
545+
c(
546+
"guid",
547+
"name",
548+
"title",
549+
"description",
550+
"access_type",
551+
"connection_timeout",
552+
"read_timeout",
553+
"init_timeout",
554+
"idle_timeout",
555+
"max_processes",
556+
"min_processes",
557+
"max_conns_per_process",
558+
"load_factor",
559+
"created_time",
560+
"last_deployed_time",
561+
"bundle_id",
562+
"app_mode",
563+
"content_category",
564+
"parameterized",
565+
"cluster_name",
566+
"image_name",
567+
"r_version",
568+
"py_version",
569+
"quarto_version",
570+
"run_as",
571+
"run_as_current_user",
572+
"owner_guid",
573+
"content_url",
574+
"dashboard_url",
575+
"app_role",
576+
"vanity_url",
577+
"id",
578+
"owner",
579+
"tags"
580+
)
581+
)
582+
expect_equal(
583+
content_df$title,
584+
c("sea bream report", "sea bream dashboard")
585+
)
586+
expect_equal(
587+
content_df$guid,
588+
c("c9f68287", "53032a0e")
589+
)
590+
})
539591
})
540592

541593
test_that("content search errors on Connect < 2024.04.0", {

0 commit comments

Comments
 (0)