I have created a shiny app connected to a Mysql database running on my Mac OS Catalina. When I run the app in Rstudio it works fine. After doing it locally I will migrate the app to the cloud on a Linux Ubuntu.
When I create a docker image of this app - just the app, not the mysql db - and I run the container, accessing the app on a browser (localhost), it is not connecting to the database.
What is the correct configuration of my app.R or dockerfile to connect to a external mysql database ?
The files I am using are bellow. Appreciate any help.
===============APP.R========================================
## libraries
library(tidyverse)
library(shiny)
library(shinydashboard)
library(ggplot2)
library(DBI)
#shiny dashboard UI
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(),
dashboardBody(
fluidPage(
plotOutput("plot1")
)
)
)
#shiny dashboard server
server <- function(input, output,session) {
#connection to database
orders <- dbConnect(RMySQL::MySQL(), user = 'root', password = '#####',
dbname = 'Archenar',host='localhost')
#check changes
rigel <- reactivePoll(5000, session,checkFunc = function() {
query2= "SELECT * FROM SENSOR"
rs = dbSendQuery(orders,query2)
dbFetch(rs)
},
valueFunc = function() {
query2= "SELECT * FROM SENSOR"
rs = dbSendQuery(orders,query2)
dbFetch(rs)
}
)
#make plot
output$plot1 <- renderPlot({
rigelp <- rigel() %>% as.data.frame()%>% group_by(category) %>%
summarize(v=sum(value1))
ggplot(rigelp,aes(x=category,y=v)) + geom_bar(stat = "identity")
})
}
shinyApp(ui, server)
=============Dockerfile========================================
# get shiny server plus tidyverse packages image
FROM rocker/shiny:latest
# system libraries of general use
RUN apt-get update && apt-get install -y \
sudo \
pandoc \
pandoc-citeproc \
libcurl4-gnutls-dev \
libcairo2-dev \
libmariadbd-dev \
libxt-dev \
libssl-dev \
libssh2-1-dev
# install R packages required
# (change it depending on the packages you need)
RUN R -e "install.packages('shiny',repos='http://cran.rstudio.com/')"
RUN R -e "install.packages('tidyverse',repos='http://cran.rstudio.com/')"
RUN R -e "install.packages('shinydashboard',repos='http://cran.rstudio.com/')"
RUN R -e "install.packages('DBI',repos='http://cran.rstudio.com/')"
RUN R -e "install.packages('RMySQL',repos='http://cran.rstudio.com/')"
# Copy configuration files into the Docker image
COPY shiny-server.conf /etc/shiny-server/shiny-server.conf
COPY /app /srv/shiny-server/
RUN rm /srv/shiny-server/index.html
# Make the ShinyApp available at port 80
EXPOSE 3838
# Copy further configuration files into the Docker image
COPY shiny-server.sh /usr/bin/shiny-server.sh
RUN ["chmod", "+x", "/usr/bin/shiny-server.sh"]
CMD ["/usr/bin/shiny-server.sh"]
=========Shiny Server========================================
# Define the user we should use when spawning R Shiny processes
run_as shiny;
# Define a top-level server which will listen on a port
server {
# Instruct this server to listen on port 80.
listen 80;
# Define the location available at the base URL
location / {
# Run this location in 'site_dir' mode, which hosts the entire directory
# tree at '/srv/shiny-server'
site_dir /srv/shiny-server;
# Define where we should put the log files for this location
log_dir /var/log/shiny-server;
# Should we list the contents of a (non-Shiny-App) directory when the user
# visits the corresponding URL?
directory_index on;
}
}