第一个 Shiny App 出错

Error in first Shiny App

我一直在尝试在 R 中创建我的第一个 Shiny 应用程序,它会显示一天中某个时间间隔的数据点,最长可能在 15 到 20.25 小时之间。 dat 是原始数据表,它具有所有 24 小时并且已经初始化。我正在使 dat5 成为一个新的数据表,并将来自滑块的 2 个输入作为新的时间间隔。我的代码在下面,我在实际应用程序中收到此错误:"object of type 'closure' is not subsettable" 尽管滑块看起来不错。任何帮助将不胜感激。这是我在控制台中遇到的错误,下面是代码:

错误

Warning in if (!is.na(attribValue)) { 
    the condition has length > 1 and only the first element will be used
Warning in charToRaw(enc2utf8(text)) :
  argument should be a character vector of length 1
all but the first element will be ignored
Warning in if (!is.na(attribValue)) { :
  the condition has length > 1 and only the first element will be used
Warning in charToRaw(enc2utf8(text)) :
  argument should be a character vector of length 1
all but the first element will be ignored
Warning: Error in $: object of type 'closure' is not subsettable
Stack trace (innermost first):
    68: output$plot1
     1: runApp

Warning messages:
1: In .HTMLsearch(query) : Unrecognized search field: title
2: In .HTMLsearch(query) : Unrecognized search field: keyword
3: In .HTMLsearch(query) : Unrecognized search field: alias
4: In .HTMLsearch(query) : Unrecognized search field: title
5: In .HTMLsearch(query) : Unrecognized search field: keyword
6: In .HTMLsearch(query) : Unrecognized search field: alias

代码

ui <- fluidPage(

  sidebarLayout(
    sidebarPanel(sliderInput(Tc, "Time Interval", min = 15, max =20.25,
                         2,    value = c(15,20.25))),
    mainPanel(
      plotOutput("plot1")
    )
  )
)

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

  dat5 <- reactive({
    dat5 <- copy(dat[Tc %between%c(input[1],input[2])])
  })

  output$plot1 <- renderPlot({

    ggplot(dat2, aes(x = dat5$Tc, y = dat5$LastPrice)) +
      geom_line(size = 2, alpha = 0.5) + 
      geom_point(size = 3) + 
      xlab("Time") +
      ylab("Price")+
      theme(text = element_text(size = 18),
        legend.position = 'bottom')
  })

}
shinyApp(ui = ui, server = server)

我构建了一个简单的工作示例:

library(data.table)
library(ggplot2)
library(shiny)

set.seed(707)

dat <- data.table(
  Tc = seq(0, 23.75, by = 0.25),
  LastPrice = exp(rnorm(24 * 4))
)


ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(sliderInput('Tc', "Time Interval", min = 15, max =20.25,
                             2, value = c(15,20.25))),
    mainPanel(
      plotOutput("plot1")
    )
  )
)


server <- function(input, output, session) {
  dat5 <- reactive({
    dat5 <- copy(dat[Tc %between% c(input$Tc[1],input$Tc[2])])
  })

  output$plot1 <- renderPlot({
    ggplot(dat5(),
           aes(x = Tc,
               y = LastPrice)) +
      geom_line(size = 2,
                alpha = 0.5) +
      geom_point(size = 3) +
      xlab("Time") +
      ylab("Price")+
      theme(text = element_text(size = 18),
            legend.position = 'bottom')
  })
}


shinyApp(ui = ui, server = server)

必要的更正:

  • sliderInput 的第一个参数必须是字符值。
  • 此值是该输入的 ID,需要用于获取滑块的值。所以c(input[1], input[2])需要改成c(input$Tc[1], input$TC[2])

另一个变化:

  • 由于通常的 ggplot2 语法,您不需要在美学参数中的列名前指定 dat5()$