使用 pickerInput 后​​的输出 table 没有显示,因为它应该使用 ShinyDashboard

Output table after using pickerInput is not showing as it should using ShinyDashboard

我正在 ShinyDashboard 中创建一个应用程序,我正在使用 pickerInput 以便能够 select 我想使用哪些列。

当我 select 处理所有列(或至少超过 2 列)时,输出 table 看起来不错。

但是,如果我 select 只有两列,则这些列会被置换。它看起来像这样:

你知道这是怎么回事吗?你知道怎么解决吗?

这里有代码:

library(shiny)
library(shinydashboard)
library(magrittr)
library(DT)
library(shinyWidgets)
library(dplyr)

ui <- dashboardPage(
  
  dashboardHeader(title = "Basic dashboard"),
  ## Sidebar content
  dashboardSidebar(
    sidebarMenu(
      menuItem("Table", tabName = "Table", icon = icon("th"))
    )
  ),
  
  dashboardBody(
    fluidRow(
    tabItems(
      
      tabItem(tabName = "Table",
              sidebarPanel(
                
                uiOutput("picker"),
                
                checkboxInput("play", strong("I want to play with my data"), value = FALSE),
                
                conditionalPanel(
                  condition = "input.play == 1",
                  checkboxInput("change_log2", "Log2 transformation", value = FALSE),
                  checkboxInput("run_sqrt", "sqrt option", value = FALSE)),
                
                actionButton("view", "View Selection")
                
              ),
              
              # Show a plot of the generated distribution
              mainPanel(
                dataTableOutput("table")
                
              )
      )
    )
  )
  )
)

server <- function(input, output, session) {
  
  data <- reactive({
    mtcars
  })
  
  data1 <- reactive({
    
    dat <- data()
    
    if(input$change_log2){
      dat <- log2(dat)
    }
    
    if(input$run_sqrt){
      dat <- sqrt(dat)
    }
    
    dat
  })
  
  # This is going to select the columns of the table
  output$picker <- renderUI({
    pickerInput(inputId = 'pick',
                label = 'Select columns to display',
                choices = colnames(data()),
                options = list(`actions-box` = TRUE),multiple = T,
                selected = colnames(data()))
  })
  
  #This function will save the "new" table with the selected columns.
  selected_columns <- eventReactive(input$view,{
    selected_columns <- data1() %>%
      select(input$pick)
    return(selected_columns)
    
  })
  
  output$table <- renderDataTable({
    
    datatable(
      selected_columns(),
      filter = list(position = 'top', clear = FALSE),
      selection = "none",
      rownames = FALSE,
      extensions = 'Buttons',
      
      options = list(
        scrollX = TRUE,
        autoWidth = TRUE,
        dom = 'Blrtip',
        buttons =
          list('copy', 'print', list(
            extend = 'collection',
            buttons = list(
              list(extend = 'csv', filename = "Counts", title = NULL),
              list(extend = 'excel', filename = "Counts", title = NULL)),
            text = 'Download'
          )),
        lengthMenu = list(c(10, 30, 50, -1),
                          c('10', '30', '50', 'All'))
      ),
      class = "display"
    )
    
    
  },rownames=FALSE)
  
}

shinyApp(ui, server)

非常感谢,

此致

一种方法是使用 fluidRow(column(align = "center",...)).

ui <- dashboardPage(

  dashboardHeader(title = "Basic dashboard"),
  ## Sidebar content
  dashboardSidebar(
    sidebarMenu(
      menuItem("Table", tabName = "Table", icon = icon("th")),
      uiOutput("picker"),
      
      checkboxInput("play", strong("I want to play with my data"), value = FALSE),
      
      conditionalPanel(
        condition = "input.play == 1",
        checkboxInput("change_log2", "Log2 transformation", value = FALSE),
        checkboxInput("run_sqrt", "sqrt option", value = FALSE)),
      
      actionButton("view", "View Selection")
    )
  ),

  dashboardBody(
    fluidRow(
      tabItems(

        tabItem(tabName = "Table",
                # Show a plot of the generated distribution
                fluidRow(column(align = "center", width=12, DTOutput("table")))
                
        )
      )
    )
  )
)

server <- function(input, output, session) {

  data <- reactive({
    mtcars
  })

  data1 <- reactive({

    dat <- data()

    if(input$change_log2){
      dat <- log2(dat)
    }

    if(input$run_sqrt){
      dat <- sqrt(dat)
    }

    dat
  })

  # This is going to select the columns of the table
  output$picker <- renderUI({
    pickerInput(inputId = 'pick',
                label = 'Select columns to display',
                choices = colnames(data()),
                options = list(`actions-box` = TRUE),multiple = T,
                selected = colnames(data()))
  })

  #This function will save the "new" table with the selected columns.
  selected_columns <- eventReactive(input$view,{
    selected_columns <- data1() %>%
      select(input$pick)
    return(selected_columns)

  })

  output$table <- renderDT({

    datatable(
      selected_columns(),
      filter = list(position = 'top', clear = FALSE),
      selection = "none",
      rownames = FALSE,
      extensions = 'Buttons',

      options = list(
        scrollX = TRUE,
        autoWidth = TRUE,
        dom = 'Blrtip',
        buttons =
          list('copy', 'print', list(
            extend = 'collection',
            buttons = list(
              list(extend = 'csv', filename = "Counts", title = NULL),
              list(extend = 'excel', filename = "Counts", title = NULL)),
            text = 'Download'
          )),
        lengthMenu = list(c(10, 30, 50, -1),
                          c('10', '30', '50', 'All'))
      ),
      class = "display"
    )


  },rownames=FALSE)

}

shinyApp(ui, server)