forked from rstudio/ShinyDeveloperConference
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Unsolution_02.R
66 lines (51 loc) · 1.34 KB
/
Unsolution_02.R
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
library(shiny)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
selectInput("xcol", "X variable", names(iris)),
selectInput("ycol", "Y variable", names(iris), names(iris)[2]),
numericInput("rows", "Rows to show", 10)
),
mainPanel(
tabsetPanel(
tabPanel("Data", br(),
tableOutput("table")
),
tabPanel("Summary", br(),
verbatimTextOutput("dataInfo"),
verbatimTextOutput("modelInfo")
),
tabPanel("Plot", br(),
plotOutput("plot")
)
)
)
)
)
server <- function(input, output, session) {
# Don't do this!
# Introduce reactive value for each calculated value
values <- reactiveValues(selected = NULL, model = NULL)
# Use observers to keep the values up-to-date
observe({
values$selected <- iris[, c(input$xcol, input$ycol)]
})
observe({
values$model <- lm(paste(input$ycol, "~", input$xcol), values$selected)
})
# The outputs all use the reactive values
output$plot <- renderPlot({
plot(values$selected)
abline(values$model)
})
output$modelInfo <- renderPrint({
summary(values$model)
})
output$dataInfo <- renderPrint({
summary(values$selected)
})
output$table <- renderTable({
head(values$selected, input$rows)
})
}
shinyApp(ui, server)