0

I've read a few posts (How can put multiple plots side-by-side in shiny r?, How to RenderTable output to tabPanel in NavBar page + Rshiny app), but none seem to solve this problem.

I've got two tabs, and in the main panel of each I'd like to display a plot, and then beneath that a table. I can get the plot working, but can't get the table working. I'm not sure if it's the fact I'm using tabs, as I can put the table in alone, without a plot, when there are no tabs.

Here's the script I'm using. I've commented out the tableOutput('table')) code that I'd like to use to insert the table because with it in neither the plot or table work!

ui <- fluidPage(
  titlePanel("Data"),
  sidebarLayout(
    sidebarPanel(
      
      selectInput(inputId = "sample_1",
                  label = "Select sample 1:",
                  choices = c(unique(instability.data$name))),
      selectInput(inputId = "sample_2",
                  label = "Select sample 2:",
                  choices = c(unique(instability.data$name))),
      selectInput(inputId = "target.channel",
                  label = "Select channel:",
                  choices = channels$name)
      
    ),
    mainPanel(
      tabsetPanel(type = "tabs",
                  tabPanel("Target", plotlyOutput(outputId = "target")),#, tableOutput('table')),
                  tabPanel("Full", plotlyOutput(outputId = "full")))#, tableOutput('table')))
      
    )
  )
)

server <- function(input, output) {
  output$target <- renderPlotly({
    names <- c(input$sample_1, input$sample_2)
    d <- subset(instability.data, name %in% names & channel == input$target.channel & rpt>=start & rpt<=end)
    r <- 
      ggplot(d, aes(x=rpt, y=rfu.rel, fill=name, colour=name)) +
      geom_line(size=0.1) +
      theme_bw() +
      geom_vline(aes(xintercept=start, colour=name), linetype="dotted") +
      geom_vline(aes(xintercept=end, colour=name), linetype="dotted") +
      geom_vline(aes(xintercept=control.mode, colour=name), linetype="dashed") +
      geom_hline(aes(yintercept=ii.threshold, colour=name), linetype="dotted")
    ggplotly(r)
  })
  
  output$full <- renderPlotly({
    names <- c(input$sample_1, input$sample_2)
    d <- subset(instability.data, name %in% names & channel == input$target.channel)
    r <- 
      ggplot(d, aes(x=rpt, y=rfu, fill=name, colour=name)) +
      geom_line(size=0.1) +
      theme_bw() +
      geom_vline(aes(xintercept=start, colour=name), linetype="dotted") +
      geom_vline(aes(xintercept=end, colour=name), linetype="dotted") +
      geom_vline(aes(xintercept=control.mode, colour=name), linetype="dashed") +
      geom_hline(aes(yintercept=ii.threshold, colour=name), linetype="dotted")
    ggplotly(r) #%>%
    
  })
  
  output$table <- renderTable({
    t <- subset(instability.summary, name %in% names)
    t[,-1] <- round(t[,-1], 2)
    t
  })
  
}

shinyApp(ui = ui, server = server)

enter image description here

Mike
  • 921
  • 7
  • 26
  • 1
    Have you tried to uncomment the output("table") for *one* tab only ? Shiny doesn't like when 2 outputs have the same id https://stackoverflow.com/questions/45162511/using-the-same-output-element-twice-in-shiny – gdevaux Aug 31 '21 at 12:51
  • Ah, yes, this works well now! How strange. I'll just create a replica table named something else to go in the second tab. Thanks! – Mike Aug 31 '21 at 12:55
  • you can do `output$table1 <- output$table2 <- renderTable({...})` for example – gdevaux Aug 31 '21 at 12:58
  • That also works perfectly, thanks. Incidentally, in this same script I can't get just to work in shiny, but the same code works perfectly outside shiny. I've added the following to the ggplots: geom_text(aes(label=round(peak.rpt, digits=0)), vjust=-1) + – Mike Aug 31 '21 at 13:05

0 Answers0