RStudio - Shiny - Error “Operation not allowed without an active reactive context”

断了今生、忘了曾经 提交于 2019-12-12 22:16:47

问题


I'm just starting to get familiar with R and trying to prepare a shiny App. I just finished my code for ui.R and server.R, but I receive the following error:

Error in unique.default(x, nmax = nmax) : unique() applies only to vectors

These are my ui.R and server.R files:

ui.r

shinyUI(pageWithSidebar(
  headerPanel("Cosmo Test"),
  sidebarPanel(
    radioButtons(inputId="Dest", label="1. Favorite Holidays Destination:", selected=NULL,
                 choices=c("Rome" = 1, "New York" = 2, "Gaborone" = 3, "Arctic Pole" = 4)),
    radioButtons(inputId="Food", label="2. Favorite Food/Meal:", selected=NULL,
                 choices=c("Strawberry Pie" = 1, "Fish & Chips" = 2, "Snake B-B-Q" = 3, "sashimi" = 4)),
    radioButtons(inputId="Clothing", label="Favorite Clothing Style:", selected=NULL,
                 choices=c("Comfy" = 1, "Stylish" = 2, "Practical" = 3, "Clothing?" = 4)),
    submitButton("Submit")
  ),

  mainPanel(
    h3("Cosmo Results"),
    h4("Based on your answers, you are a..."),
    verbatimTextOutput("Result1"),
    verbatimTextOutput("ResultExplained")
  )

))

server.R

shinyServer(
  function(input,output) {
    Type<-reactive({
      as.vector(c(input$Dest,input$Food,input$Clothing))
    })
    frec.var<-(table(Type))
      valor<-which(frec.var==max(frec.var)) # Elementos con el valor m´aximo
      my_mode<-as.vector(valor)

    output$Result1<-renderText({
      if(my_mode(Type)=="1") "Romantic"
      else if (my_mode(Type)=="2") "Hypster"
      else if (my_mode(Type)=="3") "Penguin"
      else "Undefined"
    })
    output$ResultExplained<-renderText({
      if(my_mode(Type)=="1") "Love is all around you... and you love it!!"
      else if (my_mode(Type)=="2") "Grab your electric bike, your reflex cam and go make the world a fancier place"
      else if (my_mode(Type)=="3") "How exactly were you able fill this test???"
      else "You're too complex to be a Cosmo reader; are you sure you picked the right magazine?" 
    })
  })

The goal is to obtain a result based on the mode of the responses of the first part (1,2,3 or 4). I reviewed like a dozen of entries regarding unique() issues, but couldn't find anything that helped me solve the problem.

Could anyone please take a look at my code? I guess it must be something silly but I cannot find it, nor any workaround to avoid the current server.R code that works.

Best Regards, Ignacio


回答1:


First of all Type is a reactive variable it means it has to be called (Type()) to access the value, moreover it can be accessed only in a reactive context (reactive, observe, render*, isolate).

After some rewriting:

library(shiny)

shinyServer(function(input,output) {
    Type <- reactive({
        tab <- table(c(input$Dest,input$Food,input$Clothing))
        names(tab)[which.max(tab)]
    })


    output$Result1<-renderText({
        default <- "Undefined"
        r <- list("1" = "Romantic", "2" = "Hypster", "3"="Penguin")[[Type()]]
        ifelse(is.null(r), default, r)
    })

    output$ResultExplained<-renderText({
        default <- paste(
            "You're too complex to be a Cosmo reader;",
            "are you sure you picked the right magazine?"
        )
        r <- list(
            "1" = "Love is all around you... and you love it!!",
            "2" = paste(
                "Grab your electric bike, your reflex",
                "cam and go make the world a fancier place"
             ),
            "3" = "How exactly were you able fill this test???"
        )[[Type()]]
        ifelse(is.null(r), default, r)
    })
})


来源:https://stackoverflow.com/questions/31639947/rstudio-shiny-error-operation-not-allowed-without-an-active-reactive-contex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!