Last active
June 5, 2025 16:46
-
-
Save jrosell/45da30cfba03510975b7cc97260539ed to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(shiny) | |
library(dplyr) | |
library(ggplot2) | |
library(bslib) | |
set.seed(42) | |
data <- data.frame( | |
category = c("A", "B", "C", "D", "E"), | |
value = sample(10:100, 5) | |
) | |
ui <- page_fluid( | |
h1("dplyr and ggplot2 Bar Chart in Shiny"), | |
card( | |
card_header("Sample Bar Chart"), | |
plotOutput("bar_chart", height = "400px") | |
), | |
card( | |
card_header("Data Table"), | |
dataTableOutput("data_table") | |
) | |
) | |
server <- function(input, output, session) { | |
output$bar_chart <- renderPlot({ | |
plot_data <- data %>% | |
mutate(category = factor(category, levels = category)) | |
my_colors <- c("#1f77b4", "#ff7f0e", "#2ca02c", "#d62728", "#9467bd") | |
ggplot(plot_data, aes(x = category, y = value, fill = category)) + | |
geom_bar(stat = "identity", width = 0.7) + | |
geom_text(aes(label = value), vjust = -0.5, size = 4) + | |
labs( | |
title = "Bar Chart using dplyr and ggplot2", | |
x = "Category", | |
y = "Value" | |
) + | |
scale_fill_manual(values = my_colors) + | |
theme_minimal() + | |
theme( | |
plot.title = element_text(size = 16, face = "bold"), | |
axis.title = element_text(size = 12), | |
legend.position = "none", | |
panel.grid.major.x = element_blank(), | |
panel.grid.minor.x = element_blank() | |
) | |
}) | |
output$data_table <- renderDataTable({ | |
data | |
}) | |
} | |
shinyApp(ui, server) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from shiny import App, ui, render | |
import polars as pl | |
import altair as alt | |
import numpy as np | |
import json | |
alt.renderers.enable('default') | |
alt.data_transformers.disable_max_rows() | |
np.random.seed(42) | |
data = pl.DataFrame({ | |
"category": ["A", "B", "C", "D", "E"], | |
"value": np.random.randint(10, 100, 5) | |
}) | |
app_ui = ui.page_fluid( | |
ui.h1("Polars and Altair Bar Chart in Shiny"), | |
ui.card( | |
ui.card_header("Sample Bar Chart"), | |
ui.output_ui("bar_chart"), | |
), | |
ui.card( | |
ui.card_header("Data Table"), | |
ui.output_data_frame("data_table") | |
), | |
# Include required Vega-Lite libraries directly in the page | |
ui.tags.script(src="https://cdn.jsdelivr.net/npm/vega@5"), | |
ui.tags.script(src="https://cdn.jsdelivr.net/npm/vega-lite@5"), | |
ui.tags.script(src="https://cdn.jsdelivr.net/npm/vega-embed@6") | |
) | |
def server(input, output, session): | |
@render.ui | |
def bar_chart(): | |
chart = alt.Chart(alt.Data(values=data.to_dicts())).mark_bar().encode( | |
x=alt.X('category:N', title='Category'), | |
y=alt.Y('value:Q', title='Value'), | |
color=alt.Color('category:N', legend=None) | |
).properties( | |
width=400, | |
height=300, | |
title='Bar Chart using Polars and Altair' | |
) | |
chart_spec = json.loads(chart.to_json()) | |
div_id = "vis-chart" | |
html = f""" | |
<div id="{div_id}" style="width: 100%; max-width: 800px;"></div> | |
<script type="text/javascript"> | |
var spec = {json.dumps(chart_spec)}; | |
vegaEmbed('#{div_id}', spec, {{renderer: 'svg'}}) | |
.then(function(result) {{ | |
console.log('Chart rendered successfully'); | |
}}) | |
.catch(console.error); | |
</script> | |
""" | |
return ui.HTML(html) | |
@render.data_frame | |
def data_table(): | |
return data | |
app = App(app_ui, server) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment