滑块范围 R Shiny 的唯一值
Unique values for slider range R Shiny
我正在尝试 select 在我的 Shiny App 中只为我的范围滑块设置唯一值。我可以使用 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")))
)
)
)
您也可以使用 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)
我正在尝试 select 在我的 Shiny App 中只为我的范围滑块设置唯一值。我可以使用 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")))
)
)
)
您也可以使用 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)