Home > Mobile >  Warning: Error in <=: comparison (4) is possible only for atomic and list types
Warning: Error in <=: comparison (4) is possible only for atomic and list types

Time:02-17

I have this sample app: User should pick a numer of row from which a random value will be generated and output is a plot of values.

I would like to compare input with the row number but I get this error:

Warning: Error in <=: comparison (4) is possible only for atomic and list types
  48: ifelse


library(shiny)


ui <- fluidPage(

    
    titlePanel("Random numbers"),

  
    sidebarLayout(
        sidebarPanel(
            sliderInput("row",
                        "Row number:",
                        min = 1,
                        max = 50,
                        value = 30)
        ),

     
        mainPanel(
           plotOutput("rowPlot")
        )
    )
)


server <- function(input, output) {

  
  rowTable <- data.frame(rowNumber = as.integer(), value = as.integer())
  rowTable[1:50, ] <- NA
  rowTable[["rowNumber"]] <- 1:50
  rowTable[["value"]] <- ifelse(rowTable[["rowNumber"]] <= reactive({input$row}), 0, rnorm(50))
  
  
    output$rowPlot <- renderPlot({
       
        plot(rowTable[["rowNumber"]], rowTable[["value"]])
    })
}


shinyApp(ui = ui, server = server)

CodePudding user response:

Create the data.frame inside the reactive

server <- function(input, output) {
  
  
  
  dat <- reactive({
    
    rowTable <- data.frame(rowNumber = as.integer(), value = as.integer())
    rowTable[1:50, ] <- NA
    rowTable[["rowNumber"]] <- 1:50
    
    rowTable[["value"]] <- ifelse(rowTable[["rowNumber"]] <= as.numeric(input$row), 0, rnorm(50))
    rowTable
   })
  
  
  output$rowPlot <- renderPlot({
    tmp <- dat()
    plot(tmp[["rowNumber"]], tmp[["value"]], xlab = "rowNumber", ylab = "value")
  })
  })
}

-testing

shinyApp(ui = ui, server = server)

-output

enter image description here

  • Related