0

I am having problems in getting shiny output to run when I make selections in UI. My server code is below.

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

When I make changes in the UI the print statement in observe is getting executed and I can see the values of values$clus_num changing correctly. So I know for sure the client is able to send the data to the server. But None of the print statements in output$ssid are getting executed. Any changes in the observed value should make the output$ssid to re-execute. Why is that not happening?

1 Answers1

0

So it is almost impossible to diagnose this without a reproducible example (see this post for instructions on how to do this). Nevertheless, your problem seems to be threefold:

  1. You are defining a function but not calling it.
  2. You have to call it from within the observe block.
  3. As least in your example, the function get_items_in_cluster(...) is not defined.

Since you did not provide an example, as a diagnostic I created this trivial example, which increments a counter each time the command button is pressed. It does however illustrate the points above, and it runs.

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

It also produced the following output in the R command line:

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()]
Community
  • 1
  • 1
jlhoward
  • 58,004
  • 7
  • 97
  • 140