I am learning to make my first shiny app about a simulation. I tried to make a network graph in shiny with pre-written code, but no graph appeared when I run the app. I am not sure what did I miss and would really appreciate it if someone can point it out. Thanks!
here's my code for the ui.R.
library(shiny)
library(shinythemes)
library(shinydashboard)
library(igraph)
# Define UI
ui <- fluidPage(
theme = shinytheme("flatly"),
navbarPage(
"Can't I just go to one party?",
tabPanel(
"Network Graph",
sidebarPanel(
sliderInput(
"n.people",
"Number of people who lived near campus:",
min = 1,
max = 1000,
value = 500
),
sliderInput(
"n.workers",
"Number of people have in-person jobs:",
min = 1,
max = 10,
value = 5
),
sliderInput(
"n.roommates",
"Number of rommates to live with:",
min = 1,
max = 10,
value = 5
),
plotOutput("hist")
),
# sidebarPanel
mainPanel(
h1("Network Simulation"),
h4("How connected are we?"),
verbatimTextOutput("txtout"),
) # mainPanel
)
) # navbarPage) # fluidPage
)
and here's my code for the server.R
# Define server function
server <- function(input, output) {
output$hist <- renderPlot({
distribution <- matrix(0, input$n.people, input$n.people)
### Set up the coworker connections
# 50% of people randomly selected to be workers
workers <- sample(
1:input$n.people,
size = 0.5 * input$n.people,
replace = FALSE
)
# For each worker, randomly select 2 coworkers.
for (w in workers) {
while (sum(distribution[w,]) < input$n.workers + 1) {
availableWorkers <-
workers[rowSums(distribution[workers,]) < input$n.workers]
if (length(availableWorkers[which(availableWorkers != w)]) > input$n.workers -
1) {
c <- sample(availableWorkers[which(availableWorkers != w)],
size = 1,
replace = FALSE)
}
if (length(availableWorkers[which(availableWorkers != w)]) == input$n.workers -
1) {
c <- availableWorkers[which(availableWorkers != w)]
}
if (length(availableWorkers[which(availableWorkers != w)]) == 0) {
break
}
distribution[w, c] <- 1
distribution[c, w] <- 1
}
}
#Set up the roommate connection
# Everyone has exactly 1 roommate
# Person 1 is roommates with person i+1
for (i in 1:(input$n.people - input$n.roommates)) {
for (j in 1:input$n.roommates) {
distribution[i, i + j] <- 1
distribution[i + j, i] <- 1
}
}
### Everone either has 3 edges or 1 edge
### Workers have 3, nonworkers have 1
distribution_graph <-
graph_from_adjacency_matrix(distribution, "undirected")
plot(distribution_graph)
})
}
and my code for the shiny app itself
#loading the necessary libraries and packages
library(shiny)
library(plotly)
library(dplyr)
library(ggplot2)
source("ui.R")
source("server.R")
# Calling the other files
shinyApp(ui = ui, server = server)
I tested the app with smaller default values of your inputs ( n.people
= 50, n.workers
= 3, n.roommates
= 3) and this works fine. When I increase the values using the sliders, at some point the app does not react anymore. There is no clear breakpoint: sometimes it works with a given parameter setting, and the next time it stops, which is probably due to the sampling process. So, it seems that what you are doing is just too memory-heavy for R.
There is certainly a limit to matrix dimensions, and if this is the problem, than you cannot really do anything about it. But maybe you can re-write your loops such that they need less memory?, I recommend doing the testing outside the shiny app. because shiny itself is not the problem here.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.