|
| 1 | +--- |
| 2 | +title: "DT Play" |
| 3 | +author: "Niraj Juneja" |
| 4 | +date: "8/30/2020" |
| 5 | +output: html_document |
| 6 | +runtime: shiny |
| 7 | +--- |
| 8 | + |
| 9 | +```{r setup, include=FALSE} |
| 10 | +knitr::opts_chunk$set(echo = TRUE) |
| 11 | +``` |
| 12 | + |
| 13 | +This R Markdown document is made interactive using Shiny. Made as playground for lwarnign the DT package |
| 14 | + |
| 15 | +To learn more, see [Interactive Documents](http://rmarkdown.rstudio.com/authoring_shiny.html). |
| 16 | + |
| 17 | +## DT Table playground |
| 18 | +Play with DT |
| 19 | + |
| 20 | +```{r dt-plan , echo=FALSE , message=FALSE} |
| 21 | +library(DT) |
| 22 | +iris2 = head(iris, 20) |
| 23 | +options(DT.options = list(pageLength = 5)) |
| 24 | +# default Bootstrap style in DT |
| 25 | +#datatable(iris2, style = 'bootstrap') |
| 26 | +
|
| 27 | +datatable(head(iris), colnames = c('Here', 'Are', 'Some', 'New', 'Names')) |
| 28 | +
|
| 29 | +datatable(iris2, colnames = c('Here', 'Are', 'Some', 'New', 'Names') , |
| 30 | + style = 'bootstrap', class = 'table-bordered') |
| 31 | +
|
| 32 | +
|
| 33 | +``` |
| 34 | + |
| 35 | +```{r} |
| 36 | +m = matrix(c( |
| 37 | + '<b>Bold</b>', '<em>Emphasize</em>', '<a href="http://rstudio.com">RStudio</a>', |
| 38 | + '<a href="#" onclick="alert(\'Hello World\');">Hello</a>' |
| 39 | +), 2) |
| 40 | +colnames(m) = c('<span style="color:red">Column 1</span>', '<em>Column 2</em>') |
| 41 | +datatable(m , escape = FALSE) # escape = TRUE by default |
| 42 | +``` |
| 43 | + |
| 44 | + |
| 45 | + |
| 46 | +```{r} |
| 47 | +library(shiny) |
| 48 | +
|
| 49 | +
|
| 50 | +
|
| 51 | +A <- c("Alpha", "Beta", "Gamma", "Delta") |
| 52 | +B <- c("one","two","three","four") |
| 53 | +C <- c("five","six","seven","eight") |
| 54 | +
|
| 55 | +
|
| 56 | +Test_File <- tibble::tibble("A" = A , "B" = B , "C" = C) |
| 57 | +
|
| 58 | +renderDataTable( |
| 59 | + data <- Test_File %>% |
| 60 | + dplyr::select(A,B) %>% |
| 61 | + dplyr::mutate(URL = paste0("https://www.testsite.com/abcdefg/", A))%>% |
| 62 | + dplyr::mutate(URL = paste0("<a href='", URL, "'>",A,"</a>")) |
| 63 | + , escape = FALSE) |
| 64 | +
|
| 65 | +
|
| 66 | +``` |
| 67 | + |
| 68 | + |
| 69 | +## Inputs and Outputs |
| 70 | + |
| 71 | +You can embed Shiny inputs and outputs in your document. Outputs are automatically updated whenever inputs change. This demonstrates how a standard R plot can be made interactive by wrapping it in the Shiny `renderPlot` function. The `selectInput` and `sliderInput` functions create the input widgets used to drive the plot. |
| 72 | + |
| 73 | +```{r eruptions, echo=FALSE} |
| 74 | +inputPanel( |
| 75 | + selectInput("n_breaks", label = "Number of bins:", |
| 76 | + choices = c(10, 20, 35, 50), selected = 20), |
| 77 | + |
| 78 | + sliderInput("bw_adjust", label = "Bandwidth adjustment:", |
| 79 | + min = 0.2, max = 2, value = 1, step = 0.2) |
| 80 | +) |
| 81 | +
|
| 82 | +renderPlot({ |
| 83 | + hist(faithful$eruptions, probability = TRUE, breaks = as.numeric(input$n_breaks), |
| 84 | + xlab = "Duration (minutes)", main = "Geyser eruption duration") |
| 85 | + |
| 86 | + dens <- density(faithful$eruptions, adjust = input$bw_adjust) |
| 87 | + lines(dens, col = "blue") |
| 88 | +}) |
| 89 | +``` |
| 90 | + |
| 91 | +## Embedded Application |
| 92 | + |
| 93 | +It's 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: |
| 94 | + |
| 95 | +```{r tabsets, echo=FALSE} |
| 96 | +shinyAppDir( |
| 97 | + system.file("examples/06_tabsets", package = "shiny"), |
| 98 | + options = list( |
| 99 | + width = "100%", height = 550 |
| 100 | + ) |
| 101 | +) |
| 102 | +``` |
| 103 | + |
| 104 | +Note the use of the `height` parameter to determine how much vertical space the embedded application should occupy. |
| 105 | + |
| 106 | +You can also use the `shinyApp` function to define an application inline rather then in an external directory. |
| 107 | + |
| 108 | +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. |
| 109 | + |
| 110 | + |
| 111 | + |
0 commit comments