9

I have created a markdown file containing some shiny content. When i run the current chunk code, the application works just fine. But when i try to knit the code chunk it gives the following error:

Shiny applications not supported in static R Markdown documents.

Is there anyway that i can knit this code.

On some sites i read the following could also be done

```{r, echo=FALSE}
library(shiny)
shinyAppDir(
  system.file("examples/06_tabsets", package = "shiny"),
  options = list(
    width = "100%", height = 550
  )
)
```

But again when i knit this the same thing happens. Is there any way to get an html ouput from a shiny app.

pogibas
  • 27,303
  • 19
  • 84
  • 117
mrigank shekhar
  • 544
  • 3
  • 15

1 Answers1

1

To add shiny interaction, add runtime: shiny to the YAML.

See link for how to embed a shiny app within a document.

See the standard example given when creating a new Markdown file in Rstudio (file > new file > R markdown > Shiny document):

--- 
title: "Untitled" 
author: "author" 
date: "July 24, 2018" 
output: html_document runtime: shiny
---

{r setup, include=FALSE} knitr::opts_chunk$set(echo = TRUE)

This R Markdown document is made interactive using Shiny. Unlike the more traditional workflow of creating static reports, you can now create documents that allow your readers to change the assumptions underlying your analysis and see the results immediately. 

To learn more, see [Interactive Documents](http://rmarkdown.rstudio.com/authoring_shiny.html).

## Inputs and Outputs


    ```{r eruptions, echo=FALSE}
inputPanel(
  selectInput("n_breaks", label = "Number of bins:",
              choices = c(10, 20, 35, 50), selected = 20),

  sliderInput("bw_adjust", label = "Bandwidth adjustment:",
              min = 0.2, max = 2, value = 1, step = 0.2)
)

renderPlot({
  hist(faithful$eruptions, probability = TRUE, breaks = as.numeric(input$n_breaks),
       xlab = "Duration (minutes)", main = "Geyser eruption duration")

  dens <- density(faithful$eruptions, adjust = input$bw_adjust)
  lines(dens, col = "blue")
})
```

## Embedded Application

Its also possible to embed an entire Shiny application within an R Markdown document using the `shinyAppDir` function. This example embeds a Shiny application located in another directory:

```{r tabsets, echo=FALSE} 
shinyAppDir(
system.file("examples/06_tabsets", package = "shiny"),   
options = list(width = "100%", height = 550   ) 
) 
```

Note the use of the `height` parameter to determine how much vertical space the embedded application should occupy.

You can also use the `shinyApp` function to define an application inline rather then in an external directory.

In all of R code chunks above the `echo = FALSE` attribute is used. This is to prevent the R code within the chunk from rendering in the document alongside the Shiny components.
JdP
  • 714
  • 7
  • 14
  • How do you run/render this without Rstudio? OP asks to: *get an html output from a shiny app* (he wants an html file). – pogibas Jul 24 '18 at 12:41
  • Ah, sorry I probably misunderstood the problem. Running a shiny app without R running in background is not possible, I think. Without Rstudio is possible, see eg https://stackoverflow.com/questions/15884975/possible-to-run-rshiny-app-without-opening-an-r-environment#15885347 – JdP Jul 24 '18 at 13:38
  • btw: There are several ways to host and deploy shinyapps or markdown docs with included shiny apps (https://shiny.rstudio.com/deploy/). You can insert the link to the app elsewhere in a markdown doc – JdP Jul 24 '18 at 13:38
  • @JdP the first time i was stuck here was when i tried to run readline() function and then knit it. that was the first time i got this error. – mrigank shekhar Jul 25 '18 at 16:15