我一直在尝试在 R 中创建我的第一个 Shiny 应用程序,它将显示一天中最长可能在 15 到 20.25 小时之间的时间间隔的数据点。 dat 是原始数据表,包含所有 24 小时并且已经初始化。我正在使 dat5 成为一个新的数据表,其中 slider 的 2 个输入作为新的时间间隔。我的代码如下,在实际应用程序中出现此错误:“‘closure’类型的对象不可子集化”,尽管 slider 看起来不错。任何帮助将不胜感激。这是我在控制台中遇到的错误,其代码如下:
错误
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,需要用于获取 slider 的值。所以
c(input[1], input[2])
需要改为c(input$Tc[1], input$TC[2])
<
另一个变化:
- 由于采用常用的
ggplot2
语法,您无需在美学参数中的列名称之前指定dat5()$
。
关于r - 第一个 Shiny 应用程序出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37664368/