Conditional tabPanel / tabsetPanel using renderUI and uiOutput

2,036 views
Skip to first unread message

Ben Sully

unread,
May 20, 2013, 7:56:35 AM5/20/13
to shiny-...@googlegroups.com
Hi,

I've been trying to create a tabsetPanel which depends on some sidebar inputs, avoiding conditionalPanels, by using renderUI and a function to generate the tabPanel code. I gathered from a post last month that it might be possible but I can't seem to work it! What I have looks roughly like this:

server.R:

output$theTabset <- renderUI({
theOutputs <- returnOutputs(input1, input2, input3)
tabsetPanel(
theOutputs
 )
 })

ui.R:

mainPanel(
... 
uiOutput("theTabset"),
...
)

The output of returnOutputs() is a list with each element being the HTML for a tabPanel:

> returnOutputs(1,1,1)
[[1]]
[1] "<div class=\"tab-pane\" title=\"Panel 1\">\n  <div id=\"panel1Table\" class=\"shiny-html-output\"></div>\n  <div id=\"npanel1Plot\" class=\"shiny-plot-output\" style=\"width: 90% ; height: 500px\"></div>\n</div>"
attr(,"html")
[1] TRUE

[[2]]
[1] "<div class=\"tab-pane\" title=\"Panel 2\">\n  <div id=\"panel2Table\" class=\"shiny-html-output\"></div>\n  <div id=\"npanel1Plot\" class=\"shiny-plot-output\" style=\"width: 90% ; height: 500px\"></div>\n</div>"
attr(,"html")
[1] TRUE

and I've defined output$panel1Table, output$panel1Plot, etc. in server.R.

Everything except the tabsetPanel renders fine, but I get an error:
Error in if (!grepl(pattern, text)) return(text) : 
  argument is of length zero
and no tabsetPanel.

I've also tried changing output$theTabset to exclude the tabsetPanel( ) part in server.R and just return theOutputs, and having uiOutput("theTabset") inside a tabsetPanel in ui.R, but that was even less successful.

Does anyone know any solutions to this?

Cheers,
Ben

Joe Cheng

unread,
May 20, 2013, 12:03:17 PM5/20/13
to shiny-...@googlegroups.com
Try replacing tabsetPanel(theOutputs) with do.call(tabsetPanel, theOutputs).


--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Ben Sully

unread,
May 20, 2013, 12:41:53 PM5/20/13
to shiny-...@googlegroups.com
That worked straight away! Thanks Joe. Is there an easy explanation for why do.call was needed in that situation?

Ben

Joe Cheng

unread,
May 20, 2013, 9:05:24 PM5/20/13
to shiny-...@googlegroups.com
tabsetPanel expects each tabPanel to be a separate argument. do.call takes a function and a list and invokes the function using the list's elements as arguments.

It's the difference between this (without do.call):

tabsetPanel(list(tabPanel(...), tabPanel(...), tabPanel(...)))

and this (with do.call):

tabsetPanel(tabPanel(...), tabPanel(...), tabPanel(...))

Reply all
Reply to author
Forward
0 new messages