我正在使用闪亮的应用程序,需要使用visNetwork创建网络图表。 运行代码时出现以下错误-
错误:$运算符对于原子向量无效 堆栈跟踪(从最里面开始): 77:origRenderFunc 76:输出$ kolNetwork 1:runApp
下面是我的ui代码-
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(),
dashboardBody(
#css to format the views
tags$style(
HTML("
#filterRow>div{
width: 140px;
display : inline-block;
margin-right: 15px;
}
.main-header{
display:none;
}
.content{
position: fixed;
height: 100%;
width: 100%;
top: 0;
left: 0;
background: #fff;
z-index: 1000;
}
.selectize-control.single .selectize-input, .selectize-control.single .selectize-input input{
height: 22px !important;
}
")
),
#code for main view
fluidRow(column(12,tags$div(style="height:10px;"))),
fluidRow(column(12,
tags$div(id = "filterRow",
selectInput("countryFilter", "Country", choices = c("Select", sort(unique(node_file[node_file$from_flag == "y",]$country))), selected = "Select", multiple = F, selectize = T),
selectInput("specialtyFilter", "Specialty", choices = c("Select", sort(unique(node_file[node_file$from_flag == "y",]$specialty))), selected = "Select", multiple = F, selectize = T),
selectInput("sponsorFilter", "Sponsor", choices = c("Select", sort(unique(node_file[node_file$from_flag == "y",]$sponsor))), selected = "Select", multiple = F, selectize = T),
selectInput("kolFilter", "KOL", choices = "Select", selected = "Select", multiple = F, selectize = T),
actionButton("createNetwork", "Go", style = "margin-top: -2%; background-color :#3C8DBC; color: #fff; font-weight: 600; height: 40px; width: 110px;font-size: 20px;")
))
),
fluidRow(
column(9, tags$div(style = 'height: 700px;', visNetworkOutput("kolNetwork"))),
column(3, tags$div(style = 'height: 700px;', uiOutput("networkLegend")))
),
fluidRow(
column(
width=12,
box(
width = 12,title = "Influencer Details",solidHeader = T,status = "primary",collapsible = T, collapsed = F,
DTOutput("kolNetworkTable"),
tags$div(uiOutput("download"))
)
)
)
)
)
下面是server.R文件
server <- function(input, output, session){
#update rest columns on selection in country column
observeEvent(input$countryFilter, {
if(input$countryFilter!="Select"){
node_file <- filter(node_file, country %in% input$countryFilter & from_flag == "y")
updateSelectInput(session, "specialtyFilter", choices = c("Select", sort(unique(node_file[node_file$from_flag == "y",]$specialty))), selected = "Select")
updateSelectInput(session, "sponsorFilter", choices = c("Select", sort(unique(node_file[node_file$from_flag == "y",]$sponsor))), selected = "Select")
updateSelectInput(session, "kolFilter", choices = c("Select", sort(unique(node_file[node_file$from_flag == "y",]$id))), selected = "Select")
}
else if(input$countryFilter=="Select"){
updateSelectInput(session, "specialtyFilter", choices = c("Select", sort(unique(node_file[node_file$from_flag == "y",]$specialty))), selected = "Select")
updateSelectInput(session, "sponsorFilter", choices = c("Select", sort(unique(node_file[node_file$from_flag == "y",]$sponsor))), selected = "Select")
updateSelectInput(session, "kolFilter", choices = c("Select", sort(unique(node_file[node_file$from_flag == "y",]$id))), selected = "Select")
}
}, ignoreInit = T)
observeEvent(input$specialtyFilter, {
if(input$countryFilter!="Select"){
node_file <- filter(node_file, country %in% input$countryFilter & from_flag == "y")
}
if(input$specialtyFilter!="Select"){
node_file <- filter(node_file, specialty %in% input$specialtyFilter & from_flag == "y")
updateSelectInput(session, "sponsorFilter", choices = c("Select", sort(unique(node_file[node_file$from_flag == "y",]$sponsor))), selected = "Select")
updateSelectInput(session, "kolFilter", choices = c("Select", sort(unique(node_file[node_file$from_flag == "y",]$id))), selected = "Select")
}
else if(input$specialtyFilter=="Select"){
updateSelectInput(session, "sponsorFilter", choices = c("Select", sort(unique(node_file[node_file$from_flag == "y",]$sponsor))), selected = "Select")
updateSelectInput(session, "kolFilter", choices = c("Select", sort(unique(node_file[node_file$from_flag == "y",]$id))), selected = "Select")
}
}, ignoreInit = T)
observeEvent(input$sponsorFilter, {
if(input$countryFilter!="Select"){
node_file <- filter(node_file, country %in% input$countryFilter & from_flag == "y")
}
if(input$specialtyFilter!="Select"){
node_file <- filter(node_file, specialty %in% input$specialtyFilter & from_flag == "y")
}
if(input$sponsorFilter!="Select"){
node_file <- filter(node_file, sponsor %in% input$sponsorFilter & from_flag == "y")
updateSelectInput(session, "kolFilter", choices = c("Select", sort(unique(node_file[node_file$from_flag == "y",]$kol))), selected = "Select")
}
else if(input$sponsorFilter=="Select"){
updateSelectInput(session, "kolFilter", choices = c("Select", sort(unique(node_file[node_file$from_flag == "y",]$kol))), selected = "Select")
}
}, ignoreInit = T)
#creating filtered dataset
filteredData <- eventReactive(input$createNetwork, {
temp <- copy(node_file[node_file$from_flag=="y",])
if(input$kolFilter!="Select"){
temp <- filter(temp, id %in% input$kolFilter)
}
if(input$sponsorFilter!="Select"){
temp <- filter(temp, sponsor %in% input$sponsorFilter)
}
if(input$specialtyFilter!="Select"){
temp <- filter(temp, specialty %in% input$specialtyFilter)
}
if(input$countryFilter!="Select"){
temp <- filter(temp, country %in% input$countryFilter)
}
})
# myNetworkChart <- function(edges, nodes){
#
# print("inside network chart function")
#
# output$kolNetwork <- renderVisNetwork({
#
# print("inside render visnetwork")
#
# edges <- data.frame(edges, width = edges$weight)
#
# nodes <- data.frame(nodes,
# size = nodes$pageranks*1000+3,
# title = nodes$id, borderWidth = 2,
# color.highlight.background = "yellow",
# shadow = list(enabled = TRUE, size = 10))
#
# #browser()
#
# visNetwork::visNetwork(nodes, edges, width = "100%", height = "100%")
# print("after render visnetwork")
# })
# }
observeEvent(input$createNetwork, {
#View(filteredData())
edges <- subset(edge_file, from %in% filteredData()$id)
nodes <- subset(node_file, id %in% edges$from | id %in% edges$to)
output$kolNetwork <- renderVisNetwork({
print("inside render visnetwork")
edges <- data.frame(edges, width = edges$weight)
nodes <- data.frame(nodes,
size = nodes$pageranks*1000+3,
title = nodes$id, borderWidth = 2,
color.highlight.background = "yellow",
shadow = list(enabled = TRUE, size = 10))
#browser()
visNetwork(nodes, edges, width = "100%", height = "100%")
print("after render visnetwork")
})
#myNetworkChart(edges, nodes)
}, ignoreInit = T)
#function to create network chart
}
Global.R仅引入库并读取数据文件