【问题标题】:R shiny cannot coerce type 'closure' to vector of type 'double'R shiny 不能将类型“闭包”强制为“双”类型的向量
【发布时间】:2017-12-27 11:32:46
【问题描述】:

我试图通过插入来自selectInput 的输入来修改plotpch 参数:

selectInput("points", "Points:",
                list("Job lost" = "joblost",
                     "Sex" = "sex",                  
                ))

进入

output$Plot <- renderPlot({
    plot(as.formula(formula()),data=Benefits,
         main  = caption(), pch = as.numeric(input$points), 
         col=as.numeric(input$points))
})

不幸的是,我收到一个错误:无法将“闭包”类型强制转换为“双”类型的向量。我应该采取什么步骤来解决这个问题?当然,失业和性别都是因素。 完整代码:

library(shiny)
library(Ecdat)
attach(Benefits)

u <- shinyUI(pageWithSidebar(
  headerPanel("Social benefits"),
  sidebarPanel(

    selectInput("variable1", "Zmienna X:",
                list("Bezrobocie" = "stateur", 
                     "Max zasilek" = "statemb",
                     "Wiek" = "age",
                     "Staz w bezrobociu" = "tenure",
                     "Replacement rate" = "rr"
                )),

    selectInput("variable2", "Zmienna Y:",
                list("Bezrobocie" = "stateur", 
                     "Max zasilek" = "statemb",
                     "Wiek" = "age",
                     "Staz w bezrobociu" = "tenure",
                     "Replacement rate" = "rr"
                )),
    selectInput("points", "Punkty:",
                list("Powod utraty pracy" = "joblost",
                     "Plec" = "sex",
                     "Nie-bialy" = "nwhite",
                     ">12 lat szkoly" = "school12",
                     "Robotnik fizyczny" = "bluecol",
                     "Mieszka w miescie" = "smsa",
                     "Zonaty" = "married",
                     "Ma dzieci" = "dkids",
                     "Male dzieci" = "dykids",
                     "Glowa rodziny" = "head",
                     "Otrzymuje zasilki" = "ui"

                )),
    checkboxInput("reg", "Pokaz krzywa regresji", FALSE)


  ),
  mainPanel(
    plotOutput("Plot")
  )

))

s <- shinyServer(function(input, output) 
{


  formula <- reactive({paste(input$variable2,"~",input$variable1)})

  caption <- renderText({formula()}) 

  pkt <- reactive({input$points})

  #pkt <- renderText({paste(input$points)})

  output$Plot <- renderPlot({
    plot(as.formula(formula()),data=Benefits,
         main  = caption(), pch = as.numeric(input$points), 
         col=as.numeric(input$points))

    if(input$reg == TRUE){
      abline(lm(as.formula(formula())),col ="red", lwd = 2)
      legend("topleft",inset = 0.02, legend = "Krzywa regresji",
             col="red",lty = 1, lwd = 2)

    }
  })

})
shinyApp(u,s)

【问题讨论】:

  • 请提供完整应用的代码。此错误消息与输入变量无关。
  • input$points 不是会导致 as.numeric(input$points) 变为 NA 的数字。因此出现错误。

标签: r shiny


【解决方案1】:

问题已通过使用selectInput 中的开关解决:

 pkt <- reactive({
    switch(input$points,
           "Powod utraty pracy" = joblost,
           "Plec" = sex,
           "Nie-bialy" = nwhite,
           ">12 lat szkoly" = school12,
           "Robotnik fizyczny" = bluecol,
           "Mieszka w miescie" = smsa,
           "Zonaty" = married,
           "Ma dzieci" = dkids,
           "Male dzieci" = dykids,
           "Glowa rodziny" = head,
           "Otrzymuje zasilki" = ui)
  })

  txt <- renderText({paste(input$points)})

  output$Plot <- renderPlot({
    plot(as.formula(formula()),data=Benefits,
         main  = caption(), pch = as.numeric(pkt()), 
         col=as.numeric(pkt()))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-23
    • 2015-08-19
    • 1970-01-01
    • 2015-07-01
    • 2013-12-24
    • 1970-01-01
    • 2015-04-26
    • 2019-12-11
    相关资源
    最近更新 更多