0

我最担心的是它没有出错,但它只是没有显示!我必须提一下,我什至打印了每一行,以便检查进展情况,并且一切都像控制台上的常规那样出现。我猜它必须在output$visnetwork函数中,但我只是不知道。

# --------------------------------------- Global --------------------------------------- #

# Set working directory -> Ctrl+Shift+H & Open the app folder
setwd("~/Programación en R/Shiny app/Client dashboard app")

# --------------------- Initialize program --------------------- #

# Print in console: global script is beginning to run
print("global.R")


#--------------------------------------- User Interface ---------------------------------------#

# Tell user ui script is beginning to run
print("ui.R")

ui <- fluidPage( 
  theme = shinytheme("cerulean"), 

  navbarPage("Analysis",

             #--- Home Tab (Global View)
             tabPanel("Global View",
                      sidebarPanel(),
                      visNetworkOutput("network")
                      ), #tabPanel - Global View  

             tabPanel("",
                      ) #tabPanel - 

  ) #navbarPage
) #fluidPage
#--------------------------------------- Server ---------------------------------------#

# Tell user server script is beginning to run
print("server.R")

server <- function(input, output, session) {

  # Gathering important variables in one only data set
    twd <<- NULL

  observe({                                                         
    twd <- structure(c(0.0156521739130435, 0, 0, 0, 0, 0, 0.00633484162895928, 
0, 0, 0.0137931034482759, 0.118245390972664, 0, 0.0121739130434783, 
0.141455437448896, 0.00131061598951507, 0, 0.00580720092915215, 
0, 0, 0, 0.0200445434298441, 0, 0, 0, 0, 0, 0, 0.036046511627907, 
0, 0, 0.00180995475113122, 0, 0, 0.0334975369458128, 0.0216147488874762, 
0.0449197860962567, 0.00956521739130435, 0.0834014717906787, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.0121739130434783, 0.080130825838103, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.0217391304347826, 0, 0, 0, 0, 
0, 0, 0, 0.0623608017817372, 0, 0.00762873490146217, 0, 0, 0, 
0, 0.00930232558139535, 0.0511033681765389, 0, 0, 0, 0.044543429844098, 
0, 0, 0, 0.0582608695652174, 0, 0, 0, 0, 0, 0, 0, 0.0122494432071269, 
0, 0, 0, 0, 0.00245298446443173, 0, 0, 0, 0, 0.0624434389140271, 
0, 0, 0, 0.00190718372536554, 0, 0, 0.00654129190515127, 0, 0, 
0, 0, 0, 0.0594541910331384, 0, 0, 0, 0.00320855614973262, 0.0121739130434783, 
0.00163532297628782, 0, 0.0558139534883721, 0, 0, 0, 0.00584795321637427, 
0, 0, 0, 0, 0, 0, 0, 0.0709302325581395, 0, 0, 0, 0, 0, 0, 0.00190718372536554, 
0, 0, 0, 0, 0, 0, 0, 0.0262443438914027, 0, 0.0089086859688196, 
0, 0.0133502860775588, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.0368722186904005, 
0, 0, 0, 0.072083879423329, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.0217391304347826, 
0, 0, 0, 0, 0, 0, 0.0126705653021442, 0, 0, 0.0108073744437381, 
0, 0.0165217391304348, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.0222504767959313, 
0, 0, 0, 0, 0, 0, 0, 0.0235294117647059, 0, 0.0178173719376392, 
0, 0.00699300699300699, 0, 0, 0.0098119378577269, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0.0427807486631016, 0.00521739130434783, 0.0335241210139002, 
0, 0, 0, 0, 0, 0, 0, 0, 0.00254291163382072, 0, 0, 0, 0, 0, 0, 
0.0488974113135187, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.0488974113135187, 
0, 0, 0, 0, 0, 0, 0.0182608695652174, 0.0122649223221586, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0.0128342245989305, 0, 0.00408830744071954, 
0, 0, 0, 0.0134228187919463, 0.0262443438914027, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.0305149396058487, 0), .Dim = c(12L, 
25L), .Dimnames = list(NULL, c("demand_response", "utility_model", 
"real_time", "model_discloses", "utility_model_discloses", "power_supply", 
"control_system", "control_module", "intelligent_terminal", "management_system", 
"energy_consumption", "disaggregated_model", "control_method", 
"power_grid", "electric_energy", "intelligent_power", "electricity_consumption", 
"power_consumption", "control_unit", "communication_module", 
"random_access", "uplink_frequency", "intelligent_control", "mobile_terminal", 
"power_utilization")))
    topic_names <<- c("1", "2", "3","4","5","6","7","8","9","10","11","12")                         
  })

  # Network Map

  observe({
  # Preparing data with igraph library

    twd_num_col <- ncol(twd)
    cor_threshold <- .8
    twd_cor <- cor(twd[,1:twd_num_col], method = "pearson")
    twd_cor[ twd_cor < cor_threshold ] <- 0
    diag(twd_cor) <- 0
    graph <- graph.adjacency(twd_cor, weighted=TRUE, mode="lower")
    E(graph)$edge.width  <- E(graph)$weight
    V(graph)$group       <- apply(twd, 2, which.max)  #Max topic prob for colors
    V(graph)$betweenness <- betweenness(graph, v = V(graph), directed = F)
    V(graph)$degree      <- degree(graph, v = V(graph))

    # Fit data for visNetwork 
    nm_data <- toVisNetworkData(graph)
    nodes <- nm_data[[1]]
    edges <- nm_data[[2]]
    nodes <- nodes[nodes$degree != 0,]    # Bye topics that don't have a connection (degree = 0)
    nodes$group <- swap(nodes$group , 1:length(topic_names), topic_names) # Swap long real names
    nodes$label <- rep("")
  })

  output$network <- renderVisNetwork({
    # Graph
    set.seed(17);visNetwork(nodes, edges, main = "Relation between topics")  %>% 
      visOptions(highlightNearest = TRUE, selectedBy = "group") %>%
      visInteraction(dragNodes = FALSE) %>%
      visLegend(useGroups = TRUE, main = "Topic", ncol = 2)  %>%
      visNodes(shape = "dot",label = NULL) %>%
      visEdges(smooth = FALSE) %>%
      visIgraphLayout(randomSeed = 17)
  })

}
shinyApp(ui, server)

当我超级分配适合 visNetwork 的数据时,它给了我这个:Warning: Error in <observer>: cannot change value of locked binding for 'edges'但它的打印正常,所以我很困惑。

4

0 回答 0