【发布时间】:2021-03-09 23:44:48
【问题描述】:
我正在学习制作我的第一个关于模拟的闪亮应用。我试图用预先编写的代码制作一个闪亮的网络图,但是当我运行应用程序时没有出现任何图。我不确定我错过了什么,如果有人能指出来,我会非常感激。谢谢!
这是我的 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
)
这是我的服务器代码。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)
})
}
还有我的闪亮应用程序代码
#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)
【问题讨论】:
标签: r shiny shinydashboard shinyapps shiny-reactivity