I tried to run the Schelling segregation model in shiny. I got 3 inputes: number of houses, number of neighbors and alike_preference and sorting should be done within 1000 seconds. The problem is I don't get the output.
Also, I put my renderTable()
function both in the eventreactive()
and loop, and outside them , but still no output was shown.
library(shiny)
# UI
ui <- fluidPage(
titlePanel("Schelling’s model!"),
sidebarLayout(
sidebarPanel(
sliderInput(inputId = "Ic1",
label = "Number of Houses:",
min = 1,
max = 51,
value =1),
sliderInput(inputId = "Ic2",
label = "Number of Neighbours",
min=0,
max=2000,
step=50,
value = 0),
sliderInput(inputId = 'Ic3',
label = 'alike_preference',
min=0,
max=1,
value=0),
br(),
actionButton(inputId = 'Id8','go',
style="color: #fff; background-color: #428fd6; border-color: #2e6da4")
),
# Main panel for displaying outputs ----
mainPanel(
tableOutput(outputId = "map"),
)
)
)
#Now The cycle of changing places in 1000 seconds
server <- function(input, output) {
grid_activation1 <- reactiveVal()
grid_activation1 <- eventReactive(input$Id8 ,{
input$Ic3
group<-c(rep(0,(input$Ic1*input$Ic1)-input$Ic2,rep(1,input$Ic2/2),rep(2,input$Ic2/2)))
grid <- matrix(sample(group,input$Ic1*Input$Ic1,replace=F), ncol= input$Ic1)
image(grid,col=c("black","red","green"),axes=F)
get_neighbors<-function(coords) {
n<-c()
for (i in c(1:8)) {
if (i == 1) {
x<-coords[1] + 1
y<-coords[2]
}
if (i == 2) {
x<-coords[1] + 1
y<-coords[2] + 1
}
if (i == 3) {
x<-coords[1]
y<-coords[2] + 1
}
if (i == 4) {
x<-coords[1] - 1
y<-coords[2] + 1
}
if (i == 5) {
x<-coords[1] - 1
y<-coords[2]
}
if (i == 6) {
x<-coords[1] - 1
y<-coords[2] - 1
}
if (i == 7) {
x<-coords[1]
y<-coords[2] - 1
}
if (i == 8) {
x<-coords[1] + 1
y<-coords[2] - 1
}
if (x < 1) {
x<-51
}
if (x > 51) {
x<-1
}
if (y < 1) {
y<-51
}
if (y > 51) {
y<-1
}
n<-rbind(n,c(x,y))
}
n
}
for (t in c(1:1000)) {
happy_cells<-c()
unhappy_cells<-c()
for (j in c(1:input$Ic1)) {
for (k in c(1:input$Ic1)) {
current<-c(j,k)
value<-grid[j,k]
if (value > 0) {
like_neighbors<-0
all_neighbors<-0
neighbors<-get_neighbors(current)
for (i in c(1:nrow(neighbors))){
x<-neighbors[i,1]
y<-neighbors[i,2]
if (grid[x,y] > 0) {
all_neighbors<-all_neighbors + 1
}
if (grid[x,y] == value) {
like_neighbors<-like_neighbors + 1
}
}
if (is.nan(like_neighbors / all_neighbors)==FALSE) {
if ((like_neighbors / all_neighbors) < input$Ic3) {
unhappy_cells<-rbind(unhappy_cells,c(current[1],current[2]))
}
else {
happy_cells<-rbind(happy_cells,c(current[1],current[2]))
}
}
else {
happy_cells<-rbind(happy_cells,c(current[1],current[2]))
}
}
}
}
happiness_tracker<-append(happiness_tracker,length(happy_cells)/(length(happy_cells) + length(unhappy_cells)))
rand<-sample(nrow(unhappy_cells))
for (i in rand) {
mover<-unhappy_cells[i,]
mover_val<-grid[mover[1],mover[2]]
move_to<-c(sample(1:input$Ic1,1),sample(1:input$Ic1,1))
move_to_val<-grid[move_to[1],move_to[2]]
while (move_to_val > 0 ){
move_to<-c(sample(1:input$Ic1,1),sample(1:input$Ic1,1))
move_to_val<-grid[move_to[1],move_to[2]]
}
grid[mover[1],mover[2]]<-0
grid[move_to[1],move_to[2]]<-mover_val
}
image(grid,col=c("black","red","green"),axes=F)
# I put output plotting in for loop to see changes Continuously
output$map <- renderTable({
grid_activation1
})
}
})
}
shinyApp(ui = ui, server = server)