Принимая во внимание комментарий GyD , вот простой пример. Я упростил ваш код, и все еще есть возможности для улучшения:
library(shiny)
library(dplyr)
library(ggplot2)
library(shinythemes)
library(shinyWidgets)
Generation <- c(201806, 201807, 201808, 201809, 201810, 201811, 201812, 201901, 201902, 201903, 201806, 201807, 201808, 201809, 201810, 201811, 201812, 201901, 201902, 201903)
Amount <- c(100, 200, 300, 200, 200, 100, 130, 400, 300, 200, 300, 100, 400, 320, 200, 90, 230, 430, 190, 320)
Rating <- c("A", "B", "A", "B","A", "B","A", "B","A", "B","A", "B","A", "B","A", "B","A", "B","A", "B" )
df1 = data.frame(Generation, Amount, Rating)
ui1 <- fluidPage(
theme = shinytheme("slate"),
sidebarLayout(
sidebarPanel(
sliderTextInput(inputId = "range",
label = "Choose range",
choices = Generation,
selected = range(Generation),
grid = TRUE),
selectInput(inputId = "rat",
label = "Choose the rating",
choices = unique(df1$Rating))
),#sidebar panel
mainPanel(plotOutput("graph1")
)# closing main panel
)# closing sidebarlayout
)# closing fluidpage
server1 = function(input, output) {
#interactive range
# my_range <- reactive({
# cbind(input$range[1],input$range[2])
# })
#create the filter and aggregation
df_final <- reactive({
df1 %>% filter(between(Generation,input$range[1],input$range[2]), Rating == input$rat) %>%
group_by(Generation, Rating) %>%
summarise(sum_amount = sum(Amount))
})
# plot the graph
output$graph1 <- renderPlot({
req(df_final())
ggplot(df_final(), aes(x = Generation, y = sum_amount)) +
geom_line(aes(colour = Rating)) +
geom_point()
})
}
shinyApp(ui1, server1)
Обновление
На вопрос 1 из комментария ниже:
library(shiny)
library(dplyr)
library(ggplot2)
library(shinythemes)
library(shinyWidgets)
Generation <- c(201806, 201807, 201808, 201809, 201810, 201811, 201812, 201901, 201902, 201903, 201806, 201807, 201808, 201809, 201810, 201811, 201812, 201901, 201902, 201903)
Amount <- c(100, 200, 300, 200, 200, 100, 130, 400, 300, 200, 300, 100, 400, 320, 200, 90, 230, 430, 190, 320)
Rating <- c("A", "B", "A", "B","A", "B","A", "B","A", "B","A", "B","A", "B","A", "B","A", "B","A", "B" )
Test <- c(1, 2, 1, 1, 1, 2, 2, 1, 2, 2, 2, 1, 1, 2, 1, 2, 2, 1, 2, 1)
df1 = data.frame(Generation, Amount, Rating, Test)
ui1 <- fluidPage(
theme = shinytheme("slate"),
sidebarLayout(
sidebarPanel(
sliderTextInput(inputId = "range",
label = "Choose range",
choices = Generation,
selected = range(Generation),
grid = TRUE),
selectInput(inputId = "rat",
label = "Choose the rating",
choices = unique(df1$Rating)),
selectInput(inputId = "test",
label = "Choose the test",
choices = unique(df1$Test))
),#sidebar panel
mainPanel(plotOutput("graph1")
)# closing main panel
)# closing sidebarlayout
)# closing fluidpage
server1 = function(input, output) {
#interactive range
# my_range <- reactive({
# cbind(input$range[1],input$range[2])
# })
#create the filter and aggregation
df_final <- reactive({
df1 %>% filter(between(Generation,input$range[1],input$range[2]), Rating == input$rat, Test == input$test) %>%
group_by(Generation) %>%
summarise(sum_amount = sum(Amount))
})
# plot the graph
output$graph1 <- renderPlot({
req(df_final())
ggplot(df_final(), aes(x = Generation, y = sum_amount)) +
geom_line() +
geom_point()
})
}
shinyApp(ui1, server1)
Обратите внимание, как я добавил столбец Test в df1, и рейтинг, и тест находятся в фильтре, но не в group_by.
На вопрос 2 из комментария ниже:
library(shiny)
library(dplyr)
library(ggplot2)
library(shinythemes)
library(shinyWidgets)
Generation <- c(201806, 201807, 201808, 201809, 201810, 201811, 201812, 201901, 201902, 201903, 201806, 201807, 201808, 201809, 201810, 201811, 201812, 201901, 201902, 201903)
Amount <- c(100, 200, 300, 200, 200, 100, 130, 400, 300, 200, 300, 100, 400, 320, 200, 90, 230, 430, 190, 320)
Rating <- c("A", "B", "A", "B","A", "B","A", "B","A", "B","A", "B","A", "B","A", "B","A", "B","A", "B" )
df1 = data.frame(Generation, Amount, Rating)
ui1 <- fluidPage(
theme = shinytheme("slate"),
sidebarLayout(
sidebarPanel(
sliderTextInput(inputId = "range",
label = "Choose range",
choices = Generation,
selected = range(Generation),
grid = TRUE),
selectInput(inputId = "rat",
label = "Choose the rating",
choices = c("A", "B", "A & B - one line", "A & B - two lines"))
),#sidebar panel
mainPanel(plotOutput("graph1")
)# closing main panel
)# closing sidebarlayout
)# closing fluidpage
server1 = function(input, output) {
#interactive range
# my_range <- reactive({
# cbind(input$range[1],input$range[2])
# })
#create the filter and aggregation
df_final <- reactive({
if(input$rat %in% c("A", "B")) {
df1 %>% filter(between(Generation,input$range[1],input$range[2]), Rating == input$rat) %>%
group_by(Generation) %>%
summarise(sum_amount = sum(Amount))
}else if(input$rat == "A & B - one line"){
df1 %>% filter(between(Generation,input$range[1],input$range[2])) %>%
group_by(Generation) %>%
summarise(sum_amount = sum(Amount))
}else if(input$rat == "A & B - two lines"){ # this if isn't necessary but included for clarity
df1 %>% filter(between(Generation,input$range[1],input$range[2])) %>%
group_by(Generation, Rating) %>%
summarise(sum_amount = sum(Amount))
}
})
# plot the graph
output$graph1 <- renderPlot({
req(df_final())
if(input$rat != "A & B - two lines"){
ggplot(df_final(), aes(x = Generation, y = sum_amount)) +
geom_line() +
geom_point()
}else{
ggplot(df_final(), aes(x = Generation, y = sum_amount)) +
geom_line(aes(colour = Rating)) +
geom_point()
}
})
}
shinyApp(ui1, server1)
Обратите внимание, что только для двух линий необходим параметр цвета. По сути, selectInput или radioButton просто указывает выбор в пользовательском интерфейсе (вы можете переименовать их по своему желанию), реальная работа происходит на сервере. Опять же, я уверен, что есть другие способы сделать это, но если вы овладеете функциями Tidyverse, вы сможете манипулировать данными, как вы хотите.