This project converts the SOCR AI Bot from an R/RShiny application
into a new HTML5 webapp using React
and TypeScript
frontend with an R
backend connected to the frontend UI using Plumber API
.
The SOCR AI Bot leverages SOCR/DSPA computational libraries and Generative AI Model (GAIM) interfaces,
including OpenAI's GPT-4o, to translate natural language commands into R
code, generate synthetic text and images, analyze data from uploaded files,
and provide results as downloadable reports. allows users to generate random data
in the frontend, send it to R
for analysis, and display the results returned from R
.
The R
backend provides a REST API endpoint /analyze
that:
- Receives JSON data containing x and y values from the React frontend
- Calculates summary statistics (means, medians, standard deviations, correlation)
- Fits a linear regression model to the data
- Generates three interactive Plotly visualizations:
- Scatter plot with regression line
- Histogram of residuals
- Q-Q plot for residual normality assessment
- Returns all results as a structured JSON object
The React frontend consists of several components:
-
DataGenerator: Allows users to:
- Specify sample size, correlation strength, and noise level
- Generate random correlated data
- Send the data to R for analysis
-
SummaryStats: Displays basic statistical measures returned from R
- Color-codes correlation values based on strength
-
ModelResults: Shows linear regression results
- Displays the model equation
- Presents model statistics in a formatted table
- Indicates statistical significance with appropriate markers
-
PlotDisplay: Renders the three Plotly visualizations returned from R
- Converts R Plotly objects to JavaScript Plotly format
- Displays interactive graphs with hover information
-
AnalysisResults: Combines all result components into a unified display
- The user provides instrcutions and data, or uses simulated data
- When "Analyze with R" is clicked, the data is sent to the R Plumber API
- R processes the data and returns analysis results
- The React application renders the results in an elegant and interactive interface
- Both systems are completely separate but connected via REST API
- The
R
backend can be deployed on any server that supportsR
- The
React
frontend can be deployed on standard web hosting - CORS is enabled in the
R
API to allow cross-origin requests during development
This implementation demonstrates the strategy of using R-Plumber API
as a backend
for a React
application, leveraging the strengths of both ecosystems: R
's statistical
capabilities and React
's interactive UI features.
The project consists of two main parts:
- R Plumber API Backend: Processes data and performs statistical analysis
- React/TypeScript Frontend: Provides the user interface and visualization
R
installed on your system- Required
R
packages: plumber, dplyr, ggplot2, plotly, jsonlite, tibble, ...
Open R
console and run:
install.packages(c("plumber", "dplyr", "ggplot2", "plotly", "jsonlite", "tibble"))
- Save the
app.R
file to your system - Navigate to the directory where you saved the file
- Start the R Plumber API:
Rscript -e "library(plumber); pr <- plumb('app.R'); pr$run(host='0.0.0.0', port=8000)"
The R
API will start on port 8000.
- Node.js and npm installed on your system
# Clone the repository or create a new project
cd r-analytics-frontend
# Install dependencies
npm install
npm start
The React application will start on http://localhost:3000 and connect to the R Plumber API
running on port 8000.
- Configure default (white) or optional (dark) mode app display
- Run through all 40+ steps of hte interactive hands-on help tutorial (
?
button on the top-right of the app)
React
withTypeScript
- Styled-components for styling
- Plotly.js for visualizations
- Axios for API requests
R
programming languagePlumber
for REST APIdplyr
and other tidyverse packages for data manipulationplotly
for creating interactive visualizations
- Add more complex statistical models (multiple regression, ANOVA, etc.)
- Integrate more advanced visualizations
- Add file upload capabilities for custom datasets
- Implement user authentication
- Deploy to production with proper security measures