3

I have a problem with renderUI and I couldn't find a solution anywhere. Probably I'm asking the wrong question to google and more than a shiny problem is a basic R problem.

I have a function in R which depending on the input will return a table or a text. So I created both the options in my server.R in this way:

output$table <- renderTable {(
   x <- function (y)
   print(x)
)}
output$text <- renderText {(
   x <- function (y)
   print(x)
)}

If I put both the outputs in renderUI one will always give me an error. In case of textOutput if the output is a table:

 Error: argument 1 (type 'list') cannot be handled by 'cat'

and

 Error:no applicable method for 'xtable' applied to an object of class "character"

if it is viceversa.

My question is there a way to catch this error and use an if statement within renderUI to display only one of the two? I'm here to give you more details if you need.

[EDIT]

server.R

 library(shiny)
 library(drsmooth)

 shinyServer(function(input, output,session) {

-- upload dataframe and input management goes here --

 output$nlbcd <- renderTable({
    nlbcd<-nlbcd(dosecolumn="Dose", targetcolumn=response(),cutoffdose=cutoff(),data=data1())
    print(nlbcd)
 })

 output$nlbcdText <- renderText({
    nlbcd<-nlbcd(dosecolumn="Dose", targetcolumn=response(),cutoffdose=cutoff(),data=data1())
   print(nlbcd)
 }) 

 output$tb <- renderUI({
 tableOutput("nlbcd"),
 textOutput("nlbcdText")   
})

})
SecretAgentMan
  • 2,856
  • 7
  • 21
  • 41
Daniele Avancini
  • 899
  • 1
  • 10
  • 14
  • 1
    I do think this is a good question, but can you be more specific with your situation? Why would you be outputting one of a list (not table -- you should address this as well) or a text string? Posting the simplest reproducible example of your `ui.R` and `server.R` codes would be very helpful as well. – mlegge Jan 22 '15 at 15:58
  • Ok, I'm using a package called `drsmooth` . in this package there is a function 'nlbcd' which according to one of the arguments passed to it returns a list (sorry for the wrong terminology) displayable by renderTable or a string which is displayable by renderText. – Daniele Avancini Jan 22 '15 at 16:14

2 Answers2

2

You have some issues here, the function will return different classes, including errors and warnings with interpretations. Here is a standalone example of what can happen with this function, you are encouraged to include the TryCatch in your code:

ui.R

shinyUI(
  pageWithSidebar(   
    headerPanel("drsmooth"),   sidebarPanel(
      numericInput("num", label = h3("Choose 'cutoffdose'"), value = 0)
    ),
    mainPanel(
      verbatimTextOutput('current')
    ) 
  )
)

server.R

library(drsmooth)

shinyServer(function(input, output, session) {
 output$current <- renderPrint({
   dose <- input$num

   tryCatch(isolate(nlbcd("dose", "MF_Log", cutoffdose=dose, data=DRdata)), 
            error=function(e) {
              cat(isolate(conditionMessage(e)))
            }
   )

 })
})

Sample outputs: With <code>cuttoffdose</code>= 0

With <code>cutoffdose</code>= 1

With <code>cutoffdose</code>= 1.5

mlegge
  • 6,763
  • 3
  • 40
  • 67
  • very useful thank you @mkemp6. But I guess the whole point of having a shiny app is to make it 'shiny' and the verbatim text is the less shiny thing that can be done. It was helpful because I learnt how to use `trycatch()` but still not exactly what I want from my shiny app. – Daniele Avancini Jan 23 '15 at 11:02
0

I would try to use function class().

output$table <- renderTable {(
   x <- function (y)
   if(class(x) == "table")
     print(x)

)}
output$text <- renderText {(
   x <- function (y)
   if(class(x) == "list")
     print(x)
)}
Mikael Jumppanen
  • 2,436
  • 1
  • 17
  • 29
  • I see what you mean, and I think the we are in the right direction but I got a: `the condition has length > 1 and only the first element will be used` . I must miss something but thank you very much @Mikael. I'll add any further development. – Daniele Avancini Jan 22 '15 at 17:18
  • @DanieleAvancini take a look at my solution below which will handle errors, warnings and actual output – mlegge Jan 22 '15 at 17:34