Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Knitting markdown file having shiny content

Tags:

r

knitr

shiny

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.

like image 673
mrigank shekhar Avatar asked Jun 18 '18 10:06

mrigank shekhar


People also ask

What does it mean to knit a markdown file?

You can transform an R Markdown file in two ways. knit - You can knit the file. The rmarkdown package will call the knitr package. knitr will run each chunk of R code in the document and append the results of the code to the document next to the code chunk. This workflow saves time and facilitates reproducible reports.

Why is my R Markdown not knitting?

If a chunk works in R but not when you knit, it is almost always because you've changed a variable in your global working environment not using code in a chunk. Try restarting your R session and running each chunk sequentially to make sure all your variable values are up to date.

How do you use markdown in knitting?

There are two ways to render an R Markdown document into its final output format. If you are using RStudio, then the “Knit” button (Ctrl+Shift+K) will render the document and display a preview of it. Note that both methods use the same mechanism; RStudio's “Knit” button calls rmarkdown::render() under the hood.

What are the two main differences between an R Markdown document and a shiny dashboard?

It generally comes down to the amount of interactivity you need in your app. For a full solution where data is updated and processed in real-time, Shiny is your best option. If you just need a nice format for presentation offline, then RMarkdown can produce some very nice looking formats.


1 Answers

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.
like image 113
JdP Avatar answered Oct 22 '22 01:10

JdP