【问题标题】:Unique values for slider range R Shiny滑块范围 R Shiny 的唯一值
【发布时间】:2021-10-12 14:55:39
【问题描述】:

我试图在我的 Shiny 应用程序中只为我的范围滑块选择唯一值。我可以使用 SliderTextInput 来做到这一点,但我正在努力寻找一种方法来为范围滑块做到这一点。请看下面的代码。有什么建议吗?

#Example dataframe:

    df<- data.frame("ID" = c("001","001","001"), "date" = c("2020-07-01 01:00:00","2020-07-01 03:00:00","2020-07-01 06:00:00"))
    
    library(shiny)
    library(move)
    library(amt) 
    library(tibble)
    library(dplyr)
    library(htmltools)
    library(dygraphs)
    library(ggplot2)
    library(plotly)
    library(shinythemes)
    library(shinydashboard)
    library(datetime)
    library(shinyTime)
shinyServer(function(input, output, session) {
    
    observeEvent(input$selectVariable, {
        min<- min(as.POSIXct(df$date))
        max<- max(as.POSIXct(df$date))
        
        
        updateSliderTextInput(session, "month", choices = sort(unique(df$date)), selected = sort(unique(df$date)))

        updateSliderInput(session, "falltime", min = min, max = max, value =sort(unique(as.POSIXct(df$date))), timezone = "MST")
    })
})
    
    shinyUI(navbarPage(
        tabPanel("Analysis",
                 sidebarLayout(
                     sidebarPanel(width = 5,
                                  selectInput("selectVariable", "Select an ID:",
                                  choices =  unique(df$ID)),
                                  sliderTextInput("month",
                                                  "Date Range Correct:",
                                                  choices =  sort(unique(df$date))), #This slider works with the expected behavior but I need it to be a range slider
                                  sliderInput('falltime',"Slider Incorrect Date Range:", min = as.POSIXct("2020-01-01 00:00:00", tz = "MST"), max = as.POSIXct("2020-02-02 00:00:00", tz = "MST"),
                                              value = c(as.POSIXct("2020-01-01 00:00:00", tz = "MST"),as.POSIXct("2020-02-01 00:00:00", tz = "MST"))#, step =
                                  )), #Can't figure out how to make this slider select only unique values
                     mainPanel(h2("Uploaded Data")))
                 )
    
        ) 
    )
        

【问题讨论】:

  • 好的,现在应该可以使用了

标签: r shiny shiny-server


【解决方案1】:

您也可以使用sliderTextInput。它有 choices 参数,可以采用您想要显示的所有唯一值,selected 参数将显示默认选择的第一个范围。

library(shiny)
library(shinyWidgets)

df<- data.frame("ID" = c("001","001","001"), "date" = as.POSIXct(c("2020-07-01 01:00:00","2020-07-01 03:00:00","2020-07-01 06:00:00")))
df

server <- function(input, output, session) {
}

ui <- navbarPage(
  tabPanel("Analysis",
           sidebarLayout(
             sidebarPanel(width = 5,
                          selectInput("selectVariable", "Select an ID:",
                                      choices =  unique(df$ID)),
                          sliderTextInput("month",
                                          "Date Range Correct:",
                                          choices =  sort(unique(df$date))), 
                          sliderTextInput('falltime',"Slider Incorrect Date Range:", 
                                      choices =  unique(df$date), selected = range(df$date)
                          
                          )), 
             mainPanel(h2("Uploaded Data")))
  )
  
) 
shinyApp(ui, server)

【讨论】:

  • 这比我想象的要简单。谢谢!
猜你喜欢
  • 2016-11-06
  • 2015-10-16
  • 1970-01-01
  • 1970-01-01
  • 2014-07-29
  • 1970-01-01
  • 1970-01-01
  • 2021-10-06
  • 1970-01-01
相关资源
最近更新 更多