2017-12-27 141 views
0

我試圖通過插入輸入從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)) 
}) 

不幸的是,我得到一個錯誤:不能將類型'closure'強制爲'double'類型的向量。我應該採取什麼措施來解決這個問題?當然,兼職和性是因素。 全碼:

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) 
+1

請提供代碼,獲取完整的應用程序。該錯誤消息與輸入變量無關。 –

+1

'input $ points'不是一個會導致'as.numeric(input $ points)'爲'NA'的數字。因此錯誤。 – SBista

回答

0

的問題是使用一個開關在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())) 
相關問題