Integration of control outputs with knitr

I was wondering if there is a way to integrate the package manipulateor gWidgetsManipulateso that their outputs can be viewed / manipulated in the html / markdown output file, since I think it will be extremely useful in developing reproducible interactive research reports. I know that googleVis has some functionality that allows you to integrate it with knitr so that the outputs get into the html file using parameters such as results = 'asis', but googleVis currently limits its capabilities when using sliders, for example.

If the outputs of the management pack or gWidgetsManipulate have not yet been integrated, would it be possible to propose a workaround that allows you to view it in an html file?

i.e. my current code in my rmd file before running knitr-ing in html looks like below ... but I get the following errors.

```{r}
library(manipulate)
manipulate(plot(1:x), x = slider(5, 10))
```

with exit

library(manipulate)
## Error: there is no package called 'manipulate'
manipulate(plot(1:x), x = slider(5, 10))
## Error: could not find function "manipulate"

so try the gWidgetsManipulate package instead ...

```{r}
library(gWidgetsManipulate)
manipulate(plot(1:x), x = slider(5, 10))
```

you get an error ...

library("gWidgetsManipulate")
## Loading required package: gWidgets
manipulate(plot(1:x), x = slider(5, 10))
## Error: unable to find an inherited method for function ".gwindow", for signature "NULL"

I tried to tell guiToolkit to fix this error using things like

options(guiToolkit="WWW")

but to no avail ...

Any help would be greatly appreciated, thanks in advance

+5
source share
1 answer

If you don't need to use gwidgets, I have a solution with Rook and googleVis that does what you want: displaying an interactive chart in html.

script : javascript . . min/max/... .

slider_script <- '
  <input type="range" min="5" max="10" name="plot_max" value="%s" step="1" onchange="document.form1.submit(); showValue(this.value);" />
  <span id="range">%s</span>
  <script type="text/javascript">
  function showValue(newValue)
{
    document.getElementById("range").innerHTML=newValue;
  }
</script>
'

-. : html- res $write().

### this script builds the webpage
    webreport_app <- function(
      ){
      newapp = function(env) {
        req = Rook::Request$new(env)
        res = Rook::Response$new()
        # initialise variables for first execution
        if (is.null(req$POST())){
          plot_max <- 5
        } else{
          plot_max <- as.numeric(req$POST()[["plot_max"]])
        }
        res$write('<body style="font-family:Arial">')
        res$write("<H3>My App</H3>")
        res$write('<form name = "form1" method="POST">\n')
        res$write('<br> Number of dots: \n')
        res$write(sprintf(slider_script, plot_max, plot_max))
        res$write('<br><input type="submit" name="Go!">\n</form>\n')
        if (!is.null(req$POST())) {    
          # generate the plot
          library(googleVis)
          data_for_plot <- data.frame(x_var = 1:plot_max, y_var = 1:plot_max)
          Scatter1 <- gvisScatterChart(data_for_plot)
          # extract chart script
          chart_script <- capture.output(print(Scatter1, 'chart'))
          # write to html
          res$write(paste(chart_script, collapse="\n"))
          res$write("<br><br></body></html>")
        }
        res$finish()
      }
      return(newapp)
    }

, , html Rook:

library(Rook)

# launch the web app
if (exists("report_server")){
  report_server$remove(app, all = TRUE)
  report_server$stop()
  rm(report_server)
}
report_server = Rhttpd$new()
report_server$add(app = webreport_app(), name = "My_app")
report_server$start()
report_server$browse("My_app")
report_server$browse()
+3

All Articles