Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
2 changes: 2 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@
^data-raw$
^examples$
^\.github$
^\.positai$
^\.claude$
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
.Ruserdata
node_modules
*.Rproj
.positai
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

export(JS)
export(aes)
export(bargpu)
export(chartgpu)
export(chartgpuOutput)
export(chartgpu_axis_x)
Expand All @@ -12,6 +13,8 @@ export(chartgpu_theme)
export(chartgpu_theme_dark)
export(chartgpu_theme_light)
export(chartgpu_zoom)
export(donutgpu)
export(piegpu)
export(renderChartgpu)
export(scattergpu)
importFrom(ggplot2,aes)
Expand Down
20 changes: 20 additions & 0 deletions R/bar.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#' Bar Chart with ChartGPU
#'
#' @param data A `data.frame`.
#' @inheritParams chartgpu
#' @inheritParams htmlwidgets::createWidget
#'
#' @returns A [chartgpu()] `htmlwidget` object.
#' @export
#'
#' @example examples/chartgpu_bar.R
bargpu <- function(data, mapping = NULL, width = NULL, height = NULL, elementId = NULL) {
chartgpu(
data = data,
mapping = mapping,
type = "bar",
width = width,
height = height,
elementId = elementId
)
}
11 changes: 9 additions & 2 deletions R/chartgpu.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#' @param mapping Optional mapping of variables to use if `data` is a `data.frame`
#' @param type Type of chart, if `data` is a `data.frame`.
#' @param ... Additional parameters.
#' @param group Group ID to link multiple charts together.
#' @param sync_zoom Whether to sync zoom/pan across charts in the same group.
#' @inheritParams htmlwidgets::createWidget
#'
#' @return A [chartgpu()] `htmlwidget` object.
Expand All @@ -16,7 +18,7 @@
#'
#' @export
#' @example examples/chartgpu.R
chartgpu <- function(data = NULL, mapping = NULL, type = NULL, ..., width = NULL, height = NULL, elementId = NULL) {
chartgpu <- function(data = NULL, mapping = NULL, type = NULL, ..., group = NULL, sync_zoom = FALSE, width = NULL, height = NULL, elementId = NULL) {

if (inherits(data, "data.frame")) {
x <- list(
Expand All @@ -35,7 +37,12 @@ chartgpu <- function(data = NULL, mapping = NULL, type = NULL, ..., width = NULL
options = c(data, list(...))
)
}


if (!is.null(group)) {
x$syncGroup <- group
x$syncZoom <- isTRUE(sync_zoom)
}

attr(x, "TOJSON_ARGS") <- list(dataframe = "rows")

createWidget(
Expand Down
97 changes: 97 additions & 0 deletions R/pie.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#' Pie Chart with ChartGPU
#'
#' @param data A `data.frame`.
#' @param name Series name shown in the tooltip.
#' @param radius Length-2 vector `c(inner, outer)`, e.g. `c(0, "72%")`.
#' @param center Length-2 vector for the chart centre, e.g. `c("50%", "50%")`.
#' @param start_angle Starting angle in degrees (default 90 = top).
#' @inheritParams chartgpu
#' @inheritParams htmlwidgets::createWidget
#'
#' @returns A [chartgpu()] `htmlwidget` object.
#' @export
#'
#' @example examples/chartgpu_pie.R
piegpu <- function(data,
mapping = NULL,
name = NULL,
radius = c(0, "72%"),
center = c("50%", "50%"),
start_angle = 90,
width = NULL,
height = NULL,
elementId = NULL) {

if (is.null(mapping)) {
nms <- colnames(data)
mapping <- list(
label = nms[1],
value = nms[2],
color = if (length(nms) >= 3) nms[3] else NULL
)
}

slices <- lapply(seq_len(nrow(data)), function(i) {
s <- list(name = as.character(data[[mapping$label]][i]),
value = data[[mapping$value]][i])
if (!is.null(mapping$color) && !is.na(data[[mapping$color]][i]))
s$color <- data[[mapping$color]][i]
s
})

serie <- dropNulls(list(
type = "pie",
name = name,
data = slices,
radius = as.list(radius),
center = as.list(center),
startAngle = start_angle
))

chartgpu(
data = list(
series = list(serie),
xAxis = list(type = "value", min = 0, max = 1, tickLength = 0, name = ""),
yAxis = list(type = "value", min = 0, max = 1, tickLength = 0, name = "")
),
width = width,
height = height,
elementId = elementId
)
}

#' Donut Chart with ChartGPU
#'
#' @param data A `data.frame`.
#' @param name Series name shown in the tooltip.
#' @param radius Length-2 vector `c(inner, outer)`, e.g. `c("40%", "72%")`.
#' @param center Length-2 vector for the chart centre, e.g. `c("50%", "50%")`.
#' @param start_angle Starting angle in degrees (default 90 = top).
#' @inheritParams chartgpu
#' @inheritParams htmlwidgets::createWidget
#'
#' @returns A [chartgpu()] `htmlwidget` object.
#' @export
#'
#' @example examples/chartgpu_pie.R
donutgpu <- function(data,
mapping = NULL,
name = NULL,
radius = c("40%", "72%"),
center = c("50%", "50%"),
start_angle = 90,
width = NULL,
height = NULL,
elementId = NULL) {
piegpu(
data = data,
mapping = mapping,
name = name,
radius = radius,
center = center,
start_angle = start_angle,
width = width,
height = height,
elementId = elementId
)
}
Binary file added examples/.DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions examples/chartgpu.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,4 @@ chartgpu(list(
dataZoom = list(list(type = "inside"), list(type = "slider")),
theme = "light"
))

6 changes: 6 additions & 0 deletions examples/chartgpu_bar.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
library(rchartgpu)

bargpu(
ggplot2::economics[, c("date", "unemploy")],
height = "400px"
)
23 changes: 23 additions & 0 deletions examples/chartgpu_connect_charts.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
c1 <- chartgpu(
ggplot2::economics[, c("date", "psavert")],
group = "g1",
sync_zoom = TRUE,
height = "300px",
dataZoom = list(
list(type = "inside"),
list(type = "slider")
)
)

c2 <- chartgpu(
ggplot2::economics[, c("date", "uempmed")],
group = "g1",
sync_zoom = TRUE,
height = "300px",
dataZoom = list(
list(type = "inside"),
list(type = "slider")
)
)

htmltools::browsable(htmltools::tagList(c1, c2))
13 changes: 13 additions & 0 deletions examples/chartgpu_pie.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
library(rchartgpu)

gpu_share <- data.frame(
label = c("Compute", "Memory", "Raster", "Upload", "Sync", "Other"),
value = c(42, 30, 18, 12, 9, 6),
color = c("#00E5FF", "#FF2D95", "#B026FF", "#00F5A0", "#FFD300", "#FF6B00")
)

# Pie
piegpu(gpu_share, name = "GPU budget")

# Donut
donutgpu(gpu_share, name = "GPU budget")
51 changes: 51 additions & 0 deletions examples/connect_charts_shiny.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
library(shiny)
library(rchartgpu)


ui <- fluidPage(

titlePanel("Test connectCharts in shiny"),

selectInput("var1",
"Variable 1:",
choices = names(ggplot2::economics[-1]),
selected = "psavert"),
selectInput("var2",
"Variable 2:",
choices = names(ggplot2::economics[-1]),
selected = "uempmed"),
chartgpuOutput("chart1", height = "290px"),
br(),
chartgpuOutput("chart2", height = "290px")
)


server <- function(input, output) {

output$chart1 <- renderChartgpu({
c1 <- chartgpu(
ggplot2::economics[, c("date", input$var1)],
group = "g1",
sync_zoom = TRUE,
dataZoom = list(
list(type = "inside"),
list(type = "slider")
)
)})

output$chart2 <- renderChartgpu({
c2 <- chartgpu(
ggplot2::economics[, c("date", input$var2)],
group = "g1",
sync_zoom = TRUE,
dataZoom = list(
list(type = "inside"),
list(type = "slider")
)
)

})
}

if (interactive())
shinyApp(ui, server)
24 changes: 19 additions & 5 deletions examples/shiny.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@

library(shiny)
library(rchartgpu)

gpu_share <- data.frame(
label = c("Compute", "Memory", "Raster", "Upload", "Sync", "Other"),
value = c(42, 30, 18, 12, 9, 6),
color = c("#00E5FF", "#FF2D95", "#B026FF", "#00F5A0", "#FFD300", "#FF6B00")
)

ui <- fluidPage(
fluidRow(
column(
Expand All @@ -12,19 +17,28 @@ ui <- fluidPage(
label = "Variable:",
choices = names(ggplot2::economics)[-1]
),
chartgpuOutput(outputId = "chart", height = "500px")
chartgpuOutput(outputId = "chart", height = "500px"),
chartgpuOutput(outputId = "pie", height = "350px"),
br(),
chartgpuOutput(outputId = "donut", height = "350px")
)
)
)

server <- function(input, output, session) {

output$chart <- renderChartgpu({
chartgpu(ggplot2::economics[, c("date", input$variable)]) |>
chartgpu_theme_light()
})


output$pie <- renderChartgpu({
piegpu(gpu_share, name = "GPU budget")
})

output$donut <- renderChartgpu({
donutgpu(gpu_share, name = "GPU budget")
})
}

if (interactive())
shinyApp(ui, server)
shinyApp(ui, server)
Binary file added inst/.DS_Store
Binary file not shown.
2 changes: 1 addition & 1 deletion inst/htmlwidgets/chartgpu.js

Large diffs are not rendered by default.

39 changes: 39 additions & 0 deletions man/bargpu.Rd

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

Loading
Loading