-
Notifications
You must be signed in to change notification settings - Fork 1
Resuable Code Chunks
LeahHunt edited this page Jul 1, 2020
·
5 revisions
Let's see about using these wiki pages to store reusable code chunks.
- Click on the Edit button.
- Scroll down the window to the bottom, where you should see three asterisks.
- Make a new line underneath those asterisks and click the h2 button above.
- Enter in a title for the reusable portion of the code.
- Return to a new line and type up any information about the code.
- Place a blank line between the info and where you paste the code.
- Before pasting in your code, type three back ticks (`) in a row, without spaces.
- Paste your code.
- In a new line immediately after your last line of code, type another three back ticks in a row, without spaces.
- Return to a new line and type three asterisks (or press the button that has a single horizontal line).
- Press the Save Page button.
This code allows the user to pick a population from seven choices: left skewed, right skewed, symmetric, bimodal, astragalus, playlist, or accident rate. For each of these, the user can then choose the number of samples to draw and the number of times to repeat sampling. Note that some portions may not style properly if you aren't using the style sheet.
In the UI:
# Layout for the population picker
sidebarLayout(
sidebarPanel(
width=6,
fluidRow(
column(
6,
# Select Input for the distribution type
selectInput(
"popDist",
"Population type",
list(
"Left-skewed" = "leftskewed",
"Right-skewed" = "rightskewed",
"Symmetric" = "symmetric",
"Bimodal" = "bimodal",
"Astragalus (Bone Die)" =
"astragalus",
"Playlist" =
"ipodshuffle",
"Accident Rate" = "poisson"
)
),
# Conditional Panel for type of population distribution
# Left Skewed
conditionalPanel(
condition = "input.popDist=='leftskewed'",
sliderInput(
"leftskew",
" Skewness",
min = 0,
max = 1,
value = .5,
step = 0.1,
ticks=FALSE
),
div(style = "position: absolute; left: 0.5em; top: 9em", "min"),
div(style = "position: absolute; right: 0.5em; top: 9em", "max"),
)
,
# Right Skewed
conditionalPanel(
condition = "input.popDist=='rightskewed'",
sliderInput(
"rightskew",
"Skewness",
min = 0,
max = 1,
value = .5,
step = .01,
ticks=FALSE
),
div(style = "position: absolute; left: 0.5em; top: 9em", "min"),
div(style = "position: absolute; right: 0.5em; top: 9em", "max"),
),
#Symmetric
conditionalPanel(
condition = "input.popDist=='symmetric'",
sliderInput(
"inverse",
"Peakedness",
min = 0,
max = 1,
value = .5,
step = 0.01,
ticks=FALSE
),
div(style = "position: absolute; left: 0.5em; top: 9em", "U"),
div(style = "position: absolute; left: 0.5em; top: 10em", "Shaped"),
div(style = "position: absolute; right: 0.5em; top: 9em", "Bell"),
div(style = "position: absolute; right: 0.5em; top: 10em", "Shaped"),
),
# Bimodal
conditionalPanel(
condition = "input.popDist=='bimodal'",
sliderInput(
"prop",
"% under right mode",
min = 10,
max = 90,
value = 50,
ticks=F,
post="%",
#grid_num=9
#interval = 1
)
),
# Poisson
conditionalPanel(
condition = "input.popDist=='poisson'",
sliderInput(
"poissonmean",
"Mean",
min = 0,
max = 10,
value = 1,
step = 0.1
),
conditionalPanel(
condition="input.poissonmean==0",
"Note: When the mean is set to 0, the number of accidents is always 0, so the variance is 0."
)
),
#iPod shuffle
conditionalPanel(
condition = "input.popDist == 'ipodshuffle'",
column(
width = 7,
offset = 0,
p("Number of songs:"),
# Inputs for the probabilites of each music type
numericInput(
"s1",
"Jazz",
1,
min = 0,
max = 200,
step = 1,
width="75px"
),
numericInput(
"s2",
"Rock",
1,
min = 0,
max = 200,
step = 1,
width="75px"
),
numericInput(
"s3",
"Country",
1,
min = 0,
max = 200,
step = 1,
width="75px"
),
numericInput(
"s4",
"Hip-hop",
1,
min = 0,
max = 200,
step = 1,
width="75px"
)
),
) #This parenthesis ends the iPod Shuffle Conditional Panel
), #Ends inputs column
# Inputs for each type:
column(
6,
#left skewed
conditionalPanel(
condition = "input.popDist == 'leftskewed'",
# Choose number of paths
sliderInput(
"leftpath",
"Number of paths",
min = 1,
max = 5,
value = 1
),
# Choose sample size
sliderInput(
"leftsize",
"Sample size (n)",
min = 10,
max = 1000,
value = 100
)
),
# Right skewed
conditionalPanel(
condition = "input.popDist == 'rightskewed'",
# Choose the number of sample means
sliderInput(
"rightpath",
"Number of paths",
min = 1,
max = 5,
value = 1
),
# Choose the number of sample means
sliderInput(
"rightsize",
"Sample size (n)",
min = 10,
max = 1000,
value = 100
)
),
# Symmetric
conditionalPanel(
condition = "input.popDist == 'symmetric'",
# Choose the number of paths
sliderInput(
"sympath",
"Number of paths",
min = 1,
max = 5,
value = 1
),
# Choose the number of sample means
sliderInput(
"symsize",
"Sample size (n)",
min = 10,
max = 1000,
value = 100
)
),
# Astragulus
conditionalPanel(
condition = "input.popDist == 'astragalus'",
# Choose number of paths
sliderInput(
"aspath",
'Number of paths',
min = 1,
max = 5,
value = 1
),
# Choose sample size
sliderInput(
"assize",
"Number of trials",
min = 10,
max = 1000,
value = 100
)
),
# Bimodal
conditionalPanel(
condition = "input.popDist == 'bimodal'",
# Choose the number of paths
sliderInput(
"bipath",
"Number of paths",
min = 1,
max = 5,
value = 1
),
# Choose the number of sample means
sliderInput(
"bisize",
"Sample size (n)",
min = 10,
max = 1000,
value = 100
)
),
# Poisson
conditionalPanel(
condition = "input.popDist == 'poisson'",
# Choose the number of paths
sliderInput(
"poissonpath",
"Number of paths",
min = 1,
max = 5,
value = 1
),
# Choose the number of sample means
sliderInput(
"poissonsize",
"Sample size (n)",
min = 10,
max = 1000,
value = 100
)
),
# Playlist
conditionalPanel(
condition = "input.popDist == 'ipodshuffle'",
# Choose number of paths
sliderInput(
"ipodpath",
label = "Number of paths",
min = 1,
max = 5,
value = 1
),
# Choose sample size
sliderInput(
"ipodsize",
label = "Sample size (n)",
min = 10,
max = 1000,
value = 100
),
# Buttons to choose music type
radioButtons(
"ptype",
"Genre to track:",
list("Jazz",
"Rock",
"Country",
"Hip-hop"),
selected = "Jazz"
)
)
)
)
), #End of column for slider inputs
mainPanel(
width = 6,
# Plots for each distribution; either histogram or density
conditionalPanel(condition = "input.popDist == 'leftskewed'",
plotOutput('plotleft1')),
conditionalPanel(condition = "input.popDist == 'rightskewed'",
plotOutput('plotright1')),
conditionalPanel(condition = "input.popDist == 'symmetric'",
plotOutput('plotsymmetric1')),
conditionalPanel(condition = "input.popDist == 'astragalus'",
plotOutput("pop")),
conditionalPanel(condition = "input.popDist == 'bimodal'",
plotOutput('plotbiomodel1')),
conditionalPanel(condition = "input.popDist == 'poisson'",
plotOutput('poissonpop')),
conditionalPanel(condition = "input.popDist == 'ipodshuffle'",
plotOutput("iPodBarPlot")
)
)
)
)
In the server:
# Function to create density plots for each group
# Inputs: Dataframe consisting of columns x and y to define axes, limits for x axis in form c(lower, upper), optional path for symmetric case
# Output: ggplot of density
makeDensityPlot <- function(data, xlims, path=0){
plot<-ggplot2::ggplot(aes(x=x, y=y), data= data) +
geom_path(color="#0072B2", size=1.5) +
xlim(xlims) +
xlab("Value") +
ylab("Density") +
ggtitle("Population Graph")+
theme(axis.text = element_text(size=18),
plot.title = element_text(size=18, face="bold"),
axis.title = element_text(size=18),
panel.background = element_rect(fill = "white", color="black")
)
# For case in symmetric where path is 1 causing "box" shape
if(path ==1){
plot<-plot+
geom_segment(aes(x=0, y=0, xend=0, yend=1), color="#0072B2", size=1.5)+
geom_segment(aes(x=1, y=0, xend=1, yend=1), color="#0072B2", size=1.5)
}
plot
}
# Function to create bar plots for each group
# Inputs: x axis label (string), dataframe consisting of either column x or columns x and y to define axes
# Output: ggplot of resulting bar plot
makeBarPlot<-function(xlab, data, levels=as.character(data$x)){
plot<-ggplot(aes(x=factor(x, levels=levels), y=y), data= data) +
geom_bar(stat = "identity", fill="#0072B2") +
ylim(c(0, max(data$y)+.1*max(data$y)))+
xlab(xlab) +
ylab("Probability") +
ggtitle("Population Graph")+
theme(axis.text = element_text(size=18),
plot.title = element_text(size=18, face="bold"),
axis.title = element_text(size=18),
panel.background = element_rect(fill = "white", color="black"))+
scale_x_discrete(drop=FALSE)
plot
}
###################################################################
## Left skewed
####################################################################
leftSkew<-reactive({11-10*input$leftskew})
# Population of left skewed
output$plotleft1 <- renderCachedPlot({
# Define parameters for density plot
x <- seq((leftSkew()) - 9 * sqrt((leftSkew())),0, length = input$symsize)
y <- dgamma(-x, shape = (leftSkew()), beta = 1)
data<-data.frame(x=x, y=y)
# Make Density Plot
makeDensityPlot(data=data, xlims = c((leftSkew()) - 9 * sqrt((leftSkew())), 0))
},
cacheKeyExpr = {
list(input$leftskew)
})
###################################################################
## Right skewed
####################################################################
rightSkew<-reactive({11-10*input$rightskew})
# Population of right skewed
output$plotright1 <- renderCachedPlot({
# Define parameters for density plot
x <- seq(0, (rightSkew()) + 9 * sqrt(rightSkew()), length = input$symsize)
y <- dgamma(x, shape = (rightSkew()), beta = 1)
data<-data.frame(x=x, y=y)
# Make the density plot
makeDensityPlot(data=data, xlims = c(0, (rightSkew()) + 9 * sqrt((rightSkew()))))
},
cacheKeyExpr = {
list(input$rightskew)
})
###################################################################
## Symmetric skewed
####################################################################
inverse<-reactive({round(14.6*input$inverse^3-5.7*input$inverse^2 + input$inverse+.1,3)})
# Population of Symmetric skewed
output$plotsymmetric1 <- renderCachedPlot({
x <- seq(0, 1, length = input$symsize)
dens <-
dbeta(x,
shape1 = inverse(),
shape2 = inverse())
data <- data.frame(x = x, y = dens)
# Make density plot separated by case where the peakedness is exactly 1 (causes a "box" shape)
makeDensityPlot(data = data, xlims = c(-0.03, 1.03), path=inverse())
},
cacheKeyExpr = {
list(input$symsize, input$inverse)
})
###################################################################
## Bimodal
####################################################################
# Population for bimodel
prop<-reactive({input$prop/100})
output$plotbiomodel1 <- renderCachedPlot({
# Define parameters for density plot
t <- 1 / (input$bisize * input$bipath)
y <- seq(0, 1, t)
z <- seq(1, 0,-t)
leftdraw <- dbeta(z, 4,14)*.2
rightdraw <- dbeta(y, 4,14) *.2
data<-data.frame(x = seq(0, 5, t*5), y = prop() * leftdraw + (1 - prop()) * rightdraw)
# Make the density plot
makeDensityPlot(data = data, xlims = c(0,5))
},
cacheKeyExpr = {
list(input$prop)
})
###################################################################
## Accident Rate
####################################################################
# Population of Poisson
output$poissonpop <- renderCachedPlot({
data<-data.frame(x=0:ceiling(2*input$poissonmean+5)) # More x's than necessary
data$y<-(input$poissonmean^data$x) * exp(-input$poissonmean)/factorial(data$x) # Get y vals for x's
data<-rbind(data[1:2,], filter(data[-c(1,2), ], y>.0005)) # Filter based on probability
makeBarPlot(xlab= "Number of accidents", data= data)
},
cacheKeyExpr = {
list(input$poissonmean)
})
###################################################################
## Astrugulas
####################################################################
# Die results
die <- reactive({
die <- c(rep(1, 1), rep(3, 4), rep(4, 4), rep(6, 1))
})
# Population of Astragalus
output$pop <- renderPlot({
data<-data.frame(x=c(1,3,4,6), y=c(.1,.4,.4,.1))
makeBarPlot(xlab= "Number on roll of astragalus", data= data, levels=1:6)
})
# Matrix of sample values
drawAdie <-
reactive(matrix(
sample(die(), input$aspath * input$assize,
replace = TRUE),
nrow = input$assize,
ncol = input$aspath
))
###################################################################
## iPOD SHUFFLE
####################################################################
# Reactive expression to get the number of songs of the chosen type
nSongs<-reactive({
if(input$ptype=="Jazz"){
nSongs <- input$s1
}
else if(input$ptype=="Rock"){
nSongs <- input$s2
}
else if(input$ptype=="Country"){
nSongs <- input$s3
}
else{
nSongs <- input$s4
}
})
# Set up songs from four types
songs <- reactive({
songs <- c(rep(input$s1),
rep(input$s2),
rep(input$s3),
rep(input$s4))
})
# Bar plot
output$iPodBarPlot <- renderCachedPlot({
# Parameters for bar plot
p <- nSongs() / sum(songs())
data<-data.frame(x = c("Other music (0)", paste(input$ptype,"(1)")), y=c(1-p, p))
data$x<-factor(data$x, levels=data$x) # Done to force sorted order for bars
# Make bar plot
makeBarPlot(xlab= "Genre", data= data)
},
cacheKeyExpr = {
list(input$s1, input$s2, input$s3, input$ptype, input$s4, input$ipodsize)
})