John Smith John Smith - 4 months ago 31
R Question

R shiny, cannot find the function

I have a shiny application that contains 2 tabs. In the second tab i run an SQL query to get back a data-frame which i want to post on screen so the user can see. For brevity i have only included the relevant code. Basically the user selects a date range, this goes to the database and pulls the relevant information and returns that information to the server to post to the screen. Currently when run the app i get the error message

Error: could not find function "report_data"

I would be grateful for any help you could provide

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

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

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


# 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, {

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

This function is the function that goes to the database and returns the dataframe based on the users selections of a date range.

# 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)
names(data) <- c("User", "records")



There some differences between eventReactive and observeEvent

One of the important (my mind ) that observeEvent not return values

as told in help :

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.

So you can simply use eventReactive

report_data <- eventReactive(input$search, {

Or create reactiveValues and change it in observeEvent ( sometimes it better -- when condition not simple )

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

and then use report_data$data_1