generated from jtr13/bookdown-template
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path06-nextsteps.Rmd
121 lines (81 loc) · 3.18 KB
/
06-nextsteps.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# Next steps{#nextsteps}
## Going fancy with Shiny Dashboards
```{r, eval = F}
library(shinyashboard)
```
This is the skeleton code for creating Shiny dashboards. There are three components to a Shiny dashboard:
1. Header
2. Sidebar
3. Body
```{r, eval = F}
library(shiny)
library(shinydashboard)
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(),
dashboardBody()
)
server <- function(input, output) { }
shinyApp(ui, server)
```
{width=100%}
## Interactive Documents
If you are familiar with writing R Markdown HTML documents, it is possible to embed Shiny widgets and render functions into them just be modifying the YAML header:
```
---
runtime: shiny
output: html_document
---
```
Then you can insert widgets and render functions into code chunks in the document:
```{r, eval=FALSE}
selectInput(inputId = "choice_select", label = "Select box",
choices = list("Choice 1" = 1, "Choice 2" = 2, "Choice 3" = 3),
selected = 1)
renderPlot({
plot(x = airquality[, 4], y = airquality[, as.numeric(input$choice_select)])
})
```
It is important to note that interactive R-Markdown documents must be hosted with Shiny Server or RStudio Connect like a normal shiny app otherwise the interactive functionality will not work. Interactive documents cannot be shared as a standalone html file, though the rmarkdown file can be shared and run locally.
For more information, check the [documentation here](https://shiny.rstudio.com/articles/interactive-docs.html)
## Render UI Elements From the Server Side
Sometimes we may to generate a UI element that is updated in response to actions we take with other UI elements or in response to calculations that have happened on the server side e.g we have a numeric widget that is filtering the number of rows in a data-frame and we also want to have an `selectInput` based on the values in a column in that data-frame.
We can control this with a `uiOutput` - this function allows us to render a UI element from the server side. E.g:
```{r, eval=FALSE}
ui <- fluidPage(
sidebarPanel(
numericInput(inputId = "filter", label = "Number of days to filter out", value = 1,
min = 0, max = 150),
uiOutput("select_out") # We have an output element in the UI, this where the
# the widget will be placed
),
mainPanel(
textOutput(outputId = "text_out")
)
)
server <- function(input, output, session) {
filtered_data <- reactive({
airquality$day_into <- paste0("Day", 1:nrow(airquality))
n_rows <- nrow(airquality) - input$filter
return(head(airquality, n = n_rows))
})
output$select_out <- renderUI({
filtered_df <- filtered_data()
d_list <- c(filtered_df$day_into)
d_list <- as.list(d_list)
# we've used a selectInput widget here
# we can use any input widget here
selectInput(
inputId = "day_select", label = "Select filtered day", choices = d_list,
selected = "Day1"
)
})
# even though that the widget was created
# on the server side we still access it with
# input$inputId
output$text_out <- renderText({
print(input$day_select)
})
}
shinyApp(ui, server)
```