rshiny渲染时间滑块或选择器

时间:2018-12-10 18:43:27

标签: r shiny slider timeline

我想在R-Shiny中渲染交互式滑块或时间选择器 首先,我从列表中选择一个变量,然后根据该选择从该数据中选择时间间隔。

library("shiny")
Dates = as.POSIXct(c("2018-09-27 23:00:00","2018-09-28 23:00:00","2018-09-29 23:00:00","2018-09-30 23:00:00","2018-10-01 23:00:00")) 
ID = factor(c("2090493005FT", "00209049300S", "0209049300SE", "0209049300SE", "00020904930SE"))   
df <- data.frame(ID = ID,Time = Dates)

ui <- fluidPage(
       fluidRow(
            column(width = 12, offset = 1,
                          selectInput("select", label = h3("Zaehlpunkt"), 
                                      choices = levels(df$ID), 
                                      selected = 1, multiple = TRUE
                                     )
                  )
               ),
uiOutput("Slider")
)
server <- function(input, output, session) {

MinZeitSubset <-  reactive({
  ds<- subset(df,ID == input$select)
  MinZeit <- min(df$Time)
  return(MinZeit)
    })
MaxZeitSubset <-  reactive({
  ds<- subset(df,Zaehlpunkt == input$select)
  MaxZeit <- max(df$TIme)
  return(MaxZeit)
   })
output$Slider<-renderUI({dateRangeInput(inputId = "daterange", 
                                         label = "Zeitabschnitt", 
                                         start = MinZeitSubset, 
                                         end = MaxZeitSubset, 
                                         min = MinZeitSubset,
                                         max = MaxZeitSubset, 
                                         autoclose = TRUE 
                                        )                    
                       })

                                     }
shinyApp(ui,server)

我收到此错误消息:

  

as.vector中的错误:无法将类型'closure'强制转换为类型为vector的向量   '字符'[无可用的堆栈跟踪]

我能够绘制数据表,但无法渲染滑块或数据范围对象! 有帮助吗?

1 个答案:

答案 0 :(得分:0)

您需要不带任何参数调用反应函数,即MinZeitSubset()而不是MinZeitSubset。因此,将您的renderUI修改为

output$Slider <- renderUI({dateRangeInput(inputId = "daterange", 
                                          label = "Zeitabschnitt", 
                                          start = MinZeitSubset(), 
                                          end = MaxZeitSubset(), 
                                          min = MinZeitSubset(),
                                          max = MaxZeitSubset(), 
                                          autoclose = TRUE) 

应以MinZeitSubsetMaxZeitSubset为函数来工作。

运行代码时,我得到一个错误,即Zaehlpunkt的子集中缺少MaxZeitSubset。使用ID可以使所有工作正常进行:

library("shiny")
Dates = as.POSIXct(c("2018-09-27 23:00:00","2018-09-28 23:00:00","2018-09-29 23:00:00","2018-09-30 23:00:00","2018-10-01 23:00:00")) 
ID = factor(c("2090493005FT", "00209049300S", "0209049300SE", "0209049300SE", "00020904930SE"))   
df <- data.frame(ID = ID,Time = Dates)

ui <- fluidPage(
  fluidRow(
    column(width = 12, offset = 1,
           selectInput("select", label = h3("Zaehlpunkt"), 
                       choices = levels(df$ID), 
                       selected = 1, multiple = TRUE
           )
    )
  ),
  uiOutput("Slider")
)

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

  MinZeitSubset <-  reactive({
    ds <- subset(df,ID == input$select)
    MinZeit <- min(df$Time)
    return(MinZeit)
  })

  MaxZeitSubset <-  reactive({
    ds<- subset(df, ID == input$select)
    MaxZeit <- max(df$TIme)
    return(MaxZeit)
  })


  output$Slider<-renderUI({dateRangeInput(inputId = "daterange", 
                                      label = "Zeitabschnitt", 
                                      start = MinZeitSubset(), 
                                      end = MaxZeitSubset(), 
                                      min = MinZeitSubset(),
                                      max = MaxZeitSubset(), 
                                      autoclose = TRUE 
  )                    
  })

}

shinyApp(ui,server)