-2

I've tried everything to define the new columne before adding it to the reactive formula, it still throws me an error. It worked fine when before I put into the shiny format

#library(shiny)
ui <- fluidPage( "PROJECT REKT ( CDI TOOL)" , selectInput( inputId ="State" , label = "Choose your client's state",  c( "AK","AL","AZ",
    "AR", "CA", "CO", "CT", "DE", "FL", "GA",
    "HI", "ID", "IL", "IN", "IA", "KS", "KY",
    "LA", "ME", "MD", "MA", "MI", "MN", "MS",
    "MO", "MT", "NE", "NV", "NH", "NJ", "NM",
    "NY", 'NC', 'ND', 'OH', 'OK', 'OR', "PA",
    "RI", "SC", "SD", "TN", "TX", "UT", "VT",
    "VA", "WA", "WV", "WI", "WY"), multiple = FALSE, selectize=TRUE , width = NULL , size = NULL)
)

server <- function(input, output) {
   data2 = renderTable(National_data[National_data$state == input$State,])
   #data2$captureRate = NULL

   # Summarize the data by each acuity group ( like aa pivot table), uset SetNames to rename columns

   #data3 = cast(data2 , num_is_wo + num_is_cc + num_is_mcc ~ acuity_group, mean)
   data4 = reactive(
       setNames (aggregate(list(data2()$num_is_cc , data2()$num_is_mcc, data2()$num_is_wo), by=list(data2()$acuity_group), FUN=sum, na.rm=TRUE) , c ("acuity_group","cc","mcc", "wo"))
   )

   # Find the capture rate for each row element
   data4$captureRate = NULL
   data4$captureRate = reactive(
     (data4()$cc + data4()$mcc) / (data4()$wo + data4()$cc + data4()$mcc)
   )

   State_BM_CaptureRate = reactive (
     mean(data4()$captureRate, na.rm = TRUE)
   )

   Output$bencmark = State_BM_CaptureRate
 }

shinyApp(ui = ui, server = server)

Which results in

Listening on http://127.0.0.1:7136
Warning: Error in =: object of type 'closure' is not subsettable
Stack trace (innermost first):
    45: server [#16]
     4: <Anonymous>
     3: do.call
     2: print.shiny.appobj
     1: <Promise>
Error in data4$captureRate = NULL : 
  object of type 'closure' is not subsettable
MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • 5
    Welcome to SO. Please visit https://stackoverflow.com/help to better frame your question so others can assist properly. In your above example, by looking at the error message you are receiving, you have to understand that a `reactive` variable is accessed as a `function`. So, try `output$bencmark = State_BM_CaptureRate()`. – Sagar Aug 30 '17 at 19:47
  • You can also refer to https://stackoverflow.com/q/40623749/8382207 to get more information on this error. – Sagar Aug 30 '17 at 19:48

1 Answers1

0

It is hard to assume without any sample of your data, but at the first look i think the problem is in this part:

  data4$captureRate = reactive(
         (data4()$cc + data4()$mcc) / (data4()$wo + data4()$cc + data4()$mcc)
       )

Your data4 is at this point a reactive object! Thereofre you cannot use data4$captureRate! How you can solve it:

data4 <- reactive({
  data <- setNames (aggregate(list(data2()$num_is_cc,data2()$num_is_mcc, data2()$num_is_wo), by=list(data2()$acuity_group), FUN=sum, na.rm=TRUE) , c ("acuity_group","cc","mcc", "wo"))
  data$captureRate <- (data$cc + data$mcc) / (data$wo + data$cc + data$mcc)
  data})
Mal_a
  • 3,670
  • 1
  • 27
  • 60
  • Hi Mal, Thanks for your answer. I had a follow up question. The code that you wrote does not add another column to the data set. What if I want to add a new column to the reactive object. data4 has just 4 columns, I want to calculate *and* add a fifth column to that data set which happens to be reactive – Jack the Cripple Aug 31 '17 at 19:49
  • Are you talking about the column called: captureRate or other one? – Mal_a Aug 31 '17 at 19:51