我试图创建一个闪亮的应用程序,允许我过滤联赛数据。这是数据集的一个示例:
list <- c(2.3, 2.5, 2.6, 2.8, 4.12, 5.3, 6.2, 8.2)
team <- c("A", "A", "A", "A", "B", "B", "B", "B")
time <- c(1,2,3,4,5,6,7,8)
league <- c("A", "B","A", "B","A", "B","A", "B")
df <- data.frame(list, team, time, league)
我现在使用以下脚本绘制数据:
#ui script
shinyUI(fluidPage(
titlePanel("Basic widgets"),
fluidRow(
column(3,
h3("Buttons"),
selectInput("select", label = h3("Select box"),
choices = list("Choice 1" = 1, "Choice 2" = 2), selected = 1)),
mainPanel(
plotOutput("distPlot")
)
)
))
服务器脚本:
library(shiny)
shinyServer(function(input, output) {
output$distPlot <- renderPlot({
list <- c(2.3, 2.5, 2.6, 2.8, 4.12, 5.3, 6.2, 8.2)
team <- c("A", "A", "A", "A", "B", "B", "B", "B")
time <- c(1,2,3,4,5,6,7,8)
league <- c("A", "B","A", "B","A", "B","A", "B")
df <- data.frame(list, team, time, league)
ggplot(df, aes(x = time, y = list, colour = team)) + geom_line()
})
})
这向我展示了两个联赛的路线。但我想要实现的是我可以选择“Choice1”和“Choice2”,相关的数据点也会显示在图表中。我有什么建议可以将选择的选择与独特的联赛价值联系起来吗?
答案 0 :(得分:0)
您的预期输出并不完全清楚。但是,我认为你问的是如何根据selectInput
中的选择改变情节?
如果是这种情况,您需要通过给定选项过滤数据来使您的情节被动。请参阅我在代码中的注释以获取更多详细信息。
注意:我已将您的应用变为single-file shiny app
library(shiny)
library(ggplot2)
ui <- fluidPage(
titlePanel("Basic widgets"),
fluidRow(
column(3,
h3("Buttons"),
selectInput("select", label = h3("Select box"),
choices = list("A", "B"), selected = 1, multiple = TRUE)),
mainPanel(
plotOutput("distPlot")
)
)
)
server <- function(input, output) {
list <- c(2.3, 2.5, 2.6, 2.8, 4.12, 5.3, 6.2, 8.2)
team <- c("A", "A", "A", "A", "B", "B", "B", "B")
time <- c(1,2,3,4,5,6,7,8)
league <- c("A", "B","A", "B","A", "B","A", "B")
df <- data.frame(list, team, time, league)
output$distPlot <- renderPlot({
## make the plot reactive depending on the input of the selectInput 'select'.
df_plot <- df[df$league %in% input$select, ]
## every time you make a change to selectInput 'select', this plot will update
ggplot(df_plot, aes(x = time, y = list, colour = team)) +
geom_line()
})
}
shinyApp(ui = ui, server = server)