制作具有光泽的动态CheckboxGroupInput [英] Making a dynamic CheckboxGroupInput with shiny
问题描述
我正在尝试选中一个复选框,以便能够按年份过滤数据集。但是,并非每个变量都具有每年的所有数据,因此我只希望变量具有ui中显示的数据的年份。可悲的是,将我的代码分成条件面板后,按钮不再过滤。
I am trying to make a checkbox to be able to filter the dataset by years. However not every variable has all the data for every year so I wanted only the years where the variable has data for shown in the ui. Sadly, after splitting my code into conditional panels the buttons do not filter any longer.
conditionalPanel(condition = "input.Select2 == 'AFD' | input.Select2 == 'Piraten'",
checkboxGroupInput("Year", label = h3("Checkbox group"),
choices = "2013"),
selected = "2013"),
conditionalPanel(condition = "input.Select2 == 'DieLinke'",
checkboxGroupInput("Year", label = h3("Checkbox group"),
choices = list("2009", "2013"),
selected = "2013")),
conditionalPanel(condition = "input.Select2 == 'Gruene' | input.Select2 == 'FDP' | input.Select2 == 'SPD' | input.Select2 == 'CDU'",
checkboxGroupInput("Year", label = h3("Checkbox group"),
choices = list("1998", "2002", "2005","2009", "2013"),
selected = "2013"))
有人知道为什么吗?
以下是在服务器函数中生成的图的示例:
Here's an example of plot being generated in the server function:
dfParteien %>%
filter(Partei %in% chosen_party) %>%
filter(Jahr %in% input$Year) %>%
count(word) %>%
with(wordcloud(word, n, max.words = input$Slider1, colors=pal12))
推荐答案
以下是使用 mtcars
数据集的有效示例,该数据集将帮助您实现动态的 checkboxGroupInput
并进一步过滤数据帧:
Here is a working example using mtcars
dataset which is going to help you achieve dynamic checkboxGroupInput
and further filtering of data frame:
library(shiny)
library(DT)
data <- mtcars
shinyApp(
ui = fluidPage(
selectInput("select1", "select cyl:", choices = unique(data$cyl)),
uiOutput("checkbox"),
dataTableOutput("table")
),
server = function(input, output) {
output$checkbox <- renderUI({
choice <- unique(data[data$cyl %in% input$select1, "gear"])
checkboxGroupInput("checkbox","Select gear", choices = choice, selected = choice[1])
})
output$table <- renderDataTable({
data <- data %>% filter(cyl %in% input$select1) %>% filter(gear %in% input$checkbox)
datatable(data)
})
}
)
您实际上并不需要使用 conditionalPanel
,如果您将 checkboxGroupInput
移至服务器并使用 input $,它的效果会更好选择
以过滤您的选择。
You do not really need to use conditionalPanel
, it works much better in your case if you just move checkboxGroupInput
to the server and use input$select
to filter your choices.
首先:
您的示例代码不好,您应该学习如何创建可复制的示例
第二个:
您的代码有很多错误,例如给三个 checkboxGroupInput
小部件提供相同的ID(年份),这不会在工作中,每个闪亮的小部件的ID必须唯一。
Second of all:
Your code has a quite few mistakes such as giving three checkboxGroupInput
widgets same ID (Year), which is not going to work, the ID must be unique for each widget in shiny.
这篇关于制作具有光泽的动态CheckboxGroupInput的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!