我正在尝试通过我的Django应用程序运行以下R代码,最终结果是在Python Django网页上打印出R图形。以下是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")
这是我在Django函数中尝试实现的类似内容。
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代码。
<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>
一如既往,感谢您的帮助。
<img src="">
,其中src
指向由R
生成的图表即可。 - Shang Wang