【问题标题】:Error in Running R Shiny App: Operation not allowed without an active reactive context运行 R Shiny App 时出错:如果没有活动的反应上下文,则不允许操作
【发布时间】:2019-08-27 02:53:51
【问题描述】:

我正在使用 Shiny 和 R 以交互方式可视化我的数据。我想在 Iris 数据集中绘制 Petal.Width 与 Petal.Length 的交互式散点图,并根据 k 个集群(用户输入)和 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.)

【问题讨论】:

  • input$prob 是一个反应值。您必须将数据包装在响应式内容中。例如reactiveValues()
  • @Clemsang 谢谢。你能再解释一下吗?哪一行需要改?
  • inTraintrainingtesting 是动态的。您可以为observe 中的每个人创建reactiveValues。这样,您可以在输入更改时修改 3 个对象。当您可以使用数据时,您还必须在服务器中使用reactiveValues$object_name 更改对这三个对象的调用。仔细看reactiveValues()

标签: r shiny shiny-server shiny-reactivity


【解决方案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)
  })
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-26
    • 2019-01-10
    • 1970-01-01
    • 2013-09-29
    • 2016-04-17
    • 1970-01-01
    • 2019-01-31
    • 1970-01-01
    相关资源
    最近更新 更多