0

this is my first R Shiny project.and as background I try to make a histogram from data that I take from my Database and use date as it's condition.

this is my code

App.R

library(shiny)
library(RJDBC)

# Define UI for application that draws a histogram
ui <- fluidPage(

   # Application title
   titlePanel("Test Project"),

   # Sidebar with a slider input for number of bins 
   sidebarLayout(
      sidebarPanel(
        dateRangeInput('dateRange',
                       label = 'Molding Date',
                       start = Sys.Date() - 7, end = Sys.Date()
        )
      ),

      # Show a plot of the generated distribution
      mainPanel(
         plotOutput("distPlot")
      )
   )
)

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

  drv <- JDBC("com.microsoft.sqlserver.jdbc.SQLServerDriver" , "C:/Microsoft JDBC Driver 6.4 for SQL Server/sqljdbc_6.4/enu/mssql-jdbc-6.4.0.jre8.jar" ,identifier.quote="`")

  con <- dbConnect(drv, "jdbc:sqlserver://localhost;databaseName=db1", "user1", "pass", DBMSencoding = "UTF-8")

  q1 <- dbGetQuery(con, "SELECT molding_date,thickness FROM table1")

     output$distPlot <- renderPlot({
      x    <- q1

      # draw the histogram
      hist(x[as.Date(x$molding_date,"%m/%d/%Y") >= min(input$dateRange) & as.Date(x$molding_date,"%m/%d/%Y") <= max(input$dateRange),], col = 'darkgray', border = 'white')
   })
}

# Run the application 
shinyApp(ui = ui, server = server)

and here is my data

molding_date thickness
2018/07/06 10
2018/07/07 20 2018/07/08 10
2018/07/09 9.7
2018/07/09 10

this error always come up when I run the app

Warning: Error in hist.default: 'x' must be numeric

Do I need to parse my thickness data into int before I drawing the histogram?

oRoberto
  • 165
  • 1
  • 11

1 Answers1

0

It is hard to come up with a working version without having your data but it seems you just need to extract the thickness values because you can't create a histogram of data frame:

x[as.Date(x$molding_date,"%m/%d/%Y") >= min(input$dateRange) & as.Date(x$molding_date,"%m/%d/%Y") <= max(input$dateRange), 'thickness']

or

library(dplyr)

x %>%
  filter(between(molding_date, min(input$dateRange), max(input$dateRange))) %>%
  select(thickness)

# try to wrap the dates in 'as.Date' like your example if it does not work
OzanStats
  • 2,756
  • 1
  • 13
  • 26
  • thank you for your reply. first I change my code into "hist(x[as.Date("molding_date","%m/%d/%Y") >= min(input$dateRange) & as.Date("molding_date","%m/%d/%Y") <= max(input$dateRange), "thickness"], col = 'darkgray', border = 'white')" and it give me "Error in hist.default: invalid number of 'breaks'" error, while using dplyr like on your second suggestion give me "Evaluation error: Not compatible with requested type: [type=character; target=double]" error. – oRoberto Jul 30 '18 at 02:50
  • Could you please make your question [reproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) by sharing a minimal dataset? – OzanStats Jul 30 '18 at 02:54
  • hi, I'm sorry for my late response. after try and tweak my code a little bit, the problem was because I put the wrong data column into my molding_date variable. after I fix it, the code you gave me work like a magic! thank you! – oRoberto Aug 09 '18 at 00:05
  • Sure, glad that it was helpful – OzanStats Aug 09 '18 at 00:33