I'm trying to run the following R code through my Django application with the end result being a printed R graph in a Python Django webpage. Here is the code for R.
t=read.table(file=file("request.FILES['fileUpload']"))
colnames(t) <- c('x', 'y')
t = data.frame(t)
fit1 = lm(y ~ x, data = t)
par(mfrow=c(1,1))
plot(x=t$x, y=t$y, xlab="x", ylab="y", main="Simple Linear Regression", xlim=c(0,100), ylim=c(0,6), par=20)
abline(fit1, col="red")
Here is something like what I am trying to achieve in the Django function.
from django.shortcuts import render, HttpResponse
import pandas as pd
def upload_files(request):
if request.method == 'POST':
upload = pd.read_table(request.FILES['fileUpload'])
<< Run R Code Here and return the graph >>
response = RGraph
return response
OR
return render(request, 'Regression/index.html', {'graph':response})
return render(request, 'Regression/index.html')
HTML code is as follows.
<html>
<title>Import File</title>
<body>
<h1>Import File</h1>
<hr>
{% if graph %}
<img alt="my base64 graph" src="data:image/png;base64,{{graph}}" />
{% endif %}
<form enctype="multipart/form-data" method="post">
{% csrf_token %}
<input type="file" name="fileUpload" />
<input type="submit" value="Submit" />
</form>
</body>
</html>
As always, thanks for the help.