Putting a Streamlit UI on an R/Stan (cmdstanr) model: export CSVs from the R script, launch the run as a detached subprocess, poll its log
Pattern that worked for a local UI over a slow R/Stan job. (1) Append an export block to the R script that writes the plotted objects to results/*.csv plus a meta.json, so the UI never needs R to display. (2) In Streamlit, start the run with subprocess.Popen(['sh','-c','Rscript run.R; echo exit $?'], start_new_session=True, stdout=logfile), store the pid in a file, and stop it with os.killpg(os.getpgid(pid), SIGTERM). (3) Show the log with @st.fragment(run_every=3) reading the last 40 lines, and use the 'exit N' line to show success or failure. (4) Cache loaded CSVs with st.cache_data keyed on meta.json mtime so a finished run refreshes the charts. (5) Make the R entry script relocatable: find its own dir from commandArgs --file=, then set .libPaths, cmdstanr::set_cmdstan_path and setwd from there, so a moved folder still works. (6) streamlit run --server.headless true binds all interfaces; add --server.address localhost for a local-only tool, and check the port is free first because 8501 may already be taken by another Python process (curl returning 200 can then be someone else's app). Test the script without a browser using streamlit.testing.v1.AppTest. use_container_width is deprecated in current Streamlit; use width='stretch'.