R 错误:类型为';关闭';不可再附加

R 错误:类型为';关闭';不可再附加,r,shiny,shinydashboard,R,Shiny,Shinydashboard,我试图在slider的帮助下更改2个值框中的值(每年属性“pf_分数”和“ef_分数”的平均值),slider的使用期限为2008-2016年。 正如我所希望的,输出是可见的,但我也看到了一个错误“类型为“closure”的对象不可子集” 更新:我无法通过单击run App来运行整个代码。我收到一个错误“找不到函数df1”。我必须先分别读取所有数据帧,然后单击Run App查看UI 服务器.r 您可以使用observe()来呈现值框,而不是renderUI: require(shiny) re

我试图在slider的帮助下更改2个值框中的值(每年属性“pf_分数”和“ef_分数”的平均值),slider的使用期限为2008-2016年。 正如我所希望的,输出是可见的,但我也看到了一个错误“类型为“closure”的对象不可子集”

更新:我无法通过单击run App来运行整个代码。我收到一个错误“找不到函数df1”。我必须先分别读取所有数据帧,然后单击Run App查看UI

服务器.r
您可以使用
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)
require(闪亮)
需要(dplyr)
要求(仪表板)

df此类错误在以下章节中讨论:

在这种情况下,看起来像是将
作为普通数据帧,而您需要一个
反应式
,以便在移动滑块时重新计算。此外,
renderUI
的表达式可以简化为一个列表。e、 g

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)

ui将
df
更改为
df
,看看发生了什么事。Michael,尝试了更改,但仍然无法工作!但是错误改变了吗?没有。是相同的错误。此外,我刚刚意识到我无法使用run App运行我的代码。我必须一个接一个地运行所有数据帧,然后单击run应用程序才能成功查看UI。非常感谢。成功了。你能解释一下观察的目的吗?没问题
observe()
侦听
input$years
的值,并在每次
input$years
更改时重新执行其中的表达式
observeEvent(
input$years
,{…})
将给出相同的结果。
render\uu
函数内的表达式必须返回闪亮的标记对象、HTML或此类对象的列表,而您问题中的
renderUI
返回的对象是
output$efrank
,其中包含更新值框所需的代码,即定义闭包的反应式表达式。我可能弄错了,但我认为这就是错误的来源。PS全局变量(如
df
)应该在ui/服务器函数之外或在应用程序目录中的
Global.R
文件中定义。在我的回答中,我在UI调用之前定义了它。这是一个比我发布的更好的解决方案,我希望@Advait更改接受的答案以反映这一点@James为什么
renderUI({renderValueBox(…);renderValueBox(…)})
renderUI({renderValueBox(…)})
都给出了“closure not subsettable”错误,但是
renderUI(c(renderValueBox(…),renderValueBox(…)
没有?回溯显示它在
106:lappy
paste8
行失败,但源代码没有帮助。因此,我假设它与
renderValueBox
是一个反应式表达式有关,这是一个闭包?
renderUI
参数可以有许多不同的形式,但我认为这里要记住的是,您正在设置一个数据结构,它将作为
输出的一部分返回。您给出的第一个示例在
{…}
中求值,然后将最后一个表达式结果提供给
renderUI
。这不是很好,因为您可能希望同时包含
renderValueBox
表达式。我认为闭包的问题在于,您传递的东西应该是反应式的或数据结构,而不是闭包。
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)
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)