C. Crt
C. Crt

Reputation: 355

How to simply pass a variable from server to UI?

I have a shiny app with the server and UI in two different files. The aim is to display a network plot, so I have a list of nodes and a list of edges. I would like to add an input that allows the user to select certain nodes.

My UI:

ui <- fluidPage(

      visNetworkOutput("network_proxy", height = "400px"), #Plot output
      selectInput(inputId = "selnodes", label = "Nodes selection", choices = , multiple = TRUE) #The input I want
)

My server:

    nodes <- c(1, 5, 12, 53)
    edges <- data.frame(
       from = c(1,1,5,12,12,53), 
       to = c(5,12,12,12,53, 53),
       stringsAsFactors = FALSE)

    observeEvent(input$generatePlotButton,{
        net <<- generatePlot(nodes,edges)
        output$plotPlot <- renderVisNetwork({net})
    })

In my selectInput in the UI, I want the proposed values to be the list of nodes that I am using in my plot. So the variable nodes that I use on my server.

However, I cannot simply write choices = nodes in the parameters, because the UI doesn't know what nodes is (remember, it's a variable on the server side).

So I would like to pass my nodes variable from server to UI so I can use it to have the list of nodes as choices in my selectInput.

What can I do? Thanks in advance.

Upvotes: 2

Views: 1443

Answers (2)

KSONG01
KSONG01

Reputation: 59

Another option is to use renderUI to push the selectInput from server to ui, I like to use this because it is easier to extract choices from the server dataset and you do not need to go back to ui section to change codes. In the example below, I didn't use the updateSelectInput, as I use it more often when it comes to creating a Select All button. Here are the codes I have:

library(shiny)
library(visNetwork)

ui <- fluidPage(
    visNetworkOutput("plotPlot", height = "400px"), #Plot output
    uiOutput("selnodes"),
    actionButton("Update", "Update")
)

# Define server logic required to draw a histogram
server <- function(input, output, session) {
    nodes <- data.frame(id = c(1, 5, 12, 53), label = c(1, 5, 12, 53))
    edges <- data.frame(
        from = c(1,1,5,12,12,53), 
        to = c(5,12,12,12,53, 53),
        stringsAsFactors = FALSE)

    output$selnodes <- renderUI({
        selectInput(inputId = "selnodes", label = "Nodes selection", choices = nodes, multiple = TRUE)
    })

    observeEvent(input$Update, {
        ## Update the nodes list 
        nodes <- data.frame(id = input$selnodes, label = input$selnodes)
        net <- visNetwork(nodes,edges)
        output$plotPlot <- renderVisNetwork({net})
    })
}

# Run the application 
shinyApp(ui = ui, server = server)

Upvotes: 2

arg0naut91
arg0naut91

Reputation: 14764

You could try:

ui <- fluidPage(

  visNetworkOutput("network_proxy", height = "400px"), #Plot output
  selectInput(inputId = "selnodes", label = "Nodes selection", choices = "", multiple = TRUE)
)

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

  nodes <- c(1, 5, 12, 53)
  edges <- data.frame(
    from = c(1,1,5,12,12,53), 
    to = c(5,12,12,12,53, 53),
    stringsAsFactors = FALSE)

  observeEvent(input$generatePlotButton,{
    net <<- generatePlot(nodes,edges)
    output$plotPlot <- renderVisNetwork({net})
  })

  observe({
    updateSelectInput(session, "selnodes", choices = nodes)
  })

}

Upvotes: 2

Related Questions