基于Shiny的Flexdashboard实时部署咨询(Linux/低成本服务器)
Got it, let's walk through exactly how to get your Shiny flexdashboard app deployed on a Linux (or low-cost) server with real-time Excel data sync. I’ve tackled this scenario a few times, so here’s a practical, step-by-step guide:
You don’t need a fancy server—these budget-friendly options work perfectly:
- AWS EC2 t2.micro (free tier eligible for 12 months)
- DigitalOcean Droplet ($5/month, 1GB RAM/1 CPU)
- Linode Nanode ($5/month, similar specs to DO)
- Even a local Linux machine with a public IP (if you have one)
Start by getting your Linux server (Debian/Ubuntu recommended) ready:
- Update system packages:
sudo apt update && sudo apt upgrade -y - Install R and dev tools:
sudo apt install r-base r-base-dev -y - Install Shiny Server:
# Install gdebi first to handle dependencies sudo apt install gdebi-core -y # Download the latest Shiny Server package (check RStudio's site for updates) wget https://download3.rstudio.org/ubuntu-18.04/x86_64/shiny-server-1.5.20.1002-amd64.deb # Install the package sudo gdebi shiny-server-1.5.20.1002-amd64.deb - Install required R packages: Open an R session and run:
install.packages(c("flexdashboard", "shiny", "readxl", "reactivePoll"))
The key to syncing Excel changes is using reactivePoll to check for file modifications and reload data automatically. Here’s how to adjust your RMarkdown file:
--- title: "Real-Time Status Dashboard" output: flexdashboard::flex_dashboard runtime: shiny --- ```{r setup, include=FALSE} library(flexdashboard) library(shiny) library(readxl) # Function to check when the Excel file was last modified check_file_timestamp <- function() { file.info("data.xlsx")$mtime } # Function to read the Excel data load_excel_data <- function() { read_excel("data.xlsx") } # Create a reactive data object that updates on file changes live_data <- reactivePoll( intervalMillis = 5000, # Check every 5 seconds (adjust as needed) session = NULL, checkFunc = check_file_timestamp, valueFunc = load_excel_data )
Key Metrics
renderValueBox({ data <- live_data() valueBox( value = nrow(data), caption = "Total Entries", icon = "fa-table" ) })
Trend Chart
renderPlot({ data <- live_data() # Replace with your own visualization code plot(data$date, data$value, type = "l", col = "blue", lwd = 2) })
# 4. Deploy the App to the Server - Create an app folder in Shiny Server’s default directory: ```bash sudo mkdir /srv/shiny-server/my_dashboard
- Copy your modified
.Rmdfile and Excel data file to this folder (usescpfrom your local machine):scp dashboard.Rmd data.xlsx your_username@server_ip:/srv/shiny-server/my_dashboard/ - Fix file permissions so Shiny Server can access the data:
sudo chown shiny:shiny /srv/shiny-server/my_dashboard/data.xlsx
Edit the Shiny Server config file to customize access:
sudo nano /etc/shiny-server/shiny-server.conf
Add a block for your app (adjust paths as needed):
server { listen 3838; # Default Shiny port location /my_dashboard { site_dir /srv/shiny-server/my_dashboard; log_dir /var/log/shiny-server; directory_index on; } }
Restart Shiny Server to apply changes:
sudo systemctl restart shiny-server
Open the server’s firewall for port 3838:
sudo ufw allow 3838
(For cloud servers, also add an inbound rule for port 3838 in your provider’s security group.)
Choose a method to get your local Excel updates to the server:
- Manual sync: Use
scpevery time you update the file (simple but tedious). - Auto-sync with cloud storage: Use
rcloneto sync Dropbox/Google Drive to the server. Installrclone, connect your cloud account, then set up a recurring sync:rclone sync your_remote:path/to/data.xlsx /srv/shiny-server/my_dashboard/data.xlsx --daemon --interval 30s - User uploads: Add a file input to your dashboard so users can upload Excel files directly (overwrites the server’s data file):
## Upload Updated Data ```{r} fileInput("excel_upload", "Upload New Excel File", accept = ".xlsx") observeEvent(input$excel_upload, { req(input$excel_upload) file.copy(input$excel_upload$datapath, "data.xlsx", overwrite = TRUE) })
- Visit your dashboard in a browser:
http://your_server_ip:3838/my_dashboard - Modify your Excel file, sync it to the server, and wait 5 seconds (or your chosen interval)—the dashboard should refresh automatically with the new data.
- Don’t set the
intervalMillistoo low (e.g., <1 second)—it’ll eat up server resources on low-cost plans. - Check Shiny Server logs if something breaks:
/var/log/shiny-server/ - For secure access, set up Nginx as a reverse proxy and add HTTPS with Let’s Encrypt (free certificates).
内容的提问来源于stack exchange,提问作者Praneeth

