RStudio - Shiny - 错误“没有活动的反应上下文时不允许操作”

时间:2015-07-26 17:33:16

标签: r unique rstudio shiny

我刚刚开始熟悉R并试图准备一个闪亮的应用程序。我刚刚完成了ui.Rserver.R的代码,但收到以下错误:

  

unique.default(x,nmax = nmax)中的错误:unique()仅适用于   载体

这些是我的ui.Rserver.R文件:

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?" 
    })
  })

目标是根据第一部分(1,2,3或4)的响应模式获得结果。 我回顾了十几个关于unique()问题的条目,但找不到任何有助于我解决问题的内容。

有人可以看看我的代码吗?我想它一定是愚蠢但我无法找到它,也没有任何解决办法来避免使用当前的server.R代码。

最诚挚的问候, 伊格纳西奥

1 个答案:

答案 0 :(得分:2)

首先Type是一个反应变量,它意味着必须调用它(Type())来访问该值,而且只能在被动上下文中访问它reactiveobserverender*isolate)。

经过一些改写:

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)
    })
})