[英]In R/Shiny positioning ggplots chart in specifics places after select input choice
我的主图表来自 select 输入,它显示在plotOutput('chart_1')
在此之后,从复选框输入中删除了在 SelectInput 上选择的国家/地区,然后用户应该只选择 3 个图表,它们将放置在plotOutput('chart_2')
、 plotOutput('chart_3')
、 plotOutput('chart_4')
换句话说:如果我选择 at selectInput 'Brazil' 我将只有 5 个选项可以包含在位置plotOutput('chart_2')
、 plotOutput('chart_3')
、 plotOutput('chart_4')
和 'Brazil" 将不再在复选框输入小部件处可用。
我的问题是我无法使用awesomeCheckboxGroup
小部件正确设置这个想法并包括用户将选择的其他 3 个图表。 这是我到目前为止所做的:
library(shiny)
library(ggplot2)
library(tidyverse)
library(gapminder)
library(shinyWidgets)
gapminder_df <-
gapminder %>% filter(country %in% c("Brazil", "Chile", "Argentina", "Peru")) %>%
group_split(country)
chart1 <-
ggplot(gapminder_df[[1]], aes(x = year, y = country)) + geom_line(color = 'red')
chart2 <-
ggplot(gapminder_df[[2]], aes(x = year, y = country)) + geom_line(color = 'green')
chart3 <-
ggplot(gapminder_df[[3]], aes(x = year, y = country)) + geom_line(color = 'blue')
chart4 <-
ggplot(gapminder_df[[4]], aes(x = year, y = country)) + geom_line(color = 'black')
chart5 <-
ggplot(gapminder_df[[4]], aes(x = year, y = country)) + geom_line(color = 'black')
chart6 <-
ggplot(gapminder_df[[4]], aes(x = year, y = country)) + geom_line(color = 'black')
all_countries <-
c("Brazil", "Chile", "Argentina", "Peru", "Uganda", "Turkey")
names(all_countries) <-
c("Brazil", "Chile", "Argentina", "Peru", "Uganda", "Turkey")
chart_list <-
list(chart1, chart2, chart3, chart4, chart5, chart6) %>% setNames(all_countries)
ui <- fluidPage(
selectInput(
inputId = 'country',
choices = all_countries,
label = "Paises"
),
awesomeCheckboxGroup(
inputId = "countries_check",
label = "Checkboxes",
choices = all_countries
),
div(
column(width = 6,
plotOutput('chart_1', height = '400px')),
column(
width = 6,
plotOutput('chart_2'),
plotOutput('chart_3'),
plotOutput('chart_4')
)
)
)
server <- function(input, output, session) {
output$chart_1 <- renderPlot({
chart_list[[input$country]]
})
# observeEvent(input$country,
#
# {
# if (input$country == input$countries_check) {
# awesomeCheckboxGroup(
# label = "Checkboxes",
# inputId = "countries_check",
# choices = input$country[-1]
# )
# }
output$chart_2 <- renderPlot({
chart_list[[input$countries_check[1]]]
})
output$chart_3 <- renderPlot({
chart_list[[input$countries_check[2]]]
})
output$chart_4 <- renderPlot({
chart_list[[input$countries_check[3]]]
})
# })
}
shinyApp(ui, server)
这可以通过使用observeEvent
更新您的复选框输入来实现。 为此,我在 UI 中将选项设置为NULL
,然后在observeEvent
中使用updateAwesomeCheckboxGroup
更新或设置不包括所选国家/地区的可用选项:
library(shiny)
library(ggplot2)
library(tidyverse)
library(gapminder)
library(shinyWidgets)
ui <- fluidPage(
selectInput(
inputId = "country",
choices = all_countries,
label = "Paises"
),
awesomeCheckboxGroup(
inputId = "countries_check",
label = "Checkboxes",
choices = NULL
),
div(
column(
width = 6,
plotOutput("chart_1", height = "400px")
),
column(
width = 6,
plotOutput("chart_2"),
plotOutput("chart_3"),
plotOutput("chart_4")
)
)
)
server <- function(input, output, session) {
output$chart_1 <- renderPlot({
chart_list[[input$country]]
})
observeEvent(input$country, {
updateAwesomeCheckboxGroup(
session = session,
inputId = "countries_check",
choices = all_countries[!all_countries == input$country]
)
})
n_countries_checked <- reactive({
length(input$countries_check)
})
output$chart_2 <- renderPlot({
req(n_countries_checked() > 0)
chart_list[[input$countries_check[[1]]]]
})
output$chart_3 <- renderPlot({
req(n_countries_checked() > 1)
chart_list[[input$countries_check[[2]]]]
})
output$chart_4 <- renderPlot({
req(n_countries_checked() > 2)
chart_list[[input$countries_check[[3]]]]
})
}
shinyApp(ui, server)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.