简体   繁体   中英

Histogram in R Shiny

I've some problem with my code in R Shiny. When I run my code, the error comes with Error:'x' must be numeric . This is my code at server.R

library(shiny)

# Define server logic required to draw a histogram
shinyServer(function(input, output) {


     output$distPlot <- renderPlot({
    malaria = input$malaria
    i = 'a'


     if(input$p=='a'){
      i<4
    }

    if(input$p=='b'){
      i<-5
    }


    postM    <-malaria[,4]

    bins <- seq(0, 100, length.out =1)
    hist(postM, breaks = bins, col = 'darkgray', border = 'white')
  })
})


For ui.R

   library(shiny)

    malaria <- read.csv("file:///C:/Users/Bella/Desktop/malaria/Malaria2/data/malaria.csv",header=FALSE)
year <- malaria$year

ui <- fluidPage(

  titlePanel("Rate Death of Malaria"),

     sidebarLayout(
    sidebarPanel(

      radioButtons("radio", label = h3("Select the sample of countries"),
                   choices = list("Bangladdesh", "China","Ethiopia","South Africa",
                                  "Indonesia","India", "Kenya","Cambodia"," Myanmmar","Malaysia","Nigeria","Somalia","Thailand"),
                   selected = "World"),

      selectInput("vertical","Show vertical line in year(s):", 
                  choices = unique(malaria$year),multiple=TRUE
      ),

      checkboxInput("hor", "Show horizontal axis", TRUE)

    ),

    mainPanel(

      plotOutput("lineChart")
    )   
  )      
)

This isn't so much an Shiny problem as it is a formatting issue for your data-type. Try running:

typeof(postM)

It's likely it will return "character" and the hist() function prefers numbers ("double" would be a good variable type for the histogram input)

If it's simply a matter of formatting the .csv data, you can try:

hist(as.numeric(postM))

or:

hist(as.numeric(unlist(postM)))

However, it's important to make sure that your data should be converted into a numeric value, rather than coerce the format before taking a look at the source data.

Hope that helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM