RStudio - Shiny - 错误“没有活动的反应性上下文不允许操作"; [英] RStudio - Shiny - Error "Operation not allowed without an active reactive context"

查看:58
本文介绍了RStudio - Shiny - 错误“没有活动的反应性上下文不允许操作";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

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

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

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

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

目标是根据第一部分的响应模式(1、2、3 或 4)获得结果.我查看了十几篇关于 unique() 问题的条目,但找不到任何可以帮助我解决问题的条目.

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.

有人可以看看我的代码吗?我想这一定是愚蠢的,但我找不到它,也没有任何解决方法可以避免当前有效的 server.R 代码.

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

推荐答案

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

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

经过一些重写:

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

这篇关于RStudio - Shiny - 错误“没有活动的反应性上下文不允许操作";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆