简体   繁体   English

根据 Slider 范围过滤 R Shiny 数据帧并在数据表中显示结果

[英]Filter R Shiny Data Frame based on Slider range and display result in Data Table

I'm new to R and R Shiny.我是 R 和 R Shiny 的新手。 I'm currently trying to create an R Shiny app to display individual annual Mean Sea Level values in a Data Table over a selected period for a selected location.我目前正在尝试创建一个 R Shiny 应用程序,以在选定时间段内为选定位置在数据表中显示各个年度平均海平面值。 The period is determined by the user selecting a start year and end year from a range slider, while the location is selected by the user from a drop down box.时间段由用户从 slider 范围内选择开始年份和结束年份来确定,而位置由用户从下拉框中选择。

Currently I'm just getting a bunch of html along side the drop down box and slider instead of a two column table showing the data for the selected period and location.目前,我只是在下拉框和 slider 旁边得到一堆 html,而不是显示所选期间和位置的数据的两列表。 See screenshot.见截图。 The period shown in the table should include the start year and end year selected by the user as well as the years in between, with their corresponding Mean Sea Level values shown in the adjacent column.表中显示的时间段应包括用户选择的开始年份和结束年份以及两者之间的年份,其对应的平均海平面值显示在相邻列中。

I realise it's probably an easy fix but being a novice I'm struggling.我意识到这可能是一个简单的解决方法,但作为一个新手,我正在苦苦挣扎。 I eventually want to also show a graph/plot for the selected data and also a trendline.我最终还想显示所选数据的图表/绘图以及趋势线。 But for now I just want to get the filtered data showing in a table so I can expand the table to show some statistical information.但现在我只想将过滤后的数据显示在表格中,以便展开表格以显示一些统计信息。

See my below code attempt.请参阅我下面的代码尝试。

Much appreciated非常感激

Bryan.布莱恩。

从范围滑块过滤的数据表

library(shiny)

Year <- c(2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010)
Auckland <- c(1760, 1549, 1388, 1967, 1326, 1765, 1814, 1693, 1502, 1751)
Wellington <- c(2176, 3154, 1138, 1196, 2132, 3176, 4181, 5169, 3150, 4175)
Lyttelton <- c(2176, 3154, 1138, 1196, 2132, 3176, 4181, 5169, 3150, 4175)
my_data <- as.data.frame(cbind(Year,Auckland,Wellington, Lyttelton))

ui <- fluidPage(
  titlePanel("New Zealand Annual Mean Sea Level (MSL) Summary"),

  sidebarLayout(
    sidebarPanel(
      helpText("Annual Mean Sea Level Summary for various locations around NZ."),

      selectInput("var", 
                  label = "Choose a Location",
                  choices = c("Auckland",
                              "Lyttelton",
                              "Wellington"),
                  selected = "Auckland"),

      sliderInput("range", 
                  label = "Choose a start and end year:",
                  min = min(my_data$Year), max = max(my_data$Year), value = c(2003, 2008),sep = "",)
    ),

    mainPanel(
      textOutput("DataTable")
    )
  )
)
server <- function(input, output) {

  output$DataTable <- renderTable({
    MSL <- my_data[input$range[1]:input$range[2],]
    MSL},include.rownames=FALSE)

}
shinyApp(ui, server)

This should do, renderTable in the server with tableOutput in the ui.应该这样做,在服务器中使用 ui 中的renderTable tableOutput表。 you can also filter by the Year as in my_data[my_data$Year >= input$range[1] & my_data$Year <= input$range[2],]您还可以按Year过滤,如my_data[my_data$Year >= input$range[1] & my_data$Year <= input$range[2],]

library(shiny)

Year <- c(2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010)
Auckland <- c(1760, 1549, 1388, 1967, 1326, 1765, 1814, 1693, 1502, 1751)
Wellington <- c(2176, 3154, 1138, 1196, 2132, 3176, 4181, 5169, 3150, 4175)
Lyttelton <- c(2176, 3154, 1138, 1196, 2132, 3176, 4181, 5169, 3150, 4175)
my_data <- as.data.frame(cbind(Year,Auckland,Wellington, Lyttelton))

ui <- fluidPage(
    titlePanel("New Zealand Annual Mean Sea Level (MSL) Summary"),

    sidebarLayout(
        sidebarPanel(
            helpText("Annual Mean Sea Level Summary for various locations around NZ."),

            selectInput("var", 
                        label = "Choose a Location",
                        choices = c("Auckland",
                                    "Lyttelton",
                                    "Wellington"),
                        selected = "Auckland"),

            sliderInput("range", 
                        label = "Choose a start and end year:",
                        min = min(my_data$Year), max = max(my_data$Year), value = c(2003, 2008),sep = "",)
        ),

        mainPanel(
            tableOutput("DataTable")
        )
    )
)
server <- function(input, output) {

    output$DataTable <- renderTable({
        dt <- my_data[my_data$Year >= input$range[1] & my_data$Year <= input$range[2],]
        dt[,c("Year",input$var)]
    },include.rownames=FALSE)

}
shinyApp(ui, server)

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM