6

I have a simplified version of a shiny application I developed. What I want is to be able to show dataframe1 when Frame1 actionbutton is pressed and hide dataframe2 and again show dataframe2 when Frame2 actionbutton is pressed and hide dataframe1. I need to print the tables at the same location obviously. Thanks for any help and for your time.

server.R

shinyServer(function(input, output, session) {

  data_for_use <- reactiveValues(d=NULL)

  observeEvent(input$actForFrame1,{
  data_for_use$dataFrame1 <- data.frame(firstColumn= c(1,3,5),secondColumn=c(2,4,6))})

  observeEvent(input$actForFrame2,{
  data_for_use$dataFrame2 <- data.frame(firstColumn= c(10,30,50),secondColumn=c(20,40,60))})

  output$dataFrame1 <- DT::renderDataTable({
    DT::datatable(data_for_use$dataFrame1)
  })

  output$dataFrame2 <- DT::renderDataTable({
    DT::datatable(data_for_use$dataFrame2)
  })

  observeEvent(input$actForFrame1,{
    show("dataFrame1")
    hide("dataFrame2")

  })

  observeEvent(input$actForFrame2,{
    show("dataFrame2")
    hide("dataFrame1")

  }) 
})

ui.R

library(shinyjs)
library(shiny)
library(DT)

shinyUI(pageWithSidebar(

  headerPanel("Test"),

  sidebarPanel(

    actionButton("actForFrame1", "Frame1"),
    actionButton("actForFrame2", "Frame2")
  ),

  mainPanel(
    useShinyjs(),
    wellPanel("Test",
              conditionalPanel(condition = "input.actForFrame1",
              DT::dataTableOutput("dataFrame1")
              ),
              conditionalPanel(condition= "input.actForFrame2",
              DT::dataTableOutput("dataFrame2"))
              )
  )
)
)
Alperen Taciroglu
  • 351
  • 1
  • 5
  • 15
  • 1
    Works fine for me. Have you tried clearing your session and making sure you have no other packages loaded? shinyjs show/hide are still working fine – DeanAttali Mar 10 '16 at 20:04
  • I tried restarting R studio and the example I posted above works fine. Then I tried running actual shiny application I developed. It doesnt work and then cant run above example properly either. It seems my actual application is doing something that makes show/hide broken(I am not sure if the problematic part is show hide but I assume so) Other packages I am using with actual application is DT, ggplot2 , affy, GEOquery, shinyjs besides shiny library itself. Anything you can speculate on? – Alperen Taciroglu Mar 10 '16 at 20:59
  • no global variables defined, just reactive values.. – Alperen Taciroglu Mar 10 '16 at 20:59
  • 5
    My only guess is that some other package has a `show` or `hide` functions defined. Maybe try calling `shinyjs::show()` and `shinyjs::hide()`. If not, then I don't know, you'd have to debug it further – DeanAttali Mar 10 '16 at 22:01
  • does the order `library(shinyjs)` is called matter - does `DT` or `shiny` mask `show/hide` – SymbolixAU Mar 11 '16 at 04:34
  • Thank you guys for your comments. shinyjs::show() and shinyjs::hide() solved it... – Alperen Taciroglu Mar 11 '16 at 13:38
  • @Symbolix FYI yes, the order of loading packages matters. The last package you load will overwrite any functions defined in other packages. But I don't think DT or shiny have show/hide functions in them. In fact, when you load a library in the R console, if it masks any already-defined functions, it will tell you that. Try loading dplyr and then loading plyr - it will tell you both times which functions are being "masked" – DeanAttali Mar 17 '16 at 08:05
  • @daattali - that's what I thought, though, that `show/hide` aren't masked by either `shiny` or `DT`, so I am curious why we need to explicitely use `shinyjs::show()` ? – SymbolixAU Mar 17 '16 at 08:07
  • There must be SOME package that's loaded after shinyjs that has those functions defined. I don't have your code so I can't tell you which, but there must be something – DeanAttali Mar 17 '16 at 19:43

1 Answers1

7

Turns out as @Dean Attali pointed out kindly, the functions were using the same namespace with some other package which in turn rendered them not doing their function.

Using shinyjs::show(); shinyjs::hide() solved the issue.

Alperen Taciroglu
  • 351
  • 1
  • 5
  • 15