我创建了一个shinyapp,带有制表符,它接受数据帧并允许用户对其进行过滤 - 然后根据过滤器创建直方图。当我在本地计算机上运行它时,此应用程序运行良好。但是,当我将它发布到shinyapps.io时,我收到以下错误:
ERROR: object of type 'closure' is not subsettable
我从其他帖子中了解到数据框架需要被动反应或者可能需要被动反应?但是当我将数据框封装在反应中时如下:
dat <- reactive(df)
我得到了同样的错误。
文件位于github
或在这里:
library(shiny)
library(DT)
#server.R
shinyServer(function(input, output) {
df <- read.csv('orioles.csv')
output$table <- DT::renderDataTable(DT::datatable({
dat <- df
if (input$opp != "All") {
dat <- dat[dat$Opponent == input$opp,]
}
if (input$prk != "All") {
dat <- dat[dat$parkId == input$prk,]
}
if (input$strt != "All") {
dat <- dat[dat$Day.or.Night == input$strt,]
}
dat
}))
output$plot1 <- renderPlot({
dat <- df
if (input$opp != "All") {
dat <- dat[dat$Opponent == input$opp,]
}
if (input$prk != "All") {
dat <- dat[dat$parkId == input$prk,]
}
if (input$strt != "All") {
dat <- dat[dat$Day.or.Night == input$strt,]
}
hist(dat$BAL_SCORE, main = "Based on Filters", xlab = "Runs", ylab = "Frequency of runs scored", breaks = 20, col = "orange" )
})
})
UI:
library(shiny)
library(DT)
navbarPage(
title = 'Navigation',
tabPanel('Data Table', fluidPage(
titlePanel("Baltimore Orioles Game Results from 2010-2015"),
# Create a new Row in the UI for selectInputs
fluidRow(
column(4,
selectInput("opp",
"Opponent:",
c("All",
unique(as.character(df$Opponent))))
),
column(4,
selectInput("prk",
"Ball Park:",
c("All",
unique(as.character(df$parkId))))
),
column(4,
selectInput("strt",
"Day or Night Game:",
c("All",
unique(as.character(df$Day.or.Night))))
)
),
# Create a new row for the table.
fluidRow(
DT::dataTableOutput("table")
)
)),
tabPanel('Runs', fluidPage(
titlePanel("Runs Scored in filtered games"),
fluidRow(
column(6,
plotOutput("plot1", width = 800, height = 600)
)
)
))
)
答案 0 :(得分:0)
使用reactive
并结合您的子集条件,您将获得更简洁的代码:
shinyServer(function(input, output) {
df <- read.csv('orioles.csv')
dat <- reactive(
df[(input$opp == "All" | df$Opponent == input$opp) &
(input$prk != "All" | df$parkId == input$prk) &
(input$strt != "All"| df$Day.or.Night == input$strt),])
output$table <- DT::renderDataTable(DT::datatable(dat()))
output$plot1 <- renderPlot({
hist(dat()$BAL_SCORE, main = "Based on Filters", xlab = "Runs",
ylab = "Frequency of runs scored", breaks = 20, col = "orange" )
})
})
答案 1 :(得分:0)
我能够让我的App工作。发生错误是因为数据框没有像我设置的那样加载。解决方案发布在上面显示的github链接上。