/20
+2 completion (turned in on time, turned in Rmd and Html…)
The below code sets up a data frame, df that you will
use in order to create a shiny app that compares the t distribution to
the Z distribution at different levels of degrees of freedom (n-1). Your
n term will be the input value for your shiny app.
library(shiny)
library(ggplot2)
## x <- seq(-4, 4, by=0.01)
## norm_dens <- dnorm(x)
## t_dens <- dt(x, df = n-1)
## df = data.frame(x = x, z = norm_dens, t = t_dens)
Write your code for the shiny app below. I’ve set up the UI, Server, and Run steps. You will fill in the details so that when you run the user interface and server logic together, the output will include:
Static standard Normal distribution curve in blue
using ggplot geom_line()
Dynamic t distribution curve that is dependent on degrees of
freedom (n-1) in red using ggplot
geom_line()
A slider input that determines the sample size from minimum value of 2 (since df can be no less than 1) to maximum value of 100, stepping by 1
+2 Normal distribution is shown in blue, is static (changing n will not change the shape of the curve) +2 t distribution depends on the sample size (n) via its degrees of freedom (changing n will make the t approach the Normal distribution in shape) +1 t distribution is red (+1) +2 slider changes the sample size from 2 to 100, stepping by 1 +2 Using probability (or density or rel freq) on y axis and x on x-axis where x ranges from -4 to 4
## UI function
ui <- fluidPage(
titlePanel("Normal vs t Distribution"),
sidebarLayout(
sidebarPanel(
sliderInput(
inputId = "n", # n changes becasue degree of freedom is (n - 1)
label = "Sample Size (n):",
min = 2,
max = 100,
value = 10,
step = 1
)
),
mainPanel(
plotOutput("distPlot")
)
)
)
# Server logic
server <- function(input, output) {
output$distPlot <- renderPlot({
x <- seq(-4, 4, by = 0.01)
norm_dens <- dnorm(x)
t_dense <- dt(x, df = input$n - 1)
df <- data.frame(
x = x,
z = dnorm(x),
t = dt(x, df = input$n -1)
)
ggplot(df, aes(x = x)) +
geom_line(aes(y = z), color = "blue", linewidth = 1) +
geom_line(aes(y = t), color = "red", linewidth = 1) +
labs(
x = "x",
y = "Density",
title = paste("Normal and t Distribution (n =", input$n, ")")
) +
theme_minimal()
})
}
## Run shiny app
shinyApp(ui, server)
Using the iris data set, create a scatterplot using the
shiny app. The graph should include some interactive capability to
select which variables to compare on the x and y axes.
+4 x and y variables can be selected in a sidebar layout +1 user interface (sidebar) interacts with the server so that selections appear in graph +1 all combinations of data are displayed as a scatterplot +1 x and y axis selections change correct axes +2 formatting Is legible and seems to be showing the data appropriately
## UI Function
library(shiny)
ui <- fluidPage(
titlePanel("Iris Scatter Plot"),
sidebarLayout(
sidebarPanel(
selectInput(
inputId = "xvar",
label = "Choose x-axis variable:",
choices = names(iris)[1:4],
selected = "Sepal.Length"
),
selectInput(
inputId = "yvar",
label = "Choose y-axis variable:",
choices = names(iris)[1:4],
selected = "Petal.Length"
)
),
mainPanel(
plotOutput("scatterPlot")
)
)
)
## Server logic
server <- shinyServer(function(input, output) {
output$scatterPlot <- renderPlot({
ggplot(iris, aes(x = .data[[input$xvar]], y = .data[[input$yvar]], color = Species)) + # Species included for easier reading
geom_point(size = 2) +
labs(
x = input$xvar,
y = input$yvar,
title = "Iris Scatterplot"
) +
theme_minimal()
})
}
)
## Run shiny app
shinyApp(ui, server)