【问题标题】:Why RenderPlot does not produce any graph in shiny app?为什么 RenderPlot 不会在闪亮的应用程序中生成任何图形?
【发布时间】: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


    【解决方案1】:

    我使用较小的输入默认值(n.people = 50、n.workers = 3、n.roommates = 3)测试了该应用,并且效果很好。当我使用滑块增加值时,应用程序在某些时候不再反应。没有明确的断点:有时它与给定的参数设置一起工作,而下一次它停止,这可能是由于采样过程。因此,您所做的似乎对 R 来说太占内存了。

    矩阵维度肯定是有限制的,如果这是问题所在,那么你就无法真正做任何事情。但也许你可以重写你的循环,这样它们需要更少的内存?!我建议在闪亮的应用程序之外进行测试,因为闪亮本身不是这里的问题。

    【讨论】:

    • 谢谢!您能否提供一些关于在内存使用方面优化代码的示例/建议/资源?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-26
    • 2014-11-17
    • 2021-08-27
    • 1970-01-01
    • 2020-07-09
    • 2017-07-10
    相关资源
    最近更新 更多