运行 R Shiny App 中的错误:没有活动的反应上下文不允许操作

Error in Running R Shiny App: Operation not allowed without an active reactive context

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

输出应如下所示:

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

如@Clemsang 所述,您在 oberserver/render* 函数之外使用反应值。

您想要做的是创建一个 reactive 环境,您可以在其中使用您的输入。也就是说,只要输入发生变化,就会重新计算一些东西。因此,您需要将训练计算包装在 reactive 中,当您想在渲染函数中使用它时,您可以通过添加 () 来 "call" 它。我喜欢用动词来命名我的反应式,以强调当这些函数被调用时我实际上做了一些事情,因此得名 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)
  })
}