我正在尝试使用具有2008-2016年的滑块来更改2个ValueBoxes中的值(每年属性'pf_score'和'ef_score'的平均值)。 输出是我想要的可见的,但我也看到一个错误“类型为'closure'的对象不可子集”
更新:我无法通过单击Run-App来运行完整的代码。我收到错误消息“找不到功能df1”。我必须先分别读取所有数据帧,然后单击Run-App来查看UI。
require(shiny)
require(dplyr)
require(shinydashboard)
shinyServer(function(input,output){
df <- read.csv("hfi_cc_2018.csv", header = T)
summary(df)
sapply(df, function(x) sum(is.na(x)))
#Replace Null Values
df[is.na(df)] <- 0
df[,5:ncol(df)] <- round(df[,5:ncol(df)], 2)
#adding selective columns new df1
#https://stackoverflow.com/questions/10085806/extracting-specific-columns-from-a-data-frame
df1<- df[, (names(df) %in% c("year","pf_score", "ef_score"
))]
output$select_years <- renderUI(
{
card <- df1 %>%
filter(year == input$years)
output$pfrank = renderValueBox(
valueBox(round(mean(card$pf_score),1),
"Personal Freedom Score")
)
output$efrank = renderValueBox(
valueBox(round(mean(card$ef_score),1),
"Economic Freedom Score")
)
}
)
})
require(shiny)
require(shinydashboard)
shinyUI(
dashboardPage(
dashboardHeader(title = "Human Freedom Index", titleWidth = 300),
dashboardSidebar(
sliderInput("years","Select Year:",
min = min(df1$year),
max = max(df1$year),
value = min(df1$year),
step = 1),
selectInput("variable","Select Freedom Factor:",
choices = colnames(df1)
)
),
dashboardBody(
uiOutput("select_years"),
fluidRow(
valueBoxOutput("pfrank"),
valueBoxOutput("efrank")
)
)
)
)
答案 0 :(得分:1)
在Error in <my code> : object of type 'closure' is not subsettable
中讨论了这种类型的错误。在这种情况下,好像您有card
作为普通数据帧,而您需要一个reactive
,以便在移动滑块时重新计算它。同样,renderUI
的表达式可以简化为仅一个列表。例如
ui <- shinyUI( ... )
server <- function(input, output) {
card <- reactive({
df1 %>%
filter(year == input$years)
})
output$select_years <- renderUI(
c(renderValueBox(valueBox(round(mean(card()$pf_score), 1),
"Personal Freedom Score")),
renderValueBox(valueBox(round(mean(card()$ef_score), 1),
"Economic Freedom Score"))))
}
shinyApp(ui, server)
还要注意,新版本的Shiny简化了语法。该代码仅可以输入app.R
,您需要定义ui
和server
。
答案 1 :(得分:0)
您可以使用observe()
而不是renderUI
来渲染值框:
require(shiny)
require(dplyr)
require(shinydashboard)
df <- read.csv("hfi_cc_2018.csv", header = T)
summary(df)
sapply(df, function(x) sum(is.na(x)))
#Replace Null Values
df[is.na(df)] <- 0
df[,5:ncol(df)] <- round(df[,5:ncol(df)], 2)
#adding selective columns new df1
#https://stackoverflow.com/questions/10085806/extracting-specific-columns-from-a-data-frame
df1 <- df[, (names(df) %in% c("year","pf_score", "ef_score"))]
#UI
ui <- dashboardPage(
dashboardHeader(title = "Human Freedom Index", titleWidth = 300),
dashboardSidebar(
sliderInput("years","Select Year:",
min = min(df1$year),
max = max(df1$year),
value = min(df1$year),
step = 1),
selectInput("variable","Select Freedom Factor:",
choices = colnames(df1)
)
),
dashboardBody(
fluidRow(
valueBoxOutput("pfrank"),
valueBoxOutput("efrank")
)
)
)
#Server
server <- function(input,output){
observe({
card <- df1 %>%
filter(year == input$years)
output$pfrank = renderValueBox(
valueBox(round(mean(card$pf_score),1),
"Personal Freedom Score")
)
output$efrank = renderValueBox(
valueBox(round(mean(card$ef_score),1),
"Economic Freedom Score")
)
})
}
#Run app
shinyApp(ui, server)