我正在尝试模拟Shiny中的Polya urn模型,因此用户可以输入urn包含的黑白球数量,他想要多少次迭代和试验,然后情节将显示每个试验如何进行,以瓮中白球的比例 .
这是我第一次使用Shiny,我想出了如何使用程序的核心,即如何使用户能够设置变量,如何打印出我使用的矩阵表(我不需要,只是试了一下) .
这是我的Shiny代码(没有任何输出):
library(shiny)
library(plotly)
library(ggplot2)
library(reshape2)
################ UI
ui <- fluidPage(
# App title
titlePanel("Hello Shiny!"),
###### this following part is asking the user for input
# input for the number of black balls
numericInput(inputId = "BB", label = "Number of black balls", 1, min = 1, max = 500),
# input for the number of white balls
numericInput(inputId = "WB", label= "Number of white balls", 1, min = 1, max = 500),
# input for the number of iterations
numericInput(inputId = "nIt", label= "Number of iterations", 50, min = 5, max = 500),
# input for the number of runs/trials
numericInput(inputId = "nRun", label= "Number of runs", 10, min = 1, max = 50),
# slider for the number of BBs as feedback
sliderInput(inputId = "BBfeedback", label = "BB Feedback", 1, min = 1, max = 10),
# slider for the number of WBs as feedback
sliderInput(inputId = "WBfeedback", label = "WB Feedback", 1, min = 1, max = 10),
# reserve a spot for the output
plotOutput(outputId = "mainPlot")
)
################ SERVER
server <- function(input, output) {
# PLOT
output$mainPlot <- renderPlot({
plot() # this is the code for plotting what i want to plot
})
}
################ SHINYAPP
shinyApp(ui = ui, server = server)
这是我的代码 - 它在R中运行没有问题 . 它是一个嵌套的for循环,它根据输入值创建一个矩阵,然后绘制结果 . 我的问题是:我应该在哪里放置嵌套的for循环?当我把它放在renderPlot部分时,它不起作用 .
M <- matrix(ncol=input$nRun, nrow=input$nIt) # matrix that will contain the proportion of WBs per each iteration (=rows) and run (=cols)
urn <- c(rep("BB", input$BB), rep("WB", input$WB)) # put together the urn based on previous input of BB and WB
for (r in 1:input$nRun) { # loop through number of runs/trials
# same as the initial BB and WB number but it will change as we draw balls from the urn
nBlack <- input$BB # this is important so that for each run, the previous run's results won't mess with the proportions
nWhite <- input$WB
# iterate thgourh the runs and iterations, fill up the matrix with the proportion of white balls
frac <- nWhite/(nBlack+nWhite) # this variable shows the proportion of white balls in the urn overall
frac_list <- c() # contains the proportion of WBs in a vector thorughout the iterations
for (i in 1:nIt){ # loop thourgh number of internvals (so for each run R calculates the whole interval one after the other)
s <- sample(urn, 1) # sample one ball randomly from the urn
if (s == "WB"){
nWhite <- nWhite + WB_feedback # update the number of WB
}else {
nBlack <- nBlack + BB_feedback # update the number of BB in case the sampled ball is black
}
urn <- append(urn, s, after = length(urn)) # put the last sampled ball into the urn (NOTE: since the sample s doesn't actually 'take out' the ball from the urn, there is no need to add an additional ball)
frac <- nWhite/(nWhite+nBlack) # update frac to get the proportion of white balls in this run
frac_list <- append(frac_list, frac)
}
M[,r] <- frac_list
}
这是将矩阵转换为数据帧并使用ggplot绘制结果的代码 . 这个部分应该与嵌套的for循环一起使用,还是在renderPlot部分中?
df <- as.data.frame(M) #transform matrix into a data frame
df$id = 1:nrow(df)
final_data <- melt(df, id='id') # putting the data into long format
names(final_data) <- c('id', 'func', 'value') # naming the columns so they can be used in the ggplot
g <- ggplot() + geom_line(data = final_data, aes(x = id, y = value, color = func, group = func), size = 1) # MAKES THE GRAPH
ggplotly(g) # plotlify the ggplot