r - 在 R Shiny 中选择“全部”单选按钮选项
问题描述
我正在尝试创建一个具有单选按钮的闪亮应用程序,并允许用户选择一个“全部”按钮,该按钮显示此过滤器之前的所有数据。
我在下面发布了我的完整代码,这里是当前实时应用程序的链接。
正如在实时应用程序中看到的那样,我希望用户能够选择季度下的“全部”单选按钮,这将显示所有数据,无论季度如何。目前,季度下的其他选项按计划工作。
在找到了一些其他类似的帖子后,我尝试在代码的服务器部分的过滤中设置 if else 函数,并在代码的 ui 部分的单选按钮中创建了选择,但输出没有运气。我也在努力找出是否需要对 ui 代码的“选择”部分进行其他更改。
任何建议将不胜感激!
library(shiny)
library(tidyverse)
library(hrbrthemes)
score_data <- read_csv("https://raw.githubusercontent.com/mikemaieli/superbowlsquares/master/superbowlscores.csv")
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
sliderInput("yearInput",
"Year",
min = 1960,
max = 2020,
value = c(1967, 2019),
sep = "",
ticks = 10),
radioButtons("quarterInput", "Quarter",
choices = c("All" = "???",
"1st quarter" = "1",
"2nd quarter" = "2",
"3rd quarter" = "3",
"4th quarter" = "4",
"Overtime" = "5"),
selected = "1"),
),
mainPanel(
plotOutput("heatmap"),
)
)
)
server <- function(input, output) {
output$heatmap <-
renderPlot({
digit_counts <- score_data %>%
mutate(afc_digit = afc_total_score %% 10, nfc_digit = nfc_total_score %% 10) %>%
select(year, superbowl, quarter, afc_digit, nfc_digit) %>%
mutate_all(as.character) %>%
filter(year >= input$yearInput[1],
year <= input$yearInput[2],
quarter == input$quarterInput) %>%
group_by(afc_digit, nfc_digit) %>%
summarize(occurances = n())
ggplot(digit_counts, aes( x = afc_digit,
y = nfc_digit)) +
geom_tile(aes(fill = occurances), color = "black") +
geom_text(aes(label = scales::percent((occurances/sum(digit_counts$occurances)))),
color = "white") +
scale_fill_gradient(low = "gray75", high = "dodgerblue4", na.value = "white") +
scale_x_discrete(position = "top",
limits = c("0","1","2","3","4","5","6","7","8","9")) +
scale_y_discrete(limits = rev(c("0","1","2","3","4","5","6","7","8","9"))) +
labs(x = "AFC",
y = "NFC") +
theme_minimal() +
theme(panel.grid.major = element_blank(),
legend.position = "none") +
geom_vline(xintercept=c(.5,1.5,2.5,3.5,4.5,5.5,6.5,7.5,8.5,9.5,10.5), color="black", size = .3) +
geom_hline(yintercept=c(.5,1.5,2.5,3.5,4.5,5.5,6.5,7.5,8.5,9.5,10.5), color="black", size = .3)
})
}
shinyApp(ui = ui, server = server)
解决方案
如下更改您的server
功能。
server <- function(input, output, session) { digit_counts <- reactive({
if (input$quarterInput == "???") {
score_data %>%
mutate(afc_digit = afc_total_score %% 10, nfc_digit = nfc_total_score %% 10) %>%
select(year, superbowl, quarter, afc_digit, nfc_digit) %>%
mutate_all(as.character) %>%
group_by(afc_digit, nfc_digit) %>%
summarize(occurances = n())
} else {
score_data %>%
mutate(afc_digit = afc_total_score %% 10, nfc_digit = nfc_total_score %% 10) %>%
select(year, superbowl, quarter, afc_digit, nfc_digit) %>%
mutate_all(as.character) %>%
filter(year >= input$yearInput[1],
year <= input$yearInput[2],
quarter == input$quarterInput) %>%
group_by(afc_digit, nfc_digit) %>%
summarize(occurances = n())
}})
output$heatmap <- renderPlot({
ggplot(digit_counts(), aes( x = afc_digit,
y = nfc_digit)) +
geom_tile(aes(fill = occurances), color = "black") +
geom_text(aes(label = scales::percent((occurances/sum(digit_counts()$occurances)))),
color = "white") +
scale_fill_gradient(low = "gray75", high = "dodgerblue4", na.value = "white") +
scale_x_discrete(position = "top",
limits = c("0","1","2","3","4","5","6","7","8","9")) +
scale_y_discrete(limits = rev(c("0","1","2","3","4","5","6","7","8","9"))) +
labs(x = "AFC",
y = "NFC") +
theme_minimal() +
theme(panel.grid.major = element_blank(),
legend.position = "none") +
geom_vline(xintercept = c(.5,1.5,2.5,3.5,4.5,5.5,6.5,7.5,8.5,9.5,10.5), color = "black", size = .3) +
geom_hline(yintercept = c(.5,1.5,2.5,3.5,4.5,5.5,6.5,7.5,8.5,9.5,10.5), color = "black", size = .3)
})}
在这里,我的理解是,当您选择All
作为您的选择时,不应对数据应用任何过滤器。所以我已经删除了条件中的过滤if
条件。
推荐阅读
- android - 我们更新了一个 Android 应用程序以纠正一个小问题,并陷入位置策略的无限应用程序拒绝循环
- reactjs - 反应选择:closeMenuOnSelect 和 blurInputOnSelect 无法正常工作
- python-3.x - 如何递归地找到整数中的第一个奇数
- android - 如何知道用户是否在短时间内重复安装、卸载和重新安装 Firebase 应用
- wordpress - 页面之间的图像转换
- flutter - 使用 Listview.builder 使屏幕消失
- sql - 对于每一天 x,对于列 z 的每个缺失的唯一值 y,创建一个 date=x 且 z=y 的最新值的行
- excel - 在 Selenium Vba 中读取 2 个原始数据和列数据并循环
- tableau-api - Teradata 和 Tableau 查询问题
- amazon-quicksight - 清单文件大小不能大于 1 MB?