Я предлагаю другой ответ, я сделал несколько изменений, сначала я вдохновился базовым уроком:
https://shiny.rstudio.com/tutorial/written-tutorial/lesson4/
Во-вторых, я использую aes_string как в:
Лучший способ преобразовать символьные строки в аргументы функции в R / ggplot2?
Мне кажется более естественным то, что я сделал. Во-вторых, я сделал выбор внутри сервера, я не знаю, насколько он эффективен, но думаю, что он понятен. Во-вторых, ваш cbind
внутри data.frame
вызов плохая идея, потому что вы заставляете каждую переменную иметь одинаковый тип, а это не то, что вам нужно.
year <- c(2000, 2002, 2006, 2000, 2004, 2010, 2010, 2011, 2020, 2006)
Price <- c(100, 200, 300, 120, 240, 400, 430, 490, 700, 650)
colors1 <- c("red", "red", "blue", "green","blue", "red", "blue", "green", "green", "red")
size <- c("s", "m", "l", "xl", "l", "s", "m", "xl", "l","m")
city <- c("NY","LA", "DC","NY","LA", "DC","NY","LA", "DC", "NY")
delivery <- c(3, 7, 3, 10, 20, 5, 10, 2, 12,4)
df <- data.frame(year,
Price,
colors1,
size, city,
delivery)
df$Vatprice <- df$Price * 1.2
str(df)
library(shiny)
library(ggplot2)
ui <- fluidPage(
sidebarPanel(
sliderInput("range",
"Year:", min = 1990,
max = 2040, value = c(2000, 2020)),
textOutput("SliderText"),
selectInput("yaxis",
"Y Variable",
names(df[,c(2,7)]),
selected = names(df)[[2]]),
radioButtons(inputId = "cat",
label = "Categorized by:",
names(df[,3:5]))
),
mainPanel(
plotOutput("scatter")
)
)
server <- function(input, output){
output$scatter <- renderPlot({
select_row_year <- df$year %in% input$range[1]:input$range[2]
y_indices <- which(colnames(df) == input$yaxis)
color <- which(colnames(df) == input$cat)
ggplot(df[select_row_year, ], aes_string(x = "year",
y = colnames(df)[y_indices],
color = colnames(df)[color])) +
geom_point() +
labs(title = "Overview", color = "Legend") +
xlab("Year") + ylab(input$yaxis) + theme_minimal()
})
}
Надеюсь, это поможет вам. Я сделал вашу легенду по оси Y интерактивной.
Редактировать 1:
Это то, что вы ищете:
ui <- fluidPage(
sidebarPanel(
sliderInput("range",
"Year:", min = 1990,
max = 2040, value = c(2000, 2020)),
textOutput("SliderText"),
selectInput("yaxis",
"Y Variable",
names(df[,c(2,7)]),
selected = names(df)[[2]]),
radioButtons(inputId = "cat",
label = "Categorized by:",
names(df[,3:5])),
checkboxGroupInput(inputId = "city",
label = "No city choice",
choices = NULL,
selected = NULL)
),
mainPanel(
plotOutput("scatter")
)
)
server <- function(input, output, session){
observe({
if(input$cat == "city"){
updateCheckboxGroupInput(session=session, inputId = "city",
label = "Select city",
choices = levels(df$city),
selected = NULL)
}
})
output$scatter <- renderPlot({
if(length(input$city) != 0){
select_row <- df$year %in% input$range[1]:input$range[2] & df$city %in% input$city
}else{
select_row <- df$year %in% input$range[1]:input$range[2]
}
y_indices <- which(colnames(df) == input$yaxis)
color <- which(colnames(df) == input$cat)
ggplot(df[select_row, ], aes_string(x = "year",
y = colnames(df)[y_indices],
color = colnames(df)[color])) +
geom_point() +
labs(title = "Overview", color = "Legend") +
xlab("Year") + ylab(input$yaxis) + theme_minimal()
})
}
shinyApp(ui = ui, server = server)