0
votes

I' trying to modify pch parameter of plot by inserting an input from selectInput:

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

into

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

Unfortunately, I get an error: cannot coerce type 'closure' to vector of type 'double'. What steps should I take to fix this ? Of course, both joblost and sex are factors. Full code:

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
Please supply the code for the complete app. This error message is not related to the input variable.Bertil Baron
input$points is not a number which will cause as.numeric(input$points) to be NA. Hence the error.SBista

1 Answers

0
votes

The issue was resolved by using a switch in 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()))