renderText: print each element in passed vector on a new line??

4,935 views
Skip to first unread message

Melissa Gray

unread,
Jul 15, 2014, 12:40:17 PM7/15/14
to shiny-...@googlegroups.com
I'm fairly new at both R and Shiny with a little more experience with R. As part of a Shiny app I'm creating, I'd like to output pairs of numbers in coordinate form (ex: "(1, 2)") with each new coordinate pair on a new line. I can successfully print the pairs of numbers but they all print on the same line. Is it possible to print each on a new line using renderText? 

In server.R I have the following code:

#user input determines n for findBigy1
output$summary1 <- renderText({
    dataset <- accdat
    findBigy1(input$obs1)
    })

I sourced the R script that defines this function within my Shiny app: 

findBigy1 <- function(n) {
  st <- c()
  ord_ind <- order(accdat[,2], decreasing=T)
  for (i in 1:n) {   
    st[i] <- paste(c("("), accdat[ord_ind[i],1 ], c(" , "), accdat[ord_ind[i], 2], c(")\n"), sep="", collapse="")
    #st[i] <- sprintf("(%f, %f)\n", accdat[ord_ind[i], 1], accdat[ord_ind[i], 3])
  }
  st
}

For a dataset with two columns this function will order the dataset based on the second column and will create a vector that saves the coordinates for the n biggest 2nd column values. (tried it two different ways)
Testing this function apart from the Shiny app produces output of the form: 
 [1] "(30 , 0.29)\n" "(120 , 0.29)\n" "(230 , 0.29)\n"
The Shiny app produces output like this: 
(30 , 0.29) (120 , 0.29) (230 , 0.29)

I read something that said that renderText automatically uses cat() to transform whatever is passed to a string. So it makes sense that Shiny wouldn't show the \n, but how do I get it to put each coordinate on a new line? Should I be using something other than renderText? 

When I was trying to solve this I changed my findBigy1 function to return cat(st) rather than just st. This produces the correct R output:
(30 , 0.29)
 (120 , 0.29)
 (230 , 0.29)
but then the Shiny app doesn't display any coordinates in the actual app, they are all printed in the console, which is not what I want either.

The solution is probably simple enough, but I keep running around in circles changing the function and Shiny code. Any help would be greatly appreciated!  Thanks!

Melissa




Melissa Gray

unread,
Jul 16, 2014, 1:32:53 PM7/16/14
to shiny-...@googlegroups.com
I solved my problem by following the example in this stackoverflow thread http://stackoverflow.com/questions/23233497/outputting-multiple-lines-of-text-with-rendertext-in-r-shiny

I replaced renderText and textOutput with renderUI and htmlOutput.
Reply all
Reply to author
Forward
0 new messages