Switch between plot and data table in the same UI container

I have an issue here while executing the code. The logic is working is working perfectly. But if you look the app carefully. The issue is when you click on Values under "Factors under the datasets" the plots are displayed that is good. But when you click on Data table under "Factors under the datasets" the table is getting displayed at the bottom. Can I make it displayed at the top it self where plots were displayed earlier?

df <- structure(list(A = structure(c(1L, 4L, 6L, 1L, 8L, 2L, 7L, 3L, 
                                 5L, 5L, 1L, 8L, 2L, 7L, 2L), .Label = c("asd", "dfg", "fgdsgd", 
                                                                         "fsd", "gdfgd", "gs", "sdfg", "sf"), class = 
    "factor"), B = c(29L, 
                                                                                                                                       24L, 
      46L, 50L, 43L, 29L, 32L, 24L, 35L, 39L, 33L, 47L, 53L, 26L, 
                                                                                                                                       31L), 
        C = structure(c(8L, 5L, 1L, 6L, 3L, 2L, 9L, 7L, 6L, 3L, 
                                                                                                                                                             
       2L, 9L, 8L, 8L, 4L), .Label = c("asd", "er", "fg", "gf", "gfd", 
                                                                                                                                                                                             
        "gfg", "qw", "sf", "tr"), class = "factor"), D = c(36L, 56L, 
                                                                                                                                                                                                                                                
      39L, 26L, 56L, 35L, 27L, 31L, 33L, 45L, 34L, 27L, 43L, 40L, 56L
                                                                                                                                                                                             
      ), E = structure(c(9L, 4L, 3L, 4L, 2L, 7L, 10L, 8L, 6L, 2L, 1L, 
                                                                                                                                                                                                                
       10L, 9L, 9L, 5L), .Label = c("er", "fg", "g", "gd", "gf", "gfg", 
                                                                                                                                                                                                                                             
     "gtd", "qw", "sf", "tr"), class = "factor"), F = c(44L, 34L,  
                                                                                                                                                                                                                                                                                                
        37L, 23L, 37L, 51L, 28L, 36L, 33L, 31L, 39L, 43L, 25L, 37L, 43L
                                                                                                                                                                                                                                             
       ), num = 1:15), row.names = c(NA, -15L), class = "data.frame")

       theNames <- names(df)
      MyList  <- vector(mode = "list")
      for(i in theNames){
      MyList[[i]] <- df[,i]
     }
     library(ggplot2)
     library(dplyr)
      library(shiny)

      ui <- fluidPage(
      tabsetPanel(tabPanel(
      "Factor_Bivariate_Analysis",
       sidebarLayout(sidebarPanel(
       fluidRow(
          column(h6(
      selectInput("se4", "Factors under the
                  datasets", choices = c("", "Values","Data table"))
      ), width = 5, offset =
        0),
    br(),
    column(h6(
      actionButton("Val", "See the Values", width =
                     200, offset =
                     -1)
    ), width = 5, offset = 0),
    br(),
    column(h6(selectInput(
      "state", "Filters", choices = c("",MyList)
    )), width = 5, offset = 0)
    ), width =
     1000
  ),
  mainPanel(
    h5(plotOutput(
      "Plot4", width = "1000px", height =
        "1000px"
    ), width = 1000), h5(dataTableOutput("Plot5"), width = 1000)
  ))
     )))


     server <- function(input, output, session) {
       f_data <- reactive({
         wanted_case <- input$state
         cat("selected case ", wanted_case, "\n\n")
         if (wanted_case == ""){
         fd <- df
      } else {
       fd <- df %>% filter_if(.predicate = is.factor,.vars_predicate = any_vars (. == 
                                                                              wanted_case))
    print(fd)
      }
      return(fd)
       }) 
        Plot4 <- reactive({
         if (input$se4 == "Values") {
           print(ggplot(data = 
                 f_data(),aes(x=num,y=B,fill=A))+geom_line()+facet_wrap("A",ncol=1,nrow=8, 
       scales = 
                                                                          "free"))
} else if (input$se4 == "NULL") {
  ""
}
      })
        output$Plot4 <- renderPlot({
Plot4()
       })
       Plot5 <- reactive({
if (input$se4 == "Data table") {
  print(data.frame(df))
} else if (input$se4 == "NULL") {
  ""
       }
           })
         output$Plot5 <- renderDataTable({
Plot5()
    })
  }

   shinyApp(ui, server)

Hi @vinayprakash808. Try render the ui in server.
In ui, replace with the uiOutput.

uiOutput("plot")
# h5(plotOutput(
#   "Plot4", width = "1000px", height =
#     "1000px"
# ), width = 1000), h5(dataTableOutput("Plot5"), width = 1000)

In server, render each ui depend on input$se4.

  observe({
    req(input$se4)
    switch(input$se4,
           Values = {
             output$plot <- renderUI({
               plotOutput(
                 "Plot4", width = "1000px", height =
                   "1000px"
               )
             })
           },
           "Data table" = {
             output$plot <- renderUI({
               dataTableOutput("Plot5")
             })
           })
  })

Hope the script can help.

No. It is still the same. :frowning: not coming

@vinayprakash808. Please try the script which is okay for me.

df <- structure(
  list(
    A = structure(
      c(1L, 4L, 6L, 1L, 8L, 2L, 7L, 3L,
        5L, 5L, 1L, 8L, 2L, 7L, 2L),
      .Label = c("asd", "dfg", "fgdsgd",
                 "fsd", "gdfgd", "gs", "sdfg", "sf"),
      class =
        "factor"
    ),
    B = c(
      29L,
      24L,
      46L,
      50L,
      43L,
      29L,
      32L,
      24L,
      35L,
      39L,
      33L,
      47L,
      53L,
      26L,
      31L
    ),
    C = structure(
      c(8L, 5L, 1L, 6L, 3L, 2L, 9L, 7L, 6L, 3L,
        
        2L, 9L, 8L, 8L, 4L),
      .Label = c("asd", "er", "fg", "gf", "gfd",
                 
                 "gfg", "qw", "sf", "tr"),
      class = "factor"
    ),
    D = c(
      36L,
      56L,
      
      39L,
      26L,
      56L,
      35L,
      27L,
      31L,
      33L,
      45L,
      34L,
      27L,
      43L,
      40L,
      56L
      
    ),
    E = structure(
      c(9L, 4L, 3L, 4L, 2L, 7L, 10L, 8L, 6L, 2L, 1L,
        
        10L, 9L, 9L, 5L),
      .Label = c("er", "fg", "g", "gd", "gf", "gfg",
                 
                 "gtd", "qw", "sf", "tr"),
      class = "factor"
    ),
    F = c(
      44L,
      34L,
      
      37L,
      23L,
      37L,
      51L,
      28L,
      36L,
      33L,
      31L,
      39L,
      43L,
      25L,
      37L,
      43L
      
    ),
    num = 1:15
  ),
  row.names = c(NA,-15L),
  class = "data.frame"
)

theNames <- names(df)
MyList  <- vector(mode = "list")
for (i in theNames) {
  MyList[[i]] <- df[, i]
}
library(ggplot2)
library(dplyr)
library(shiny)

ui <- fluidPage(tabsetPanel(tabPanel(
  "Factor_Bivariate_Analysis",
  sidebarLayout(sidebarPanel(
    fluidRow(
      column(h6(
        selectInput(
          "se4",
          "Factors under the
          datasets",
          choices = c("", "Values", "Data table")
        )
      ), width = 5, offset =
        0),
      br(),
      column(h6(
        actionButton("Val", "See the Values", width =
                       200, offset =
                       -1)
      ), width = 5, offset = 0),
      br(),
      column(h6(
        selectInput("state", "Filters", choices = c("", MyList))
      ), width = 5, offset = 0)
      ), width =
      1000
    ),
    mainPanel(
      uiOutput("plot")
    # plotOutput(
    #     "Plot4", width = "1000px", height =
    #       "1000px"
    #   ), dataTableOutput("Plot5")
    ))
  )))


server <- function(input, output, session) {
  f_data <- reactive({
    wanted_case <- input$state
    cat("selected case ", wanted_case, "\n\n")
    if (wanted_case == "") {
      fd <- df
    } else {
      fd <-
        df %>% filter_if(.predicate = is.factor,
                         .vars_predicate = any_vars (. ==
                                                       wanted_case))
      print(fd)
    }
    return(fd)
  })
  Plot4 <- reactive({
    if (input$se4 == "Values") {
      print(
        ggplot(data =
                 f_data(), aes(
                   x = num, y = B, fill = A
                 )) + geom_line() + facet_wrap(
                   "A",
                   ncol = 1,
                   nrow = 8,
                   scales =
                     "free"
                 )
      )
    } else if (input$se4 == "NULL") {
      ""
    }
  })
  output$Plot4 <- renderPlot({
    Plot4()
  })
  Plot5 <- reactive({
    if (input$se4 == "Data table") {
      print(data.frame(df))
    } else if (input$se4 == "NULL") {
      ""
    }
  })
  output$Plot5 <- renderDataTable({
    Plot5()
  })
  
  observe({
    req(input$se4)
    switch(input$se4,
           Values = {
             output$plot <- renderUI({
               plotOutput(
                 "Plot4", width = "1000px", height =
                   "1000px"
               )
             })
           },
           "Data table" = {
             output$plot <- renderUI({
               dataTableOutput("Plot5")
             })
           })
  })
}

shinyApp(ui, server)

Perfect thanks But I still did not understand below code, What is the importance of this

  observe({
 req(input$se4)
 switch(input$se4,
       Values = {
         output$plot <- renderUI({
           plotOutput(
             "Plot4", width = "1000px", height =
               "1000px"
           )
         })
       },
       "Data table" = {
         output$plot <- renderUI({
           dataTableOutput("Plot5")
         })
       })
   })

When you define the plotOutput and dataTableOutput in ui. They will create the container and hold up the place. So, that why even no plot render, the space still reserved for it. And the datatable was render at bottom.
If we just define there was an ui by uiOutput but without specific which output, space will reserve for the uiOutput only. Then, we can define which outputs will fill the container of the uiOutput conditionally by input$se4. So that when input$se4 == "Values", we fill the uiOutput with plotOutput. When input$se4 == "Data table", we fill the uiOutput with dataTableOutput. Each time just one output fill the uiOutput and then no shift of datatable.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.