2016-09-20 63 views
0

我有一個閃亮的應用程序,其中包含2個選項卡。在第二個選項卡中,我運行SQL查詢來獲取我想要在屏幕上發佈的數據幀,以便用戶可以看到。爲了簡潔起見,我只包含了相關的代碼。基本上,用戶選擇一個日期範圍,這將轉到數據庫並提取相關信息並將該信息返回到服務器以發佈到屏幕上。運行應用程序現在,當我收到錯誤消息R閃亮,找不到功能

Error: could not find function "report_data"

我會感謝任何幫助您可以提供

#----------------------------------------------------------------------------------------- 
# UI  
# TAB 2 which lets the user select a date range and press the submit button 
#----------------------------------------------------------------------------------------- 
tabPanel("Review Uploaded Data", 
        # Side Panel with Options 
        fluidRow(
        column(4, wellPanel(
         id = "leftPanel", 

         div(id = "Header", 
          h3("Options", align = "center"), 
          tags$hr() 
        ), 

         div(id = "form2", 

          dateRangeInput("dates", label = h3("Entry Date Range")), 
          actionButton("search", "Search Database", class = "btn-primary") 

        ) 
        )), 

        column(8, id = "reporttable", 
          # Main Panel shows the uploaded excel document when a user first uploads 
          DT::dataTableOutput("reportTable") 

        ))) 


#----------------------------------------------------------------------------------------- 
# Server  
# TAB 2 Review Uploaded Data 
#----------------------------------------------------------------------------------------- 


# When User selects a date Range. Run the SQL to pull information for that Date Range 
report_data <- observeEvent(input$search, { 
    load_data(input$dates) 
}) 

# Show the summary table 
output$reportTable <- DT::renderDataTable(
    DT::datatable(
    report_data(), 
    rownames = TRUE, 
    options = list(searching = FALSE, lengthChange = FALSE, scrollX = FALSE) 
)) 

此功能是數據幀基礎上進入到數據庫,並返回功能用戶選擇日期範圍。

# Load the data from the MYSQL table 
load_data <- function(dateRange) { 
    # Connect to the database 
    db <- dbConnect(MySQL(), dbname = databaseName, host = options()$mysql$host, 
       port = options()$mysql$port, user = options()$mysql$user, 
       password = options()$mysql$password) 


    start_date <- dateRange[1] 
    end_date <- dateRange[2] 

    # Construct the fetching query 
    query <- sprintf("SELECT USER, COUNT(*) as records FROM %s 
WHERE ENTRY_DATE BETWEEN '%s' AND '%s' GROUP BY 1", table, start_date,  end_date) 

    # Submit the fetch query and disconnect 
    data <- dbGetQuery(db, query) 
    dbDisconnect(db) 
    names(data) <- c("User", "records") 

    return(data) 
} 
+4

你需要'eventReactive',而不是'observeEvent'因爲observeEvent - 什麼也沒回 – Batanichek

+0

非常感謝你,我在這個年紀裏撓了撓頭。請把它作爲答案,我會立即標記它 –

回答

1

有作爲的幫助告訴eventReactiveobserveEvent

其中一個重要的(我的心)的那observeEvent沒有返回值

之間的一些差異:

Use observeEvent whenever you want to perform an action in response to an event. (Note that "recalculate a value" does not generally count as performing an action–see eventReactive for that.)

Use eventReactive to create a calculated value that only updates in response to an event.

這樣你就可以只需使用eventReactive

report_data <- eventReactive(input$search, { 
    load_data(input$dates) 
}) 

或者創建reactiveValuesobserveEvent改變它(更好有時它 - 當條件並不簡單)

report_data <- reactiveValues(data_1=NULL) 
observeEvent(input$search, { 
    report_data$data_1<-load_data(input$dates) 
}) 

然後用report_data$data_1