本地Shiny应用修改server.R/ui.R后自动重载方案咨询
Absolutely! I feel your pain—restarting runApp() every time you tweak a line of UI or server code is such a drag. The good news is there are several solid solutions that mirror the auto-refresh magic you love from xaringan::infinite_moon_reader. Here are the most straightforward options:
1. Use Shiny's Built-in Hot Reload (Recommended)
Starting with Shiny version 1.7.0, there's a native hot_reload parameter that does exactly what you want. It watches your ui.R, server.R, and even static files in the www/ folder (like CSS/JS), and auto-refreshes the browser as soon as you save changes.
Just run your app with this flag:
library(shiny) runApp("path/to/your/app", hot_reload = TRUE)
No extra packages needed, and it works seamlessly with both single-file app.R and split ui.R/server.R setups.
2. Leverage the servr Package (Same as xaringan!)
Since xaringan::infinite_moon_reader uses the servr package under the hood, you can use the same tool for Shiny apps. The servr::shiny() function launches your app and monitors file changes to trigger auto-refreshes.
First install and load the package if you haven't:
install.packages("servr") library(servr)
Then start your app with:
shiny("path/to/your/app")
This will give you that familiar "save and watch it update instantly" experience you get with xaringan slides.
3. RStudio IDE Integrated Hot Reload
If you're using RStudio, you don't even need to write extra code. When you open your Shiny app project, click the dropdown next to the "Run App" button and select "Run App with Hot Reload". RStudio will handle the rest—every time you save ui.R, server.R, or app.R, the app in your browser will refresh automatically.
All three options eliminate the need for manual runApp() restarts, so you can focus on building your app instead of reloading it. Pick the one that fits your workflow best!
内容的提问来源于stack exchange,提问作者Nicolabo

