5

I can see how ggvis, rCharts, etc. fits in with a server.r + ui.r construction. I'm now trying to make an HTML UI but am and unable to find any clues about passing an interactive chart to an HTML UI. Any clues?

Investigated for ggvis, rCharts, NBD3. Not investigated for plotly. Avoiding googleVis.

A similar question has been asked before, by an Evan Farrell, but with no response - https://groups.google.com/forum/#!topic/ggvis/GsZQRl3VOZ4

Edit: Do these need custom binding? Any simpler alternatives? I thought the output would be javascript produced SVG, so is it not possible for the libraries to be included manually and the SVG just bound to a div element?

Edit 2: Could I pass my dataset to the UI somehow and build say a D3 chart in index.html itself

TheComeOnMan
  • 12,535
  • 8
  • 39
  • 54
  • There are lots of examples you can find by a simple google search. For a start go to the end of this page: 'http://ggvis.rstudio.com/interactivity.html' with a basic ggvis/shiny example. – hvollmeier Feb 15 '15 at 13:46
  • Thanks @hvollmeier, I'm struggling with converting the ui.r part to something that works with an HTML UI. Have you any pointers on that? – TheComeOnMan Feb 15 '15 at 14:01
  • 1
    You should not have an ui.r file at all if you work with an html-ui. You basically put an index.html file, which replaces the ‘ui.r’ file into the ‘www’ directory (which is a sub-directory to your project directory). Have a look at this link http://shiny.rstudio.com/articles/html-ui.html – hvollmeier Feb 15 '15 at 14:21
  • 1
    Yes, I understand that too. Can you help piece these two together now? I can write an HTML UI, and I can plot ggvis though ui.r. Now how do I plot ggvis though an HTML UI? – TheComeOnMan Feb 15 '15 at 14:43

2 Answers2

5

You can find here examples such as which is shiny with ggvis:

library(shiny)
library(ggvis)

runApp(list(

  ui={
    library(ggvis)
    shinyUI(pageWithSidebar(
      div(),
      sidebarPanel(
        sliderInput("n", "Number of points", min = 1, max = nrow(mtcars),
                    value = 10, step = 1),
        uiOutput("plot_ui")
      ),
      mainPanel(
        htmlOutput("ggvis_plot"),
        tableOutput("mtc_table")
      )
    ))

  },

  server={
    library(ggvis)
    shinyServer(function(input, output, session) {

      output$ggvis_plot <- renderUI({
        ggvisOutput("plot1")
      })
      # A reactive subset of mtcars
      mtc <- reactive({ mtcars[1:input$n, ] })
      # A simple visualisation. In shiny apps, need to register observers
      # and tell shiny where to put the controls
      mtc %>%
        ggvis(~wt, ~mpg) %>%
        layer_points() %>%
        bind_shiny("plot1")
      output$mtc_table <- renderTable({
        mtc()[, c("wt", "mpg")]
      })
    })
  }
))

To convert it to a html-UI, shiny project you would need to create a directory with the following structure (as described here, as pointed by hvollmeier):

<shinnyappName>
|-- www
     |-- index.html
|-- server.R

The server part will remain the same. For our example the index.html part would be something like:

<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  <script type="application/shiny-singletons"></script>
  <script type="application/html-dependencies">json2[2014.02.04];jquery[1.11.0];shiny[0.11.1];ionrangeslider[2.0.2];bootstrap[3.3.1]</script>
<script src="shared/json2-min.js"></script>
<script src="shared/jquery.min.js"></script>
<link href="shared/shiny.css" rel="stylesheet" />
<script src="shared/shiny.min.js"></script>
<link href="shared/ionrangeslider/css/normalize.css" rel="stylesheet" />
<link href="shared/ionrangeslider/css/ion.rangeSlider.css" rel="stylesheet" />
<link href="shared/ionrangeslider/css/ion.rangeSlider.skinShiny.css" rel="stylesheet" />
<script src="shared/ionrangeslider/js/ion.rangeSlider.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link href="shared/bootstrap/css/bootstrap.min.css" rel="stylesheet" />
<script src="shared/bootstrap/js/bootstrap.min.js"></script>
<script src="shared/bootstrap/shim/html5shiv.min.js"></script>
<script src="shared/bootstrap/shim/respond.min.js"></script>

</head>
<body>
  <div class="container-fluid">
    <div class="row">
      <div></div>
    </div>
    <div class="row">
      <div class="col-sm-4">
        <form class="well">
          <div class="form-group shiny-input-container">
            <label class="control-label" for="n">Number of points</label>
            <input class="js-range-slider" id="n" data-min="1" data-max="32" data-from="10" data-step="1" data-grid="true" data-grid-num="7.75" data-grid-snap="false" data-prettify-separator="," data-keyboard="true" data-keyboard-step="3.2258064516129"/>
          </div>
          <div id="plot_ui" class="shiny-html-output"></div>
        </form>
      </div>
      <div class="col-sm-8">
        <div id="ggvis_plot" class="shiny-html-output"></div>
        <div id="mtc_table" class="shiny-html-output"></div>
      </div>
    </div>
  </div>
</body>
</html>

Which you should write or you could get if you save the html page generated by the shinyUI as index.html and modify it to your needs as well as remove anything extra and undesirable.

 library(shiny)
 runApp("<shinyAppName>")
Community
  • 1
  • 1
Stanislav
  • 2,629
  • 1
  • 29
  • 38
  • Thanks @Stanislav. I'm able to see the table correctly but there is no scatterplot. The div `plot-gear-icon` is populated, but the div `plot` is empty. Do you see the same result when you run it? – TheComeOnMan Feb 16 '15 at 01:45
  • Yes, however when I tried it on another computer I had to run first the whole example with the ui.R probably in order to generate all the includes. – Stanislav Feb 16 '15 at 02:09
  • By includes, do you mean the scripts and CSS being sourced (I'm new to this :)) ? Running ui before isn't an option for me as I have to share this app with others as well. – TheComeOnMan Feb 16 '15 at 02:35
  • Done: Removed "ggvisOutput" from ui.R and now it works. – Stanislav Feb 16 '15 at 05:52
  • I don't understand. The HTML UI is the one that isn't working for me. I'm not testing UI.r at all. – TheComeOnMan Feb 16 '15 at 11:10
  • I have updated the "server.R" and the "index.html". In a few words what I did is move from the ui.R to the server.R all the ggvis code, thus leaving the UI free of them and since the index.html file is build from the ui.R it is free from the ggvis and is working (The index.html is the file generated from the ui.R (only on one machine, you can use directly the server.R and index.html)). Note: You need to have ggvis and shiny installed. – Stanislav Feb 16 '15 at 11:24
  • To sum up, all the code above is updated. Create the: |-- www |-- index.html |-- server.R scheme and it will work for you. Kind Regards, – Stanislav Feb 16 '15 at 11:36
  • Your logic seems sound but it still doesn't work. The plot1 div is empty. – TheComeOnMan Feb 17 '15 at 18:30
  • I have tested it on independent computers both windows and linux. Are your `shiny ` and `ggvis` up to date? Otherwise I cannot help further without extra information. – Stanislav Feb 17 '15 at 20:11
  • Just installed them again - ggvis is 0.3.0.1 and shiny 0.11.1. Still nothing. – TheComeOnMan Feb 19 '15 at 14:18
  • I do get the gear button on the top right which offers svg, etc. but doesn't do anything if I click something. The console says - "window.controllers is deprecated. Do not use it for UA detection. https-everywhere.js:326 Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help http://xhr.spec.whatwg.org/ jquery.min.js:4 mutating the [[Prototype]] of an object will cause your code to run very slowly; instead create the object with the correct initial [[Prototype]] value using Object.create" – TheComeOnMan Feb 19 '15 at 14:19
  • That looks to me like a new issue and probably needs a new post. Something is not up to date, completely out of my mind maybe your Firefox browser, but I do not claim to know what. – Stanislav Feb 20 '15 at 06:37
  • I'm able to run the ggvis link you've posted. The only difference between your server+ui and the link is that the 'ggvisoutput' function is in the server or in the ui, right? I ask becase your server + ui is also not working for me. – TheComeOnMan Feb 22 '15 at 06:52
  • I spent a half hour updating everything, old or new. EVERYTHING. Even Rstudio. It works now. Not sure why it didn't work earlier, I wasn't using something that old. But it works now. Thanks a lot, @Stanislav! – TheComeOnMan Feb 22 '15 at 07:50
  • Did you get this working with a `plotly` plot, @TheComeOnMan? I'm having the same problem: the HTML UI method is pretty minimally documented, and it's not clear to me how to bind a `div` to a `renderPlotly` method in `app.r` (or `server.r`). Using `shiny-html-output` or `shiny-plot-output` doesn't work. – jimjamslam Sep 23 '17 at 06:21
0

You can use Shiny, ggplot2, and Plotly to generate interactive, web-based plots. Plots are rendered with D3.js, and can be embedded in HTML.

In general this means you have two options. First, generate a Shiny app with interactive graphs and embed the whole app. Second, generate interactive graphs from within a Shiny app or using ggplot2, then embed that figure.

For code and an explanation for creating interactive grahps with Shiny, see this tutorial. This approach lets you make interactive graphs like this:



enter image description here

Mateo Sanchez
  • 1,604
  • 15
  • 20