-
Notifications
You must be signed in to change notification settings - Fork 16
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
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e1fe6cb
Uploads data sources, initial shiny app.
stanleyo03 ba511a7
Completed layout and features for app
stanleyo03 b78b83e
Finalized app changes
stanleyo03 752b90e
updated Dockerfile, removed data folder, included app in doc/apps/sh…
stanleyo03 2523c18
Pull from main
stanleyo03 f2b88e6
Fixed image, deleted data files
stanleyo03 2c35224
Removed personal database parameters.
stanleyo03 2930dce
missing closed parenthesis
stanleyo03 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
::: | ||
|
||
:::: |
This file contains 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
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"] |
This file contains 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
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. | ||
|
||
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. | ||
|
This file contains 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
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) |
This file contains 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
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.
This file contains 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
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') |
This file contains 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
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) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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