在R Shiny中获取反应数据框的行名并创建日期范围滑块

时间:2019-08-19 13:43:26

标签: r shiny reactive

我有一个以时间序列为索引的数据框。数据框中的数据通过仪表板操作(例如,下载按钮)进行更新,因此该数据框是反应性的。使用滑块,我希望只能选择数据框的某些行。因此,滑块的min max值是指反应数据帧的行名。到目前为止,我还无法实现这一目标。下面的代码。 SERVER部分的if(0)部分是我正在谈论的部分。任何帮助表示赞赏。

require(shiny)

AquireData <- function(){
  # In this function the data are created
  df <- data.frame(replicate(3,sample(0:50,1000,rep=TRUE)))
  rownames(df)  <- seq(from = as.POSIXct("2012-05-15 07:00"), 
                       to = as.POSIXct("2019-05-17 18:00"), by = "min")[0:dim(df)[1]]
  names(df) <- c('A','B','C')
  return (df)
}


ui <- fluidPage(
  # App title
  titlePanel("my dashboard"),

  # define stuff for the sidebar (buttons, selectlists etc.). These items will
  # be displayed for all panels
  sidebarLayout(
    sidebarPanel(
      actionButton("Button_GetAndUpdate", "Update data"),
      sliderInput("start_end_dates", "Date range", min =0, max=0, value=1)
    ),

    # Main panel. Here you can display your graphs, plots and tables
    mainPanel("observed data", tableOutput("rawdata"))      

  )
)


server <- function(input, output,session) {
  # When the app is called an update of the data is drawn
  df_data <- reactive({AquireData()})

  # Check what the update button is doing. If its getting pressed pull and update
  observeEvent (input$Button_GetAndUpdate,{df_data <<- reactive({AquireData()})})  


  # set date range slider values using the dates from the data frame index
  if(0){
    updateSliderInput(session, "start_end_dates",
                      label = "Date range",
                      min = as.POSIXct(min(rownames(df_data())),"%Y-%m-%d %H:%M:%S",tz=""),
                      max = as.POSIXct(max(rownames(df_data())),"%Y-%m-%d %H:%M:%S",tz="")
    )
  }
  # get the head of the dataframe
  data_head <- reactive({
    input$Button_GetAndUpdate
    isolate({
      head(df_data())
    })
  })

  output$rawdata <- renderTable({
    data_head()
  })   
}

shinyApp(ui = ui, server = server)
runApp("Header_dashboard")

1 个答案:

答案 0 :(得分:1)

为此,您可以分别使用shinyWidgets::sliderTextInputshinyWidgets::updateSliderTextInput代替sliderInput

shinyWidgets::updateSliderTextInput(
  session, "start_end_dates",
  choices = rownames(df_data())
)

对于您的应用程序而言:

require(shiny)

AquireData <- function(){
  # In this function the data are created
  df <- data.frame(replicate(3,sample(0:50,1000,rep=TRUE)))
  rownames(df)  <- seq(from = as.POSIXct("2012-05-15 07:00"), 
                       to = as.POSIXct("2019-05-17 18:00"), by = "min")[0:dim(df)[1]]
  names(df) <- c('A','B','C')
  return (df)
}


ui <- fluidPage(
  # App title
  titlePanel("my dashboard"),

  # define stuff for the sidebar (buttons, selectlists etc.). These items will
  # be displayed for all panels
  sidebarLayout(
    sidebarPanel(
      actionButton("Button_GetAndUpdate", "Update data"),
      shinyWidgets::sliderTextInput(
        "start_end_dates", 
        label = "Time range",
        choices = c(as.POSIXct("2019-01-01 12:00:00"), as.POSIXct("2019-12-31 14:00:00")), 
      )
    ),

    # Main panel. Here you can display your graphs, plots and tables
    mainPanel("observed data", tableOutput("rawdata"))      

  )
)


server <- function(input, output,session) {
  # When the app is called an update of the data is drawn
  df_data <- reactive({AquireData()})

  # Check what the update button is doing. If its getting pressed pull and update
  observeEvent (input$Button_GetAndUpdate,{df_data <<- reactive({AquireData()})})  


  # set date range slider values using the dates from the data frame index
  observe({
    shinyWidgets::updateSliderTextInput(
      session, "start_end_dates",
      choices = rownames(df_data())
    )
  })
  # get the head of the dataframe
  data_head <- reactive({
    input$Button_GetAndUpdate
    isolate({
      head(df_data())
    })
  })

  output$rawdata <- renderTable({
    data_head()
  })   
}

shinyApp(ui = ui, server = server)