2017-11-04 68 views
1

我想使用用戶可以更改的radioButtons在數據表中顯示列。使用radioButtons在數據表中顯示列(閃亮)

到目前爲止,我的代碼是:

UI:

fluidRow(column(4, radioButtons("radio","Choose:",c("A","B"))))), 
fluidRow(DT::dataTableOutput("table")) 

服務器:

輸出

$table <- DT::renderDataTable({ 
    DT::datatable({table 

    if (input$radio != "A") { 
    table <- table[,1:5] 
    } 

    if (input$radio != "B") { 
    table <- table[,6:10)] 
    } 

    table 

})},rownames = FALSE,options = list(lengthMenu = c(25,50,100))) 

當我運行應用程序時,它顯示的是帶A和B但無數據表的radioButton。

+1

爲什麼不只是使用colVis擴展? https://rstudio.github.io/DT/extensions.html –

+0

在你的'output $ table'的第2行,'DT :: datatable({table'什麼是「表」? –

+0

@Addison Hayes Can you如果對你有幫助,接受答案? – Santosh

回答

0

你的代碼有很多問題。我修復了您的代碼並在mtcars數據集上進行了測試。

library(shiny) 

ui <- fluidPage(

    sidebarLayout(
    sidebarPanel(
     radioButtons("radio","Choose:", choices = list("A" = "A", "B" = "B")) 
    ), 

    mainPanel(
     dataTableOutput("table") 
    ) 
    ) 
) 

server <- function(input, output) { 

    output$table <- renderDataTable({ 
    if (input$radio == "A") { 
     table1 <- mtcars[,1:5] 
    } 
    else { 
     table1 <- mtcars[,6:10] 
    } 
    table1 
    }) 
} 

# Run the application 
shinyApp(ui = ui, server = server) 

如果您運行此應用程序,您將看到它顯示基於您單選按鈕選擇的數據表。