I am trying to figure out how can I like together visNetwork and DT in Shiny R. I want to use a selected node in the visNetwork as an input for selected row in the DT and vise versa. For example, if a user click on a node in the visNetwork, the corresponding row (by Id of the dataset "data") is selected. The problem is that I struggle to find out how to create an input variable for the visNetwork as well as input and output variables for the DT (and make them reactive).
I have the following code for the visNetwork:
renderVisNetwork({
search_input <- input$search_input
result <- data %>%
filter(grepl(search_input, Name, ignore.case = TRUE) |
grepl(search_input, Abstract, ignore.case = TRUE)) %>%
select(Id)
nodes <- data.frame(
id = data$Id,
label = gsub("(\\S+\\s+\\S+\\s+\\S+\\s+\\S+)\\s+", "\\1\n", data$Name),
color = ifelse(data$Id %in% result$Id & input$search_input != "", "#f7bf05", NA)
)
edges_citation <- data.frame(
from = data$Citation1,
to = data$Id
)
all_edges <- rbind(edges_citation)
network <- visNetwork(nodes, all_edges) %>%
visEvents(
click = "function(nodes) {
var selectedNodeId = nodes.nodes[0]; // Get the selected node ID
Shiny.setInputValue('selectedNodeId', selectedNodeId); // Set the input value for Shiny
}"
)
hey <- reactive({
selected_data <- input$selectedNodeId
return(selected_data)
})
output$selectedNodeId = renderText({
hey()
})
network
})
And I have the following code for the DT:
output$data_table <- DT::renderDataTable({
search_input <- input$search_input
result <- data %>%
filter(grepl(search_input, Name, ignore.case = TRUE) |
grepl(search_input, Abstract, ignore.case = TRUE)) %>%
select(Id)
data_render_id <- data %>%
filter(Id %in% result$Id) %>%
select(Id, Name, Authors, Year)
data_render <- data_render_id %>%
select(-Id)
data_render_output <- DT::datatable(data_render)
idfinaly <- reactive({
selected_data <- input$data_table_rows_selected
selected_id <- data_render_id$Id[selected_data]
selected_id_reactive(selected_id)
return(selected_id)
})
output$selectedRowId <- renderText({
idfinaly()
})
data_render_output})
DTOutput("data_table")
P.S. This is a Flexdashboard application, therefore I did no apply ui/server logic here.