【问题标题】:Shiny output not running闪亮的输出没有运行
【发布时间】:2013-12-24 17:13:56
【问题描述】:

当我在 UI 中进行选择时,我在运行闪亮的输出时遇到问题。我的服务器代码如下。

shinyServer(function(input, output, session) {      

    values <- reactiveValues();  
    values$clus_num <- NULL; 

    observe({    
      values$clus_num <- input$ss_clus_num_btn; 
      print(values$clus_num)
    })    

    output$ssid <- function(){
      cluster_num <- values$clus_num;
      if(identical(values$clus_num, 0)){
        print("no Cluster selected\n")
      }else{
        print("cluster selected\n")
      }
      print(cluster_num)
      ssids <- get_items_in_cluster(cluster_result, cluster_num)
      return(ssids)
    }
})

当我在 UI 中进行更改时,observe 中的打印语句正在执行,我可以看到 values$clus_num 的值正确更改。所以我确定客户端能够将数据发送到服务器。但是 output$ssid 中​​的任何打印语句都没有被执行。观察值的任何变化都应该使 output$ssid 重新执行。为什么这没有发生?

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    因此,如果没有可重现的示例,几乎不可能诊断此问题(有关如何执行此操作的说明,请参阅this post)。不过,您的问题似乎有三个方面:

    1. 您正在定义一个函数,但没有调用它。
    2. 您必须在观察块中调用它。
    3. 至少在您的示例中,函数 get_items_in_cluster(...) 没有定义。

    由于您没有提供示例,作为诊断,我创建了这个简单的示例,每次按下命令按钮时都会增加一个计数器。但是,它确实说明了上述几点,并且可以运行。

    ui.R

    shinyUI(
      pageWithSidebar( 
        headerPanel("Shiny App"),
        sidebarPanel(
          actionButton("ss_clus_num_btn", "SSID:"),
          h3(textOutput("cluster"))
          ),
        mainPanel()
      )
    )
    

    server.R

    shinyServer(function(input, output, session) {      
    
      values <- reactiveValues();  
      values$clus_num <- NULL; 
    
      observe({    
        values$clus_num <- input$ss_clus_num_btn; 
        print(paste(values$clus_num,"[from outside fn()]"),quote=FALSE)
        output$cluster <- renderText(fn())
      })    
    
      fn <- function(){
        cluster_num <- values$clus_num;
        if(identical(values$clus_num, 0)){
          print("no Cluster selected\n",quote=FALSE)
        }else{
          print("cluster selected\n",quote=FALSE)
        }
        print(paste(cluster_num,"[from inside fn()]"),quote=FALSE)
    #    get_items_in_cluster(...) NOT DEFINED
    #    ssids <- get_items_in_cluster(cluster_result, cluster_num)
        return(cluster_num)
      } 
    })
    

    它还在 R 命令行中产生了以下输出:

    Listening on port 4053
    [1] 0 [from outside fn()]
    [1] no Cluster selected\n
    [1] 0 [from inside fn()]
    [1] 1 [from outside fn()]
    [1] cluster selected\n
    [1] 1 [from inside fn()]
    [1] cluster selected\n
    [1] 1 [from inside fn()]
    [1] 2 [from outside fn()]
    [1] cluster selected\n
    [1] 2 [from inside fn()]
    

    【讨论】:

    • 我正在使用自定义 html。我没有 ui.R 文件。我试过你的方法,但没有奏效。
    猜你喜欢
    • 2017-01-24
    • 1970-01-01
    • 1970-01-01
    • 2019-06-19
    • 1970-01-01
    • 2015-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多