2016-09-20 89 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