0

I found this regarding getting multiple outputs from a reactive script, but this still assumes that both will be used in the same object which is ultimately output. However, even if I get this working, I'm concerned that using these outputs from 2 different renderSomething({}) functions will mean my reactive script has to run twice each time.

My original code is quite lengthy, so I'll just include where I'm having an issue:

#main function
    server<-function(input, output){
    main<-reactive({

    #a bunch of stuff producing what it needed for output

    if(nglabels>0){
        #return number of labelings:
        outputText<-paste("Graph has",nglabels,"graceful labelings",sep=" ",collapse=NULL)
        #return FedgeLabel for graph:
        outputGraph<-graph_from_adjacency_matrix(FedgeLabel,mode=c("undirected"),weighted=TRUE,add.colnames=FvertexLabel,add.rownames=FvertexLabel)
      } else {
        outputText<-"No graceful labels"
        #return imported graph as-is
        outputGraph<-graph_from_adjacency_matrix(mat,mode=c("undirected"))
      }
      #reactive in R only allows 1 output, so we need to make a list of these:
      outputList<-list(outputText=outputText,outputGraph=outputGraph)
      return(outputList)
      })
#outputting what was calculated
    output$txt<-renderText({
        getOutput<-main()
        getOutput$outputText})
    output$Graph<-renderPlot({
        getOutput<-main()
        plot(getOutput$outputGraph)})
    } #this is the end brace from the server function

Of course, as of right now, neither is returning correctly, but given computation times could get large depending on input, I would much rather find an elegant way to get my outputs out of just the one reactive function.

10
  • Remove the brackets when you're assigning the reactive object to main on the second line. Commented Nov 30, 2018 at 3:21
  • Thanks, didn't catch that when posting. Still getting a "subscript out of bounds" on my outputs though, which I'm assuming means the values didn't make it out of the reactive part correctly. Commented Nov 30, 2018 at 3:47
  • If you add observe(print(str(main()))) outside of the reactive() calls and run the app, what does it say in the console? Commented Nov 30, 2018 at 3:59
  • It gives me "Warning: Error in read.table: 'file' must be a character string or connection" a bunch of times. In that same reactive() it imports a file, and the text output and graph are blank until that file is imported. Commented Nov 30, 2018 at 5:34
  • 1
    Reactive functions execute only when a reactive value change... Once they are executed they are like a value that you can use many times.. You can use the same reactive funciton (main) is many render functions and it will execute one time for all the outputs... Commented Nov 30, 2018 at 13:17

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.