[英]How do I run a plotly dash application through reticulate in R?
im trying to run my code through R's reticulate.我试图通过 R 的网状结构运行我的代码。 However, its a dash app.但是,它是一个破折号应用程序。 - -
My code in python-我在python中的代码-
#### lib required for dashboard
import plotly.graph_objects as go
import dash
import dash_html_components as html
import dash_core_components as dcc
import plotly.offline as pyo
from dash.dependencies import State, Input, Output
import numpy as np
import pandas as pd
## Lets create a dataFrame generating data with help of numpy
def datagen():
my_sample_data = np.random.random_sample([100,3])
cat_g = ["good","bad","worst"]
sample_Cat = [cat_g[np.random.randint(0,3)] for i in range(100)]
Base_Data = pd.DataFrame(my_sample_data,columns=["val_1","val_2","val_3"])
Base_Data["sample_Cat"] = sample_Cat
return(Base_Data)
cat_g = ["good","bad","worst"]
## Creating values and lables for dropdown
options_list = []
for i in cat_g:
options_list.append({'label': i, 'value': i})
def fig_generator(sample_data):
sample_data = sample_data.reset_index(drop=True)
sample_data.head()
plot_data =[]
for i in range(1,4):
plot_data.append(go.Scatter(x=sample_data.index, y=sample_data['val_'+ str(i)], name = 'val_'+ str(i) ))
plot_layout = go.Layout(title = " This plot is generated using plotly ")
fig = go.Figure( data = plot_data ,layout = plot_layout)
return(fig.data,fig.layout)
### Dash board code
app = dash.Dash()
### defining the HTML component
app.layout = html.Div(children=[html.Div("Welcome to the dashboard",style= { "color": "white",
"text-align": "center","background-color": "blue",
"border-style": "dotted","display":"inline-block","width":"80%"
}),
html.Div(dcc.Dropdown(id = "drop_down_1" ,options= options_list , value= 'good'
),style= {
"color": "green",
"text-align": "center","background-color": "darkorange",
"border-style": "dotted","display":"inline-block","width":"20%"
}),
html.Div(children=[html.P(
id="map-title",
children = "Forecast and validation for Facility ",
), html.Div(dcc.Graph(id ="plot_area"))
],style= {
"color": "black",
"text-align": "center","background-color": "yellow",
"border-style": "dotted","display":"inline-block","width":"75%",
})],style={"width":"100%",'paffing':10})
## Creating callback buttons
@app.callback(Output("plot_area", 'figure'),
[Input("drop_down_1", "value")])
def updateplot(input_cat):
df= datagen()
sample_data = df[df["sample_Cat"] == input_cat ]
trace,layout = fig_generator(sample_data)
return {
'data': trace,
'layout':layout
}
if __name__=='__main__':
app.run_server()
When I run this code through python IDE it works perfectly and lets me see the UI.当我通过 python IDE 运行此代码时,它可以完美运行并让我看到 UI。 I am new to the reticulate package.我是网状 package 的新手。 How would I run this code through Rstudio?我将如何通过 Rstudio 运行此代码? I am trying to use the same and it is not working.我正在尝试使用它,但它不起作用。
After installing and loading reticulate
安装并加载reticulate
后
install.packages("reticulate")
library(reticulate)
you can use the source_python
function to load Python script file.您可以使用source_python
function 加载 Python 脚本文件。
source_python("app.py")
The app will be run immediately just as if you were to call python app.py
on the file directly.该应用程序将立即运行,就像您直接在文件上调用python app.py
。 Type in the url Dash is running on in your browser to see the result.在浏览器中输入 url Dash 正在运行以查看结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.