它。听起来你想要一个submitButton而不是ActionButtonJDHarrison——我也是这么想的,但是当我使用submitButton时,UI不能正确显示,我想我知道actionButton比submitButton更健壮、更灵活。当然,当
它。听起来你想要一个submitButton而不是ActionButtonJDHarrison——我也是这么想的,但是当我使用submitButton时,UI不能正确显示,我想我知道actionButton比submitButton更健壮、更灵活。当然,当,r,shiny,R,Shiny,它。听起来你想要一个submitButton而不是ActionButtonJDHarrison——我也是这么想的,但是当我使用submitButton时,UI不能正确显示,我想我知道actionButton比submitButton更健壮、更灵活。当然,当我使用actionButton时,UI可以工作。 # filename: ui.r # Michael Camann, Humboldt State University, 2014 #############################
它。听起来你想要一个submitButton而不是ActionButtonJDHarrison——我也是这么想的,但是当我使用submitButton时,UI不能正确显示,我想我知道actionButton比submitButton更健壮、更灵活。当然,当我使用actionButton时,UI可以工作。
# filename: ui.r
# Michael Camann, Humboldt State University, 2014
################################################################################
#
# Define the user interface
#
################################################################################
library(shiny)
shinyUI(
fluidPage(
titlePanel("BIOL 330 ecological simulations"),
sidebarLayout(
sidebarPanel("",
helpText(HTML("<h3 style='text-align:center;'>Control Panel</h3>"), align='center'),
tags$hr(style='height:2px; border-width:0; color:gray; background-color:gray'),
# choose a simulation from a drop down menu
selectInput("sim", HTML("<b>Select a simulation to explore:</b>"),
# list the simulations available
c("No simulation selected (Introduction)" = "none",
"Dorriefish growth/reproduction trade-offs" = "dorriefish",
"Optimal foraging" = "optfor")
),
tags$hr(style='height:2px; border-width:0; color:gray; background-color:gray'),
conditionalPanel(condition="input.sim=='dorriefish'",
helpText(HTML("<b>Simulation model parameters:</b>")),
sliderInput("S.df", label=div(HTML("Specify <em>S</em>, the switch point mass (g) for
transition from somatic growth to reproduction:")),
min = 1, max = 50, value = 10, step=5),
sliderInput("p.df", label=div(HTML("Specify <em>p</em>, the probability of mortality
by predation in any given time step:")),
min = 0, max = 1, value = 0.12, step=0.01),
sliderInput("reps.df", label=div(HTML("Specify the number of full simulations to
run:")), min = 1, max = 100, value = 1, step=1)
),
# bottom controls common to all panels
conditionalPanel(condition="input.sim!='none'",
tags$hr(style='height:2px; border-width:0; color:gray; background-color:gray'),
fluidRow(column(4, actionButton("runSim", "Run simulation")),
column(4, actionButton("saveSim", "Save output file")),
column(4, actionButton("printSim", "Save/print plot"))),
tags$hr(style='height:2px; border-width:0; color:gray; background-color:gray')
)
),
mainPanel("",
# no model selected-- show information page
conditionalPanel(condition="input.sim=='none'",
includeHTML("www/simNoneText.html")
),
tabsetPanel(id="outTabs", type="tabs",
tabPanel("Plot", plotOutput("modl.plot")
),
tabPanel("Summary"
),
tabPanel("R Code"
)
)
)
)
)
)
# filename: server.r
# Michael Camann, Humboldt State University, 2014
library(shiny)
################################################################################
#
# Dorrie fish reproduction/growth trade-offs
#
################################################################################
# This function implements the Doriefish simulation exercise distributed in Java
# with Cain et al Ecology, 2nd ed.
# Arguments:
#
# S The switch point in grams for transitioning from growth to reproduction.
# Default = 10 g.
#
# p Probability of predation. Default = 0.12.
#
# show Logical. If TRUE (default), print and plot single simulation output. Set
# to FALSE when running multiple simulations in order to average the number
# of offspring across multiple simulations, in which case viewing the
# results of individual simulations isn't necessary.
df.sim <- function(S=10, p=0.12, show=TRUE)
{
if (S < 10 | S > 50) stop("S must be between 10 and 50 grams.")
doriefish <- 10 # cohort size
imass <- 5 # initial mass
mass <- rep(imass, doriefish) # init vector of dorrie fish mass
dead <- rep(FALSE, doriefish) # init vector of mortality status
mhistory <- matrix(dead, nrow=1, byrow=TRUE) # init a matrix of mortality history
offspring.t <- 0 # init offspring count
total.offspring <- vector() # storage vector
while(any(dead==FALSE)) # continue until all are dead
{
repro.t <- mass >= S & !dead # who reproduces during this time step?
offspring.t <- offspring.t + sum(mass[repro.t==TRUE]/5) # one offspring per 5 g body mass
total.offspring <- c(total.offspring, offspring.t) # keep track of offspring each time step
mass[!dead & !repro.t] <- mass[!dead & !repro.t] + 5 # incr mass by 5 g if not reproducing
die.t <- sapply(1:length(dead), function(x) runif(1) <= p) # stochastic selection of some for predation
dead <- dead | die.t # update the mortality vector
mhistory <- rbind(mhistory, dead) # update the mortality history matrix
}
rownames(mhistory) <- c(1:nrow(mhistory))
rval <- list(S=S, p=p, mass=mass, mhistory=mhistory, offspring.t=total.offspring,
offspring=total.offspring[length(total.offspring)], show=show)
class(rval) <- "df.sim"
return(rval)
}
print.df.sim <- function(sim) # print function for df.sim()
{
if(sim$show)
{
cat("\n\tDoriefish simulation\n\n")
cat("Switch point from growth to reproduction:", sim$S, "g.\n\n")
cat("Probability of predation:", sim$p, "per time step.\n\n")
cat("Time to cohort extinction:", length(sim$offspring.t), "time steps.\n\n")
cat("Total adult Doriefish biomass:", sum(sim$mass), "g.\n\n")
cat("Total offspring produced:", sim$offspring, "\n\n")
plot(sim)
}
}
plot.df.sim <- function(sim, sstep=FALSE, s=0.75, ...) # plot function for df.sim()
{
opar <- par(no.readonly=TRUE) # save graphics parameters
txt <- paste("Total offspring:", sim$offspring,
" Time to cohort extinction:", length(sim$offspring.t), "time steps.")
step.through <- function(sim, s=0.5, txt=txt)
{
len <- length(sim$offspring.t)
par(fig=c(0,1,0.1,0.9), xpd=NA)
plot(sim$offspring.t, type="n", xlab="Time steps", ylab="Cumulative Doriefish offspring",
xlim=c(1, max(len, length(sim$mass))))
mtext("Dorriefish living per time step (green = alive, red = dead):", side=3, at=(max(len)/2)+0.5,
line=4.2)
for(i in 1:nrow(sim$mhistory))
{
z <- rep("green", length(sim$mass))
z[sim$mhistory[i,]] <- "red"
points(seq(1,len, length=length(sim$mass)), rep(max(1.18*sim$offspring.t), length(sim$mass)),
pch=21, col="black", bg=z, cex=2.5)
lines(sim$offspring.t[1:i], type="h")
Sys.sleep(s)
}
mtext(txt, side=1, at=0, line=5, adj=0)
}
if(sstep) step.through(sim, s, txt)
else
{
par(fig=c(0,1,0.1,1), xpd=NA)
plot(sim$offspring.t, type="h", xlab="Time steps",
ylab="Cumulative Doriefish offspring", ...)
mtext(txt, side=1, at=0, line=5, adj=0)
}
par(opar)
}
################################################################################
#
# Define the shiny server
#
################################################################################
shinyServer(function(input, output) {
observe({
if(input$runSim == 0) return()
isolate({
sim <- reactive({
switch(input$sim,
dorriefish = {
df.sim(input$S.df, input$p.df, show=FALSE)
} # end case dorriefish
) # end switch(input$sim)
}) # end reactive()
output$modl.plot <- renderPlot({
switch(input$sim,
dorriefish = {
if (input$reps.df == 1)
{
dev.new()
plot(sim(), sstep=TRUE)
}
}, # end case dorriefish
) # end switch(input$sim)
}) # end renderPlot()
}) # end isolate()
}) # end observe()
}) # end shinyServer()
shinyServer(function(input, output) {
sim <- reactive({
switch(input$sim,
dorriefish = {
df.sim(input$S.df, input$p.df, show=FALSE)
} # end case dorriefish
) # end switch(input$sim)
}) # end reactive()
output$modl.plot <- renderPlot({
if(input$runSim == 0) return()
isolate({
switch(input$sim,
dorriefish = {
if (input$reps.df == 1)
{
plot(sim(), sstep=TRUE)
}
}, # end case dorriefish
) # end switch(input$sim)
}) # end isolate
}) # end renderPlot()
}) # end shinyServer()
shinyServer(
function(input, output) {
observeEvent(input$run, {
# simulation code
})
})
actionButton("run", label = "Run Simulation")