1

I've been trying to create custom graphs in my Shiny Project, but there is error occuring :

> Warning in model.response(mf, "numeric") : NAs introduced by coercion
> Error in contrasts<-(`*tmp*`, value = contr.funs[1 + isOF[nn]]) :   
>     contrasts can be applied only to factors with 2 or more levels

Here is the code :

observeEvent(input$create_cutom_graph, {
  output$cutom_graph <- reactive(renderPlot(
    plot(input$graph_X,input$graph_Y),
    abline(lm(input$graph_X~input$graph_Y)),
    title(input$graph_X,"i",input$graph_Y)
  ))
 }
))

The way it is supposed to work is that you choose from the dropdown menu which data should be on the X axis and then you do the same with Y axis, then you click the button "Create" and it does the trick but somehow it doesnt. And i have to also stress that I've tried to apply function na.omit before the data like : na.omit(input$graph_X) still it does not do the trick.

Thanks anyways for your help!

Henrique Barcelos
  • 7,670
  • 1
  • 41
  • 66
Johny Dern
  • 11
  • 1

1 Answers1

0

If I understand you correctly the user is supposed to choose the name of a column in the data imported. The problem here is that the input arguments from users are of a character class. When you call the plot function the name of the desired column in the data is put inside the function call as a character vector of length 1. It does not know how to handle such arguments. The same holds for the lm function. To solve this, you can for example subset the data using data[,'someCharacter'].

Next time you post a question it would be helpful to include a reproducible example, see here. I created a reproducible dummy Shiny App to demonstrate the Answer. To run this app successfully you need to save this file with the name "app.R" and be sure that the working directory includes the file.

If you want your plotOutput to depend on some event from the user, that is you want the code to run when the user e.g. clicks a button, I recommend using eventReactive instead of observeEvent.

ui <- shinyUI(fluidPage(



 titlePanel("Plot columns in a dataset"),
    fluidRow(
        column(4,
               selectInput("graph_X", "Select column 1", choices=c("One","Two","Three")),
               selectInput("graph_Y", "Select column 2", choices=c("One","Two","Three"),selected="Two"),
               actionButton("create_custom_graph","Plot")
        ),
        column(8,
               plotOutput("plot")
        )

    )
    ))

    server <- function(input,output){
        makePlot<- eventReactive(input$create_custom_graph,{

        #make some dummy data
        data=data.frame(One=c(1,2,3,4,5),Two=c(1,2,3,4,5),Three=c(4,5,6,7,8))
        col1=data[,input$graph_X]
        col2=data[,input$graph_Y]

        #evaluate the characters
        plot(col1,col2)
        abline(lm(col1~col2))
        title(paste(input$graph_X,"i",input$graph_Y))
    })
    output$plot <- renderPlot({
        makePlot()
    })
}

shinyApp(ui,server)

Hope this helps!

Community
  • 1
  • 1
Sölvi
  • 500
  • 5
  • 17