闪亮错误:与数据库连接时未读取数据

时间:2016-11-24 19:57:00

标签: r shiny

项目概述:连接包含id,分数和日期的postgres数据库,以便闪亮的仪表板用户可以输入id,然后我想计算一个上下该id的均值(分数)范围,以便最终我可以在图表中显示所有内容。

问题:看起来没有数据从数据库中读取,错误:未找到分数

以下是剧本:

library(ALL relevant libraries)

drv <- dbDriver("PostgreSQL")

con<-dbConnect(drv,dbname = "", host = "", port = "", user = "", password= "")
# correct credentials placed above

dates <- seq(as.Date(as.character(Sys.Date() - 10)), as.Date(as.character(Sys.Date() - 5)), by = 1)
# dates vector should have 5 days 

r <<- length(dates)


ui <- fluidPage(

titlePanel("Score"),

sidebarPanel(numericInput(inputId = "id",label = "user", value = 0000 ,
                        min = 100, max = 1000000)),

mainPanel(plotOutput("Coolplot"))
)


server <- function(input, output, session) {
  browser()

  generate <- function(r) {

 listofdfs <- list() # Create a list in which you intend to save your df's. 

 for (i in 1:length(dates)) {

  data <- dbGetQuery(con, sprintf("select score, CAST (date AS date), id from My_Table 
                                         where id = ",input$id," and
                                date<=date('%s') and date>=date('%s')- INTERVAL '7 day'",dates[i],dates[i])) 

    # changed the date<='%s' to date<=date('%s') now at least can read the data. 


   data$score_mean <- mean(data[,1])

   data$upper_threshold <- data$score_mean * 1.2

   data$lower_threshold <- data$score_mean * 0.8

  listofdfs[[i]] <- data # save your dataframes into the list
  }

  return(listofdfs) #Return the list of dataframes.
}


 df <- as.data.frame(do.call("rbind", generate(r)))
 df<-reactive({df[!duplicated(df$date)]}) #since data needed some subsetting




output$Coolplot <- renderPlot({

    browser()
    ggplot(df(), aes(date)) +
    geom_line(aes(y = score, colour = "score"))+
    geom_line(aes(y = upper_threshold, colour = "upper_threshold")) +
    geom_line(aes(y = lower_threshold, colour = "lower_threshold"))
})
}


 shinyApp(ui = ui, server = server) 

谢谢大家的帮助。

干杯!

1 个答案:

答案 0 :(得分:1)

  1. 我认为您不应在min
  2. 下设置初始值

    sidebarPanel(numericInput(inputId = "id",label = "user", value = 0000 , min = 100, max = 1000000)),

    =&GT;

    sidebarPanel(numericInput(inputId = "id",label = "user", value = 100 , min = 100, max = 1000000)),

    1. 保持生成为常规函数,根据提供的参数返回内容
    2. generate <- function(r) {

      =&GT;

      generate <- function(user_id) {

      1. 不要将sprintfpasteprevent SQL injection
      2. 混淆

        sprintf("select score, CAST (date AS date), id from My_Table where id = ",input$id," and date<=date('%s') and date>=date('%s')- INTERVAL '7 day'",dates[i],dates[i]))

        =&GT;

        sprintf("select score, CAST (date AS date), id from My_Table where id = %d and date<=date('%s') and date>=(date('%s')- INTERVAL '7 day')", as.numeric(user_id), as.date(dates[i]), as.date(dates[i])))

        1. 在引用input参数
        2. 的被动反应中添加所有数据操作

          df <- as.data.frame(do.call("rbind", generate(r))) df<-reactive({df[!duplicated(df$date)]})

          =&GT;

          df<-reactive({ data <- as.data.frame(do.call("rbind", generate(input$id))) data[!duplicated(data$date)] })