[英]Shiny — how to output a reactive list?
我正處於學習閃亮狀態的過程中,並且希望能夠根據側面板中復選框組指定的過濾條件在主面板中輸出列表。
復選框代碼如下所示:
checkboxGroupInput("cats",
label = "Which category would you like to see?",
choices = list("Interest",
"Demographics",
"Travel",
"Retail",
"Financial",
"Lifestyle",
"Technology"),
selected = c("Interest", "Demographics", "Travel", "Retail", "Financial", "Lifestyle", "Technology"))
我想知道的是,是否有一種方法可以根據是否選中這些值在主面板中查看列表。 也就是說,如果我已選中“人口統計”和“旅行”,並且我的數據框如下所示:
A B
Interest 7
Interest 2
Demographics 3
Travel 4
Financial 4
Lifestyle 6
Lifestyle 7
Technology 9
我希望能夠在主面板中顯示如下列表:
B
3
4
直截了當:
library(shiny)
myData <- read.table(text = " A B
Interest 7
Interest 2
Demographics 3
Travel 4
Financial 4
Lifestyle 6
Lifestyle 7
Technology 9", header = TRUE, stringsAsFactors = FALSE)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(checkboxGroupInput("cats",
label = "Which category would you like to see?",
choices = unique(myData$A),
selected = unique(myData$A))),
mainPanel(dataTableOutput("table"))
)
)
server <- function(input, output) {
output$table <- renderDataTable({
subset(myData, A %in% input$cats, select = "B")
})
}
shinyApp(ui, server)
您可以使用%in%來檢查checkboxGroupInput中是否選擇了多個選項,並檢索所選選項的數據。 類似於以下內容:
df.selected <- reactive({
foo <- subset(YOUR.DATA(), A %in% input$cats)
return(foo)
})
然后,可以通過將df.selected()['A','B']呈現為表格來將其顯示為輸出。
output$table.show <- renderTable({df.selected()})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.