R有光泽,找不到功能

时间:2016-09-20 13:12:44

标签: r shiny

我有一个包含2个标签的闪亮应用程序。在第二个选项卡中,我运行SQL查询以获取我想要在屏幕上发布的数据框,以便用户可以看到。为简洁起见,我只包含相关代码。基本上用户选择日期范围,这将进入数据库并提取相关信息并将该信息返回给服务器以发布到屏幕。目前,当运行应用程序时,我收到错误消息

  

错误:无法找到功能" 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)
}

1 个答案:

答案 0 :(得分:1)

eventReactiveobserveEvent

之间存在一些差异

observeEvent没有返回值的重要(我的想法)

在帮助中说:

  

每当您想要执行操作以响应时,请使用observeEvent   一个事件。 (注意,“重新计算值”通常不算作   执行操作 - 请参阅eventReactive。)

     

使用eventReactive创建仅更新的计算值   回应一个事件。

所以你可以简单地使用eventReactive

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

或创建reactiveValues并在observeEvent中进行更改(有时情况更好 - 条件不简单时)

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

然后使用report_data$data_1

相关问题