问题
Working with a Shiny Dashboard, ui.r, server.r and several r scripts that contain functions.
Base problem is: I have two data sets that I bring into the server.r and I pass those to a function the prepares/cleanses the data sets and then binds them together and should return a complete dataframe. The dataFrame should be reactive. Here is what I have so far:
In the server.r I load the data prior to the function(input, output, session). Then I have:
################server.r code #########################
data <- reactive({
testDF <- prepData(data1, data2)
})
The prepData function does a variety of things but ends with:
####################prepData function return#################
return(rbind(data1, data2))
If I use something like:
############## server.r code#######################
value = nrow(data()),
Then the code returns the correct value. However I would prefer to just return the data frame as I did with testDF.
Shiny will throw and error in the UI of object 'testDF' not found.
I tried working through: How do I build a reactive dataframe in R / Shiny? using the code:
dataR <- prepData(data1, data2)
makeReactiveBinding(dataR)
This still throws the error. The function is clearly working and has been validated but there must be something I don't understand about the reactive component of utilizing this function. Any help would be appreciated. Thanks!
server.ui
function(input, output, session) {
### PreProcess the Data
data <- reactive({
testDF <- prepData(bdata, qdata)
})
#dataR <- prepData(bData, qData)
#makeReactiveBinding(dataR)
### Information Box Populations
output$monthlytransactions = renderInfoBox({
infoBox(
title = "Payments",
value = nrow(data()),
icon = icon("comments-dollar"),
color = "blue"
)
})
output$monthlyGrossDollars = renderInfoBox({
infoBox(
title = "Payments",
value = sum(testDF$GrossAmount),
icon = icon("comments-dollar"),
color = "blue"
)
})
}
prepData
############# FUNCTIONS ##############
prepData <- function(beamData, qlawData){
##Processing##
#Join DFs
return(rbind(bData, qData))
回答1:
You can think of reactive expressions as being functions that return a value. So you're getting the object 'testDF' not found
error because testDF
only exists within the scope of the data
reactive object (function).
When you create a reactive expression:
data <- reactive({
testDF <- prepData(bdata, qdata)
})
what you're actually doing is creating a function that returns a value. In this case, you run prepData()
, save the result in the object testDF
and then implicitly return that value (since R implicitly returns the value of the last line of a function). You access that value by calling data()
, since that's the name of the reactive expression that returns your value, not testDF
, since that object is deleted once the function has finished running.
If you want testDF
to contain the result of prepData
, just change the name of the reactive expression like so:
testDF <- reactive({
prepData(bdata, qdata)
})
testDF() # this will return the value you want
Calling the reactive expression does actually return a data frame which you can subset and work with as normal. So you can work with it just like a data frame (but don't forget the parentheses!):
data()$GrossAmount
data()[data()$GrossAmount > 100, c('name', 'date')]
来源:https://stackoverflow.com/questions/53876948/returning-a-dataframe-in-a-reactive-function-in-r-shiny-dashboard