Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何简单地将变量从服务器传递到UI?_R_Server_Shiny - Fatal编程技术网

R 如何简单地将变量从服务器传递到UI?

R 如何简单地将变量从服务器传递到UI?,r,server,shiny,R,Server,Shiny,我有一个闪亮的应用程序,服务器和用户界面在两个不同的文件中。目的是显示网络图,因此我有一个节点列表和一个边列表。 我想添加一个允许用户选择某些节点的输入 我的用户界面: ui <- fluidPage( visNetworkOutput("network_proxy", height = "400px"), #Plot output selectInput(inputId = "selnodes", label = "Nodes selection", choic

我有一个闪亮的应用程序,服务器和用户界面在两个不同的文件中。目的是显示网络图,因此我有一个节点列表和一个边列表。 我想添加一个允许用户选择某些节点的输入

我的用户界面:

ui <- fluidPage(

      visNetworkOutput("network_proxy", height = "400px"), #Plot output
      selectInput(inputId = "selnodes", label = "Nodes selection", choices = , multiple = TRUE) #The input I want
)
ui您可以尝试:

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)
  })

}

ui另一个选项是使用renderUI将selectInput从服务器推送到ui,我喜欢使用它,因为它更容易从服务器数据集中提取选项,并且您不需要返回ui部分来更改代码。在下面的示例中,我没有使用updateSelectInput,因为在创建全选按钮时,我更经常使用它。
以下是我的代码:

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)
库(闪亮)
图书馆(visNetwork)

我不知道
updateSelectInput
是什么东西!谢谢请不要将任何输出放入observe,因为您可能有内存leaks@PorkChop这是一个单独的问题,但OP可以注意到,我没有想到这一点。谢谢你,好主意!我将其标记为已解决,因为我认为它比arg0naut91提供的(非常好的)答案更具普遍性,并且它可能会帮助其他人。
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)