0
votes

This is my code below, I think it may just be a silly mistake but I have spent the last 5 days trying to find where I went wrong in my app:-

ui <- fluidPage(h1("Left Ventricular Hypertrophy"),
                titlePanel("Regression Model"),
                sidebarLayout(
                sidebarPanel(

                  h3("Marginal Histogram"),
                  selectInput("marhis1", "X:", choices = unique(colnames(lvh))),
                  selectInput("marhis2", "Y:", choices = unique(colnames(lvh)), selected = "sbp"),
                  sliderInput("bin", "Bin Width:", min = 1, max = 100, value = 10),
                  h3("Box Plot"),
                  selectInput("Variable4", "llvmi or sbp:", choices = c("llvmi", "sbp")),
                  selectInput("Variable5", "Grouped by:", choices = c("sex", "card", "dgp", "surv")),

                  mainPanel(
                            h3("Marginal Histogram"),
                            plotOutput("hist"),
                            br(),
                            h3("Boxplot"),
                            plotOutput("Box")
                            )

                  )
)

server <- function(input, output) {

  lvh <- read.table('lvh.dat.txt', header = T)



  output$hist <- renderPlot({
  marg <- ggplot(lvh, aes(input$marhis1, input$marhis2) + geom_point() + theme_light() +
  xlab(input$marhis1) + ylab(input$marhis2))
  ggMarginal(marg, input$marhis1, input$marhis2, type = "histogram", binwidth = input$bin)
})


  output$Box <- renderPlot({
    choice2 <- data.frame(x=lvh[input$Variable4], y=lvh[input$Variable5])
    ggplot(choice2, aes(lvh[input$Variable4], lvh[input$Variable5]) + geom_boxplot() + theme_light() +
            xlab(input$Variable4) + ylab(input$Variable5))
  })

}


shinyApp(ui = ui, server = server)

I keep getting an error of non-numeric argument to binary operator in both of the plots when I run app, could anyone help

1
This is probably too much code to wade through for anyone wanting to help. Try narrowing the problem down to something more specific. For instance, your error probably has nothing really to do with Shiny. In particular, aes(as.character(input$marhis1), as.character(input$marhis2)) looks rather wrong to me.joran
Yeah i removed this, I will move the code that is causing the issue over into a smaller versionAaron Andrew Macintyre
Is that better? Its really just the plots that the issues comes up with, also thanks for pointing out about code being too long, first postAaron Andrew Macintyre
It may be trying to pull input before they're selected. You can try adding req() for each input within the relevant renderPlot() functions OR have default selections for the input OR both.Ryan Morton
Put up a working example that can reproduce the error. I am unable to reproduce your erroruser5249203

1 Answers

2
votes

First, let me encourage you to always provide code samples that are easy to inspect. Since, from your comment conversation in the original post, one can see that you did not quite get what that means: Make the code snippet so that simple copy + paste will be enough to run into the errors you are facing.

At least four people looked at your code and everyone was immediately discouraged to invest some patience into it.

Otherwise it would have been easy to detect, that you are just missing a parenthesis in the part

ggplot(lvh, aes(input$marhis1, input$marhis2) + geom_point()

where it must be

ggplot(lvh, aes(input$marhis1, input$marhis2)) + geom_point()

(of course eliminating the also misplaced original closing bracket).

Edit: Same goes for the second call to ggplot. More on that: ggplot works by adding layers to the plot. That is why you have to add (+) the ggplot(...) generated elements. Not adding options inside the call to ggplot.