'paste" gives wrong answer w/in shiny app

I have a function that (among other things) pastes together the elements of an array, something like this:
f <- function(){
xy <- c("x", "y")
pxy <- paste(xy, collapse = " and ")
ans <- paste(pxy, "exist")
answer <<- ans
return(ans)
}

When I run it directly in R, answer looks just like you'd expect it too: x and y exist
When I run it via a shiny button, answer looks like: c("x", "y") and y exist

Any ideas what could be going on here?

[NB: Sorry for not including code that can reproduce the error. My app is many thousand lines long, and I cannot reproduce the pattern in a simple example.]

here is a working reprex in a shiny context.
image
I wonder if you might have oversimplied your f(), I would have expected you to be passing parameters, to get different effects ? The issue might be what you are trying to pass...

library(shiny)
f <- function(){
    xy <- c("x", "y")
    pxy <- paste(xy, collapse = " and ")
    ans <- paste(pxy, "exist")
    return(ans)
}
ui <- fluidPage(
    actionButton("runme","run"),
    textOutput("txtout")

)
server <- function(input, output) {

   output$txtout<-renderText({
       req(input$runme)
       f()
   })
}

# Run the application 
shinyApp(ui = ui, server = server)
1 Like

Excellent, Nir, thank-you.

The arg list contains several very complicated objects, and there was a subtle difference in the way I was constructing them from the command line compared to the way the app was constructing them.

If I do pxy <- paste(unlist(xy)) in the real code, it works.

1 Like

Hurray, all is well that ends well.

If anything I hoped to show that even a shiny app can sometimes be used as a minimal reprex().

Glad this worked out for you Dan.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.