2016-11-24 41 views
0

項目概述:要連接包含id,分數和日期的postgres數據庫,以便閃亮的儀表板用戶可以輸入id和下一個I想要計算該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) 

感謝大家的幫助。

乾杯!

+0

如果你想改變user_id生成應該是一個被動的。 – HubertL

+0

你可以刪除你不使用的r參數,也請注意你應該使用'inputId =「user_id」 – HubertL

+0

@HubertL ....嗨,我的朋友...再次感謝你的回覆...所以你是否說我應該在第34條線上產生反應? –

回答

1
  1. 我想你不應該下min

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

=>

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

設置初始值
  • 保持生成爲常規函數返回基於提供參數的東西RS
  • generate <- function(r) {

    =>

    generate <- function(user_id) {

  • 不與paste混淆sprintfprevent SQL injection
  • 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]))

    =>

    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])))

  • 添加內的反應性的所有數據manipuation該引用input參數
  • df <- as.data.frame(do.call("rbind", generate(r))) df<-reactive({df[!duplicated(df$date)]})

    =>

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

    +0

    非常感謝@Hubert ...它終於工作了..你救了我的週末:) –

    +0

    我很高興它做到了。有一個晴朗的週末! – HubertL

    +0

    謝謝休伯特....和你一樣..再次! –