項目概述:要連接包含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)
感謝大家的幫助。
乾杯!
如果你想改變user_id生成應該是一個被動的。 – HubertL
你可以刪除你不使用的r參數,也請注意你應該使用'inputId =「user_id」 – HubertL
@HubertL ....嗨,我的朋友...再次感謝你的回覆...所以你是否說我應該在第34條線上產生反應? –