2017-05-31 157 views
0

我想添加一個下拉菜單來操縱我的數據框。Shiny R - 表格輸出取決於從下拉菜單中的選擇

例如,

假設我DF具有列 - A1 A2 A3 B1,B2,C1,C2,C3

,我想補充與選項的下拉菜單=清單( 「所有」, 「一」 ,「b」,「c」)將過濾表格以包括所有列,以a開頭或以b開頭的列等等。

不知道如何去做這件事。打開可以創建新的df,當選擇選項或根據選擇使df被操縱時可以調用。

下面是一些示例代碼(不是我真正的代碼):

library(shiny) 
shinyUI(fluidPage(
    titlePanel("x"), 
    sidebarLayout(
    sidebarPanel(
     selectInput(inputId = "x", 
        choices = list("All", "a", "b", "c")) 
    ), 
    mainPanel(
     dataTableOutput('x') 
    ) 
) 
)) 


#dfa <- data.frame(select(df, starts_with("a"))) 

#dfb <- data.frame(select(df, starts_with("b"))) 

#dfc <- data.frame(select(df, starts_with("c"))) 

shinyServer(function(input, output) { 
    output$x = renderDataTable({ 

    ????? 

    }) 
}) 

回答

1

您可以使用反應:

shinyServer(function(input, output) { 

    reactive_df <- reactive({ 
     if(input$x=="All") 
     return df 
     else 
     return(select(df, starts_with(input$x))) 
    } 

    output$x <- renderDataTable(reactive_df()) 

} 
+0

謝謝你的快速反應!很有幫助。 – Jason

+0

如果賈森工作,請接受答案 – Mark

相關問題