我一直在尝试在R中创建我的第一个Shiny应用程序,该应用程序将显示从一天中的时间间隔开始的数据点,最长可能在15到20.25小时之间。 dat是原始数据表,全部24小时,已经初始化。我正在将dat5作为一个新的数据表,滑块的2个输入作为新的时间间隔。我的代码在下面,我在实际应用程序中收到此错误:“类型'闭包'的对象不是子集”,尽管滑块看起来很好。任何帮助将不胜感激。这是我在控制台中得到的错误,下面是代码:
错误
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)
答案 0 :(得分:3)
我构建了一个简单的工作示例:
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
的第一个参数需要是字符值。c(input[1], input[2])
需要更改为c(input$Tc[1], input$TC[2])
另一个变化:
ggplot2
语法,因此您无需在美学参数中的列名前指定dat5()$
。