Python Script භාවිතා කරමින් R-Shiny Interactive Dashboard නිර්මාණය කිරීම.


Well-known member
  • Nov 19, 2016

    Creating interactive dashboards in R Shiny using Python scripts as the backend.

    Why is it always R v Python? Why can’t we admit that both are unique in their own way and we should know how to leverage both of them at once!

    Shiny is an R package that makes it easy to build interactive web apps straight from R. You can host standalone apps on a webpage or embed them in R Markdown documents or build dashboards. You can also extend your Shiny apps with CSS themes, Html widgets, and JavaScript actions.


    R Shiny
    When I started talking about R and R Shiny, People tend to ask me this question:

    Why did you choose Shiny over Dash (By Plotly — Python)? Do they have the same level of complexity ? Which has a bigger community and why would someone prefer one over the other?

    Ok, let’s breakdown the question into three parts.
    1. Why Shiny over Dash?
    2. Do they have the same level of Complexity?
    3. Who has the biggest community?
    Here’s the answer:

    Currently Shiny is far more mature than Dash. Dash doesn’t have a proper layout tool yet, and also not build in theme, so if you are not familiar with Html and CSS, your application will not look good (You must have some level of web development knowledge). Also, developing new components will need ReactJS knowledge, which has a steep learning curve.
    I would say that right now Shiny might be a little better, especially if your needs are more complex dashboards, but I feel as Python community is so much larger, Dash is exponentially growing up and it could surpass shiny in general as Python can be used for multiple stuff and who knows what else Dash can evolve to be.
    If you’re after more detailed comparison, click here.

    AND... Who said you have to choose between one over another?

    Because and this is my answer!: We are data scientists, We know how to code in R and Python both! Why can’t we use R+Python for our development right? It’s not rocket science it’s Data Science and that’s what we good at. Let’s see how to get the maximum out of R and Python for a Shiny Dashboard!


    The basic parts of a Shiny app

    figure 1.0
    Shiny applications have two components, a user interface object and a server function, that are passed as arguments to the shinyApp function that creates a Shiny app object from this UI/server pair. The source code for both of these components is listed below.

    You can either create a one R file named app.R and create two seperate components called (ui and server inside that file) or create two R files named ui.R and server.R (figure 1.0)

    In subsequent sections of the article, we’ll break down Shiny code in detail and explain the use of “reactive” expressions for generating output. For now, though, just try playing with the sample application and reviewing the source code to get an initial feel for things. Be sure to read the comments carefully.


    The user interface is defined as follows: [Note that I’m going to create one .R file called app.R and will put these two code blocks inside of it]
    # Define UI for app that draws a histogram ----
    ui <- fluidPage(

    # App title ----
    titlePanel("Hello Shiny!"),

    # Sidebar layout with input and output definitions ----

    # Sidebar panel for inputs ----

    # Input: Slider for the number of bins ----
    sliderInput(inputId = "bins",
    label = "Number of bins:",
    min = 1,
    max = 50,
    value = 30)


    # Main panel for displaying outputs ----

    # Output: Histogram ----
    plotOutput(outputId = "distPlot")


    The server-side of the application is shown below. At one level, it’s very simple — a random distribution is plotted as a histogram with the requested number of bins. However, you’ll also notice that the code that generates the plot is wrapped in a call to renderPlot. The comment above the function explains a bit about this, but if you find it confusing, don’t worry, we’ll cover this concept in much more detail soon.

    # Define server logic required to draw a histogram ----
    server <- function(input, output) {

    # Histogram of the Old Faithful Geyser Data ----
    # with requested number of bins
    # This expression that generates a histogram is wrapped in a call
    # to renderPlot to indicate that:
    # 1. It is "reactive" and therefore should be automatically
    # re-executed when inputs (input$bins) change
    # 2. Its output type is a plot

    output$distPlot <- renderPlot({

    x <- faithful$waiting
    bins <- seq(min(x), max(x), length.out = input$bins + 1)

    hist(x, breaks = bins, col = "#75AADB", border = "white",
    xlab = "Waiting time to next eruption (in mins)",
    main = "Histogram of waiting times")




    Finally, we use the shinyApp function to create a Shiny app object from the UI/server pair that we defined above
    shinyApp(ui, server)
    We save all of this code, the ui object, the server function, and the call to the shinyApp function, in an R script called app.R. This is the same basic structure for all Shiny applications.

    If you want to go through more examples, There are plenty of examples in this page provided by Shiny R Studio.

    Your final code should look like this;


    Then try to hit run and see what brings out to the browser page:

    Read rest on here (Increase 10000 words if I try to write everything)

    Thank you