运行R Shiny App时出错:没有活动的响应上下文,不允许进行操作

时间:2019-04-05 08:36:22

标签: r shiny shiny-server shiny-reactivity

我正在使用Shiny和R交互地可视化我的数据。我想在Iris数据集中绘制Petal.Width与Petal.Length的交互式散点图,并基于k个聚类(用户输入)和p(专用于训练数据集的数据行的百分比)(用户输入)对点进行聚类。我在散点图上添加了悬停功能,以便通过单击每个点,可以演示该点的整个数据集。

输出应如下所示: enter image description here

# Loading Libraries
library(shiny)
library(caret)
library(ggplot2)
data(iris)


ui <- pageWithSidebar(
  headerPanel("Clustering iris Data"),

  sidebarPanel(
    sliderInput("k", "Number of clusters:",
                min = 1, max = 5,  value = 3),

    sliderInput("prob", "Training percentage:",
                min=0.5, max=0.9, value = 0.7)),

  mainPanel(
  # img(src='iris_types.jpg', align = "center", height="50%", width="50%"),

  plotOutput("plot1", click = "plot_click"),
  verbatimTextOutput("info")
  )
)


server <- function(input, output) {

  inTrain  <- createDataPartition(y=iris$Species, 
                                  p=input$prob, 
                                  list=FALSE)
  training <- iris[ inTrain,]
  testing  <- iris[-inTrain,]

  kMeans1 <- kmeans(subset(training,
                           select=-c(Species)),
                           centers=input$k)

  training$clusters <- as.factor(kMeans1$cluster)

  output$plot1 <- renderPlot({
    qplot(Petal.Width,
          Petal.Length,

          colour = clusters,
          data   = training,

          xlab="Petal Width",
          ylab="Petal Length")
  })

  output$info <- renderPrint({
    # With ggplot2, no need to tell it what the x and y variables are.
    # threshold: set max distance, in pixels
    # maxpoints: maximum number of rows to return
    # addDist: add column with distance, in pixels
    nearPoints(iris, input$plot_click, threshold = 10, maxpoints = 1,
               addDist = FALSE)
  })
}

shinyApp(ui, server)

在R Studio中运行应用程序时,出现以下错误:

Error in .getReactiveEnvironment()$currentContext() : 
  Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)

1 个答案:

答案 0 :(得分:1)

如@Clemsang所述,您在oberserver/render*函数外部使用电抗值。

您想要做的是创建一个reactive环境,您可以在其中使用输入。也就是说,每当输入更改时,都会重新计算某些内容。因此,您需要将训练计算包装在reactive中,并且要在渲染函数中使用它时,可以通过添加()来“调用”它。我喜欢用动词来命名我的反应堆,以强调这样一个事实,即我在调用这些函数时实际上在做某事,因此命名为get_training_data

server <- function(input, output) {

  get_training_data <- reactive({ ## now your inputs are in a reactive environment
     inTrain  <- createDataPartition(y=iris$Species, 
                                     p=input$prob, 
                                     list=FALSE)
     training <- iris[ inTrain,]
     testing  <- iris[-inTrain,]

     kMeans1 <- kmeans(subset(training,
                              select=-c(Species)),
                       centers=input$k)

     training$clusters <- as.factor(kMeans1$cluster)
     training
  })

  output$plot1 <- renderPlot({
    qplot(Petal.Width,
          Petal.Length,

          colour = clusters,
          data   = get_training_data(),

          xlab="Petal Width",
          ylab="Petal Length")
  })

  output$info <- renderPrint({
    # With ggplot2, no need to tell it what the x and y variables are.
    # threshold: set max distance, in pixels
    # maxpoints: maximum number of rows to return
    # addDist: add column with distance, in pixels
    nearPoints(iris, input$plot_click, threshold = 10, maxpoints = 1,
               addDist = FALSE)
  })
}