【问题标题】:RStudio - Shiny - Error "Operation not allowed without an active reactive context"RStudio - Shiny - 错误“在没有活动反应上下文的情况下不允许操作”
【发布时间】:2015-07-26 17:33:16
【问题描述】:

我刚刚开始熟悉 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 代码有效。

最好的问候, 伊格纳西奥

【问题讨论】:

    标签: r unique rstudio shiny


    【解决方案1】:

    首先Type是一个反应变量,这意味着它必须被调用(Type())才能访问该值,而且它只能在反应上下文中访问(reactiveobserve,@ 987654325@, 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)
        })
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-26
      • 2013-09-29
      • 2016-04-17
      • 1970-01-01
      • 2019-01-10
      • 2019-08-27
      • 2023-03-12
      • 2012-02-28
      相关资源
      最近更新 更多