Returning two values from a reactive function


#1

This is what I have coded so far: I am trying for the reactive function to return the two values returned by the extractParams() function in functionName(). It does not work. I do not get the textOutput displayed. What is the right way of doing this?

ui <- fluidPage(

textOutput("list(value1, value2")

)
functonName <- function(sentvairble){

  c(outL, numericL) %<-% extractParams(sentVariable)  # the function that responds to this function call returns a list "list(outL, numericsL)
list(outL = outL, numericsL = numericsL)
}

extractParams <- function(sentVariable){

 outL <- 1
  numericsL <- 2

  return(list(outL, numericsL))
}

server <- function(input, output){
output$checkedInput <- reactive({
    output$list(value1, value2) %<-% renderText({
      list(result1, result2) %<-% functionName(inFileName)
      return(list(result1$outL, result2$numericsL))
    })


}

#2

You can only return one object in each R function, and the textOutput() just creates some html with that ID attached, that needs a renderText() to assign and change the HTML content and display your text.

Something like the below:

ui <- fluidPage(
  textOutput("your_output")
)

functionName <- function(sent_variable){
  # the function that responds to this 
  # function call returns a list "list(outL, numericsL)
  extractParams(sent_variable)  
}

server <- function(input, output){

  output$your_output <- renderText({
       
       #variable could be an input
       two_value_list <- functionName(variable) 
       
       # turn the list into a single text string
       paste("1:",two_value_list[[1]], "2:", two_value_list[[2]]) 
    })
}

#3

Thanks, the follwoing works weel: but it returns TRUE, FALSE instead of the values 1 and 2

 output$checkedInput <- reactive({
     output$value <- renderText({
     result <- glycoPipe(inFileName)
     returnedParam1 <- result$outL
     returnedParam2 <- result$numericsL
     paste("1:", result[[1]], "2:", result[[2]])
     return(paste("1:", result[[1]], "2:", result[[2]]))
     #return(returnedParam2)
     })

#4

Thank you. If I use :
’’‘
return(paste(“1:”, returnedParam1, “2:”, returnedParam2))
’’'
I get the right results. How can I extract each value from the string?


#5

I wouldn’t put the renderText() inside the reactive() function, thats confusing and perhaps your issue.

 output$checkedInput <- renderText({
     result <- glycoPipe(inFileName)

     returnedParam1 <- result$outL
     returnedParam2 <- result$numericsL

     return(paste("1:", result[[1]], "2:", result[[2]]))

     })

I’m not sure what you want to extract each value from the string, but if you have a variable length of list you could perhaps do something like paste(1:length(result),":" ,unlist(result), collapse = ", ")


#6

Thanks very much.

I am trying to use getwd() in shiny, but it returns NA. what can I use instead to get the path in the local directory of the server? Not the PC working directory.

Please delete this post. This is a different question.


#7

Probably not a good idea to use getwd() at all, try relative paths to where your shiny app is deployed

e.g. if

|--ui.R
|--server.R
|--/data/
        |-mydata.csv
|

…then you can read data from within the Shiny app via “data/mydata.csv”


#8

OK, thanks. Is there any way I can get the folder where an uploaded and saved file (copy()) is in the server? Not in the local PC