从 RShiny 上的日期范围输入制作反应直方图

make a reactive histogram from daterange input on RShiny

这是我的第一个 R Shiny project.and 作为背景我尝试制作直方图 来自我从我的数据库中获取的数据并使用日期作为条件。

这是我的代码

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)

这是我的数据

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

当我 运行 应用

时总是会出现此错误

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

在绘制直方图之前是否需要将我的厚度数据解析为 int?

如果没有您的数据,很难想出一个工作版本,但您似乎只需要提取厚度值,因为您无法创建数据框的直方图:

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

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