Get Free Trial
  1. API reference
Connecting to Data Source
Configuring the component
  • Documentation for older versions
  • Table of contents

    Integration with R Shiny

    This tutorial will help you integrate Flexmonster with the R Shiny application. It is based on the how to build a Shiny app guide.

    Prerequisites

    To run a simple application, you will need R. Get it here if it’s not already installed on your machine.

    Then open the R console and install the Shiny package by running:

    install.packages("shiny") 

    After that, choose one of the following options:

    1. Run a simple R Shiny and Flexmonster sample from GitHub
    2. Integrate Flexmonster into a Shiny application

    Run a simple R Shiny and Flexmonster sample from GitHub

    Step 1. Download the .zip archive with the sample or clone it from GitHub with the following commands:

    git clone https://github.com/flexmonster/pivot-r-shiny
    cd pivot-r-shiny

    Step 2. Run your application from the R console:

    library(shiny)
    runApp('~/pivot-r-shiny')

    The application will be automatically opened in your browser.

    It can be shut down manually with Ctrl + C.

    If any errors appear on this step, refer to the troubleshooting subsection.

    Integrate Flexmonster into a Shiny application

    To integrate Flexmonster into an R Shiny application, follow these steps:

    Step 1. If you don’t have an R Shiny app yet, create a new empty folder (e.g., shinyapp/) with an empty app.r file inside.

    Step 2. In the app.r file, import Shiny into your application:

    library(shiny)

    Step 3. In the app.r file, define a simple server function:

    server <- function(input, output) {
    
    }

    Step 4. In an .html file (e.g., index.html), create a <div> container for the component:

    <div id="pivotContainer">The component will appear here</div>

    Step 5. Include flexmonster.js in your HTML page:

    <div id="pivotContainer">The component will appear here</div>
    <script src="https://cdn.flexmonster.com/flexmonster.js"></script>

    Step 6. Add a simple script to embed the component:

    <div id="pivotContainer">The component will appear here</div>
    <script src="https://cdn.flexmonster.com/flexmonster.js"></script>
    <script>
    var pivot = new Flexmonster({
    container: "pivotContainer",
    componentFolder: "https://cdn.flexmonster.com/",
    toolbar: true
    });
    </script>

    Step 7. Configure a simple report:

     <div id="pivotContainer">The component will appear here</div>
     <script src="https://cdn.flexmonster.com/flexmonster.js"></script>
    
     <script type="text/javascript">
         var pivot = new Flexmonster({
             container: "pivotContainer", 
             componentFolder: "https://cdn.flexmonster.com/",
             toolbar: true, 
             report: {
                 dataSource: {
                     type: "json",
                     filename: "data/data.json"
                 }
             }
         });
    </script>

    Step 8. In the app.r file, define the user interface by calling the htmlTemplate function with the HTML file path (e.g., index.html) as a parameter.

    ui <- htmlTemplate("index.html")

    Step 9. Add the shinyApp function that uses the ui object and the server function to build a Shiny app object:

    shinyApp(ui, server)

    Step 10. Run your application from the R console:

    library(shiny)
    runApp('~/shinyapp')

    The application will be automatically opened in your browser.

    It can be shut down manually with Ctrl + C.

    If any errors appear on this step, refer to the troubleshooting subsection.

    Troubleshooting

    While completing this tutorial, users may face problems with running the app, especially macOS or Linux users. This subsection provides simple instructions on how to fix errors that may be encountered.

    Error: No Shiny application exists at the path

    If you followed the tutorial accurately and got this error, try the following:

    • Make sure the right path to the project folder was specified when running the app. Note that if the app is run right in the project folder (e.g., pivot-r-shiny/), the runApp function should be called without parameters:
      library(shiny)
      runApp()

    The following instructions only apply to UNIX-like systems (macOS, Linux, etc.):

    • Try starting the R interactive shell without superuser privileges. It can be done with the following command:
      R
      Then run the app with commands from the guide:
      library(shiny)
      runApp('~/pivot-r-shiny')
    • Running the app right from the terminal may also help. To do this, open the terminal in the project folder (e.g., pivot-r-shiny/) and run the following command:
      R -e "shiny::runApp()"
    • The following command can also be used to run the app from the terminal opened in the project folder:
      Rscript app.R

    What’s next?

    You may be interested in the following articles: