Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shiny-R PostgresSQL Example #252

Merged
merged 8 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from 7 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
6 changes: 5 additions & 1 deletion doc/apps/shiny-r.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,14 @@ If you need help installing certain libraries, email us at [[email protected]]
:link: https://github.com/ploomber/doc/tree/main/examples/shiny-r/docker-based
:::

:::{grid-item-card} Data visualization
:::{grid-item-card} Data Visualization
:link: https://github.com/ploomber/doc/tree/main/examples/shiny-r/shiny-r-data-visualization
![](https://github.com/ploomber/doc/raw/main/examples/shiny-r/shiny-r-data-visualization/screenshot.webp)
:::

:::{grid-item-card} App Connected to Postgres
:link: https://github.com/ploomber/doc/tree/main/examples/shiny-r/connect-shiny-to-postgresql
![](https://github.com/ploomber/doc/tree/main/examples/shiny-r/connect-shiny-to-postgresql/screenshot.png)
:::

::::
14 changes: 14 additions & 0 deletions examples/shiny-r/connect-shiny-to-postgresql/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
FROM rocker/shiny

WORKDIR /app

RUN apt-get update
RUN apt-get install -y libpq-dev

COPY install.R /app/

RUN Rscript install.R

COPY . /app

ENTRYPOINT ["Rscript", "startApp.R"]
10 changes: 10 additions & 0 deletions examples/shiny-r/connect-shiny-to-postgresql/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# R Shiny App Connected to PostgreSQL Database

Interactive R Shiny dashboard, connected to PostgreSQL database.

![](screenshot.png)

To use the app, fill in the database parameters with the parameters from your PostgreSQL database.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add the steps here or add a link to the blog once it's merged


NOTE: The data used in this app is not present in this repository, to obtain the data, go to this [link](https://archive.ics.uci.edu/dataset/320/student+performance), download and unzip the data folder.

93 changes: 93 additions & 0 deletions examples/shiny-r/connect-shiny-to-postgresql/app.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
library(shiny)
library(DBI)
library(bslib)
library(dplyr)
library(ggplot2
db <- "[DATABASE NAME]"
db_host <- "[YOUR HOST ADDRESS]"
db_port <- "[PORT NUMBER]"
db_user <- "[DATABASE USERNAME]"
db_pass <- "[DATABASE PASSWORD]"

conn <- dbConnect(
RPostgres::Postgres(),
dbname = db,
host = db_host,
port = db_port,
user = db_user,
password = db_pass
)

tables <- dbListTables(conn)

ui <- page_sidebar(
title = "PostgreSQL with R Shiny Demo",

sidebar = sidebar(
accordion(
accordion_panel(
"Main Controls",
selectInput(inputId = "table",
label = "Select Table in Database",
choices = tables,
selected = tables[]),
accordion_panel(
"Histogram Controls",
selectInput(inputId = "hist_x",
label = "Variable",
choices = NULL)
),
accordion_panel(
"Scatterplot Controls",
selectInput(inputId = "scatter_x",
label = "X Variable",
choices = NULL),
selectInput(inputId = "scatter_y",
label = "Y Variable",
choices = NULL)
)
)
)
),
plotOutput(outputId = "hist"),
plotOutput(outputId = "scatter")
)

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

quant_vars <- reactive({
query <- sprintf("SELECT * FROM %s LIMIT 1", input$table)
df <- dbGetQuery(conn, query)
names(dplyr::select_if(df, is.numeric))
})

observe({
updateSelectInput(session, "hist_x", choices = quant_vars())
updateSelectInput(session, "scatter_x", choices = quant_vars())
updateSelectInput(session, "scatter_y", choices = quant_vars())
})

output$hist <- renderPlot({
query <- sprintf("SELECT * FROM %s", input$table)
df <- dbGetQuery(conn, query)

ggplot(df, aes_string(x = input$hist_x)) +
geom_histogram(fill = "turquoise") +
labs(title = sprintf("Distribution of %s", input$hist_x),
x = input$hist_x,
y = "Frequency")
})

output$scatter <- renderPlot({
query <- sprintf("SELECT * FROM %s", input$table)
df <- dbGetQuery(conn, query)

ggplot(df, aes_string(x = input$scatter_x, y = input$scatter_y)) +
geom_point(alpha = 0.5) +
labs(title = sprintf("%s vs %s", input$scatter_y, input$scatter_x),
x = input$scatter_x,
y = input$scatter_y)
})
}

shinyApp(ui = ui, server = server)
6 changes: 6 additions & 0 deletions examples/shiny-r/connect-shiny-to-postgresql/install.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
install.packages("shiny")
install.packages("RPostgres")
install.packages("DBI")
install.packages("bslib")
install.packages("dplyr")
install.packages("ggplot2")
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions examples/shiny-r/connect-shiny-to-postgresql/startApp.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
library(shiny)
options(shiny.host = '0.0.0.0')
options(shiny.port = 80)
runApp('app.R')
26 changes: 26 additions & 0 deletions examples/shiny-r/connect-shiny-to-postgresql/upload.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
library(DBI)

db <- "[DATABASE NAME]"
db_host <- "[YOUR HOST ADDRESS]"
db_port <- "[PORT NUMBER]"
db_user <- "[DATABASE USERNAME]"
db_pass <- "[DATABASE PASSWORD]"

conn <- dbConnect(
RPostgres::Postgres(),
dbname = db,
host = db_host,
port = db_port,
user = db_user,
password = db_pass
)

math <- read.csv("data/student-mat.csv", sep=';')
por <- read.csv("data/student-por.csv", sep=';')

print(conn)

dbWriteTable(conn, "math", math, overwrite=TRUE)
dbWriteTable(conn, "portuguese", por, overwrite=TRUE)

dbDisconnect(conn)