splaisan
splaisan

Reputation: 923

shiny display multiline text output from system call

How can I get the newlines from a bash system call correctly interpreted in a shiny render object?

In the UI I am using

textOutput("cmd_out")

and in the server, triggered by a button and command assembled in a preliminary object 'fullcmd'

cmdres <- eventReactive(input$runbtn, {
    res <- system(fullcmd(), intern=TRUE)
    })

output$cmd_out <- renderText({
    cmdres()
    })

but the text is not wrapped at the newline as in a terminal and I get it in none line

# ------------------------------------------------------------------------------ # Usage: script.sh -a ActionDemo -p var1=1 -p var2=azerty -p var3="'two words'" -p var4="'accepts (){}/\:;,-'" # ------------------------------------------------------------------------------ # opt_actparams is: var1=1 var2=azerty var3="two words" var4="accepts (){}/\:;,-" # var1 has value: 1 # var2 has value: azerty # var3 has value: "two words" # var4 has value: "accepts (){}/\:;,-" # ------------------------------------------------------------------------------ # # script version 1.0; 2020-09-11

instead of the expected:

# ------------------------------------------------------------------------------
# Usage: script.sh -a ActionDemo -p var1=1 -p var2=azerty -p var3="'two words'" -p var4="'accepts (){}/\:;,-'"
# ------------------------------------------------------------------------------
# opt_actparams is: var1=1 var2=azerty var3="two words" var4="accepts (){}/\:;,-"
# var1 has value: 1
# var2 has value: azerty
# var3 has value: "two words"
# var4 has value: "accepts (){}/\:;,-"
# ------------------------------------------------------------------------------
#
# script version 1.0; 2020-09-11

I also tried verbatimTextOutput, htmlOutput, and renderUI, no better, always one line

Should I replace the current newlines by new ones (which?)?

Thanks!

Upvotes: 1

Views: 67

Answers (1)

Abdessabour Mtk
Abdessabour Mtk

Reputation: 3888

all you need to do is use paste0(, collapse="\n"), because the return value of system is a vector where each line is an item. and textOutput collapses vector items with space, this will work with verbatimTextOutput.

cmdres <- eventReactive(input$runbtn, {
    res <- paste0(system(fullcmd(), intern=TRUE), collapse="\n")
    })

Upvotes: 1

Related Questions