From cfca190fa970d8540b111ed2f7940dcc0e055739 Mon Sep 17 00:00:00 2001 From: Yihui Xie Date: Fri, 18 Jul 2014 14:33:53 -0500 Subject: [PATCH] an inline output example --- 026-shiny-inline/DESCRIPTION | 7 +++++++ 026-shiny-inline/index.Rmd | 38 ++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 026-shiny-inline/DESCRIPTION create mode 100644 026-shiny-inline/index.Rmd diff --git a/026-shiny-inline/DESCRIPTION b/026-shiny-inline/DESCRIPTION new file mode 100644 index 00000000..2d682767 --- /dev/null +++ b/026-shiny-inline/DESCRIPTION @@ -0,0 +1,7 @@ +Title: Inline Output +Author: Yihui Xie +AuthorUrl: http://yihui.name +License: MIT +DisplayMode: Showcase +Tags: inline +Type: Shiny diff --git a/026-shiny-inline/index.Rmd b/026-shiny-inline/index.Rmd new file mode 100644 index 00000000..37c416a2 --- /dev/null +++ b/026-shiny-inline/index.Rmd @@ -0,0 +1,38 @@ +--- +title: "Inline Output in R Markdown Documents" +author: "Yihui Xie" +runtime: shiny +output: html_document +--- + +Since **shiny** 0.10.1, you can embed inline output elements. We have added an argument `inline` to a few output functions such as `textOutput()`, `plotOutput()`, and `uiOutput()`. This argument will be set to `TRUE` automatically when these types of output are rendered in the inline R expressions of R Markdown documents, e.g. `` `r '\x60r'` renderText(input$foo)` ``. + +First we show a normal shiny app, with a select input to change the title of a plot, and a slider to change the size of points: + +```{r} +library(shiny) +sidebarLayout( + sidebarPanel( + selectizeInput('main', 'Main title', LETTERS), + sliderInput('size', 'Point size', min = 0.2, max = 5, value = 1) + ), + mainPanel( + renderPlot(plot(cars, main = input$main, cex = input$size, pch = 19), + width = 600, height = 400) + ) +) +``` + +Now we use `renderText()` to render the plot title and point size in this paragraph. The main title is **`r renderText(input$main)`** and the point size is **`r renderText(input$size)`**. + +Besides `renderText()`, you can also embed some other output elements inline. We define a function to draw the `sunspots` data, with the aspect ratio value from a slider: + +```{r} +sunsplots_line = function() { + par(mar = rep(0, 4)) + plot(sunspots, axes = FALSE, ann = FALSE, asp = input$asp) +} +sliderInput('asp', 'Change the aspect ratio', .02, .3, .2) +``` + +Here is a spark line `r renderPlot(sunsplots_line(), width=300, height=40)` that shows you the time series `sunspots`. Note when you render inline plots, you must provide both `width` and `height` values (in pixels), because it is not possible for **shiny** to figure out the width and height values automatically in this case. We used the size `300 x 40` here.