-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathapp_simple.R
executable file
·78 lines (50 loc) · 1.96 KB
/
app_simple.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
67
68
69
70
71
72
73
74
75
76
77
78
##############################
# This is a simple shiny app.
# To run it, just press the "Run App" button on upper right of this panel.
##############################
##############################
# Below is the setup code that runs only once at startup
# when the shiny app is started.
# In the setup code you can load packages, define functions
# and variables, source files, and load data.
ndata <- 1e4
stdev <- 1.0
# End setup code
##############################
##############################
## Define the user interface
uifun <- shiny::fluidPage(
titlePanel("A Simple Shiny App"),
## Create interface for the input parameters.
# Create numeric input for the number of data points.
numericInput("ndata", "Number of data points:", value=ndata),
# Create slider input for the standard deviation parameter.
sliderInput("stdev", label="Standard deviation:",
min=0.1, max=3.0, value=stdev, step=0.1),
## Produce output.
# Render plot in a panel.
plotOutput("plotobj", height=300, width=500)
) # end user interface
##############################
## Define the server function, with the arguments "input" and "output".
# The server function performs the calculations and creates the plots.
servfun <- function(input, output) {
## Recalculate the model with new parameters
# The function shiny::reactive() accepts a block of expressions
# which calculate the model, and returns the model output.
datav <- shiny::reactive({
cat("Calculating the data\n")
# Simulate the data
rnorm(input$ndata, sd=1)
}) # end reactive code
# Plot the data
output$plotobj <- shiny::renderPlot({
cat("Plotting the data\n")
datav <- input$stdev*datav()
# Plot the data
par(mar=c(2, 4, 4, 0), oma=c(0, 0, 0, 0))
hist(datav, xlim=c(-4, 4), main="Histogram of Random Data")
}) # end renderPlot
} # end servfun
## Return a Shiny app object
shiny::shinyApp(ui=uifun, server=servfun)