Reporting tool

Here we are going to create an interactive reporting tool with R and R-shiny

This coding example is a bit more complex than the previous one and is written in R language. Start by 3 main .R files. Extensive and detailed documentation about R-shiny apps can be found here.

  • ui.R

  • server.R

  • global.R

Initially the user interface (ui.R) file is created, where all the functions related to the graphics of the interface are placed. Then we build the server component, which is actually the part all the work behind the scenes. There the functions that create API calls to the answr platform are placed and their response is passed on to generate the plots for visualization. Lastly, the global.R is created, which contains all the libraries to be used by the app.

ui.R

# Choices for drop-downs
templates <- c(
  "Climate Statistics (Free)" = "climatestatistics",
  "Advanced Climate Statistics (Free)" = "advancedclimatestatistics",
  "Natural Disaster (Premium)" = "naturaldisaster"
)

navbarPage("Answr Dashboard API Visualizations", id="nav",

  tabPanel("Configuration",
    div(class="outer",

      tags$head(
        # Include our custom CSS
        includeCSS("styles.css"),
        includeScript("gomap.js")
      ),

      # If not using custom CSS, set height of leafletOutput to a number instead of percent
      leafletOutput("map", width="100%", height="100%"),

      # Shiny versions prior to 0.11 should use class = "modal" instead.
      absolutePanel(id = "controls", class = "panel panel-default", fixed = TRUE,
        draggable = TRUE, top = 60, left = "auto", right = 20, bottom = "auto",
        width = 330, height = "auto",

        h2("Configuration"),

        textInput("email", "Email", value = "", width = NULL, placeholder = NULL),
        passwordInput("password", "Password", value = "", width = NULL, placeholder = NULL),
        selectInput("reporttype", "Report Type", templates),
        actionButton("execute", "Create the Report")
      ),

      tags$div(id="cite",
        'answr.space'
      )
    )
  ),

  tabPanel("Generated Report",
           titlePanel(textOutput("template")),
           plotlyOutput("graph1"), 
           plotlyOutput("graph2"), 
           plotlyOutput("graph3")
  #          sidebarLayout(
  #            sidebarPanel(
  #              ),
  #              mainPanel(
  #                plotlyOutput("a_con_dry_days"), 
  #                plotlyOutput("a_con_frost_days"), 
  #                plotlyOutput("a_con_wet_days")
  #                )
  #              
  # )
)
)

server.R

global.R

User Interface

By running the app, a window will appear initially, that includes the map and the configuration menu.

  • Step 1: Add a marker on the map

  • Step 2: Fill in the email and password of your account

  • Step 3: Select the report type and click on "Create the Report"

Configuration tab

Wait a few seconds and click on the "Generated Report" tab to see the results.

Generated Report from selected template

Last updated