简体   繁体   English

如何通过 R 中的网状体运行 plotly dash 应用程序?

[英]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.

相关问题 如何在Python + Dash + Plotly中将表格移动到网页的中间? - How do I move table to the middle of the webpage in Python + Dash + Plotly? 如何为 Dash plotly 可编辑数据表排列和编码数据? - How do I arrange and code data for a Dash plotly editable datatable? 如何使用 python 中的破折号 plotly 更改图表的 colors - How do I change the colors of the graph using dash plotly in python 我应该如何最好地在 RStudio 中“运行”Python 脚本? 通过运行(使用 reticulate::repl_python()?)或来源? - How should I preferably "run" Python scripts in RStudio? Through run (using reticulate::repl_python()?) or source? 在 plotly dash 中,如何将本地图像文件源到 dash.html.Img? - In plotly dash, how do I source a local image file to dash.html.Img? 如何在 Plotly Dash 应用程序中水平放置图形? - How to put graphs horizontally in Plotly Dash application? Plotly Dash 应用程序未运行 - Plotly Dash application not running 如果 Anaconda 环境位于我的 WSL 主目录中,我如何从 R reticulate 访问它? - How do I access Anaconda environment from R reticulate if it is in my WSL home directory? 如何通过网状结构在R中使用熊猫编写csv? - how to write csv using pandas in R through reticulate? 如何使用网状结构从R运行Python gensim函数 - How to run a Python gensim functions from R with reticulate
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM