首页 > 解决方案 > 如何使用数据框值自定义 R Shiny 中的日期选择

问题描述

我有一个数据框,其中有一列中的日期和其他相关变量,对于相同的日期,我有要显示的栅格和矢量信息。我正在构建一个闪亮的仪表板,我想根据数据可用的日期自定义选择。如您所见,我使用的是最小和最大日期,而不是步骤。我已经阅读了指向 stackexchange 的其他帖子链接,其中他们使用步骤来定义其间的天数,但在我的情况下,例如,我没有每 5 天的数据。所以我想使用数据框中的日期作为步骤。可能吗?除此之外,我不知道为什么我在表格中的日期显示为数字。任何提示以正确的格式呈现它们?

对于下面的代表,我不包括栅格或矢量数据,但同样的原则适用。我过滤栅格数据的方式是使用带有日期信息的名称,例如 id_date.tif(例如:“231_2020-01-23.tif”)

#

library(shiny)

#dummy data for reprex

dd<-c("2020-01-02","2020-01-05","2020-01-12","2020-06-06")
yield<-c(12 , 13, 15, 22)
ph<-c(0.55,0.60,0.65,0.8)  
df<-data.frame(dd,yield,ph)
str(df)
df

df$dd<-as.Date(dd,"%Y-%m-%d",origin="1970-01-01")
str(df)

ui <- fluidPage(

    # Application title
    titlePanel("custom slider with dates"),

    # Sidebar with a slider input for number of bins 
    sidebarLayout(
        sidebarPanel(


            sliderInput("date_s",
                        "Time of data collection: ",
                        min = min(df$dd),
                        max = max(df$dd),
                        value = min(df$dd),
                        #step = df$dd[1],
                        animate = TRUE)
        ),

        # Show a plot of the generated distribution
        mainPanel(
            h3("Table 1. Date selection"),
            tableOutput("table1"),
            h3("Table 2. DF subset based on date selection"),
            tableOutput("table2"),
            h3("Table 3. Complete DF"),
            tableOutput("table3")

        )
    )
)


server <- function(input, output) {

    #table 1 selection
    output$table1<-renderTable({
        ds<-format(as.Date(input$date_s,origin="1970-01-01"),"%Y-%m-%d")
        return(ds)
    })
    
    #table 2 subset
    output$table2<-renderTable({
        dfs<-df[df$dd == input$date_s,]
        return(dfs)
    })
    
    #table 3 all
    output$table3<-renderTable({
    
        return(df)
    })
}

shinyApp(ui = ui, server = server)

标签: rdateshiny

解决方案


推荐阅读