【问题标题】:Code for a reactive table to plot in Shiny在 Shiny 中绘制反应表的代码
【发布时间】:2019-04-05 16:19:17
【问题描述】:

我是闪亮的新手,我正在尝试做一个简单的密度图,其中有 2 组数据,均值中的反应性“变化”等。 对此的简化总结是,一组数据的平均值为 0,方差为 1。第二组数据的平均值为 shift,在滑块中定义。

我曾尝试使用reactiveValues,如下面的代码所示来存储观察矩阵d1,由density函数y值生成,对应的x值存储在x中。


library(shiny)

# Define UI for application that draws a histogram
ui <- fluidPage(

   # Application title
   titlePanel("Old Faithful Geyser Data"),

   # Sidebar with a slider input for number of bins 
   sidebarLayout(
      sidebarPanel(
         sliderInput("shift",
                     "shift of 2nd set",
                     min = -1,
                     max = 1,
                     value = 0)
      ),

      # Show a plot of the generated distribution
      mainPanel(
         plotOutput("distPlot")
      )
   )
)

# Define server logic required to draw a histogram
server <- function(input, output) {
   data <- reactiveValues({

     d1 <- matrix(nrow=100, ncol=512)
     for(i in 1:70){
       d1[i,] <- density(rnorm(1000),from = -3, to = 3)$y
     }
     for(i in 71:100){
       d1[i,] <- density(rnorm(1000, input$shift),from = -3, to = 3)$y
     }
     x <- density(rnorm(1000),from = -3, to = 3)$x

   })
   output$distPlot <- renderPlot({
      matplot(data$x, t(data$d1), type = "l", lty = 1, col = c(rep(1,70),rep(2,30)))
   })
}

# Run the application 
shinyApp(ui = ui, server = server)

以上代码主要来自示例闪亮应用程序,因此请原谅任何通用引用。它应该仍然有效。

我期待一个闪亮的图,左边有一个滑块,右边有一个有 100 条密度线的 2 种颜色的图。当shift 滑块更改时,第二组数据(红色)将根据移位向左或向右滑动。

相反,我收到了错误消息

  55: stop
  54: .getReactiveEnvironment()$currentContext
  53: .subset2(x, "impl")$get
  52: $.reactivevalues
  47: server [/beavis/Documents/test/app.R#37]
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.)

有人能帮我修复这个代码吗?任何帮助将不胜感激。玩了一个小时后,我相信问题出在reactiveValues 部分,但到目前为止没有任何效果。

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    不错的尝试。你很接近。你缺少的是两件事。您正在创建一个 data 对象,该对象变为反应式(我使用 reactive 代替)。这意味着无论何时您调用data,您都需要将其称为data()

    其次,您的data 仅返回x。在您的情况下,此反应式环境的输出应该是 xd1 的列表。

    library(shiny)
    
    # Define UI for application that draws a histogram
    ui <- fluidPage(
    
      # Application title
      titlePanel("Old Faithful Geyser Data"),
    
      # Sidebar with a slider input for number of bins
      sidebarLayout(
        sidebarPanel(
          sliderInput("shift",
                      "shift of 2nd set",
                      min = -1,
                      max = 1,
                      value = 0, 
                      step = 0.1)  # I added a step
        ),
    
        # Show a plot of the generated distribution
        mainPanel(
          plotOutput("distPlot")
        )
      )
    )
    
    # Define server logic required to draw a histogram
    server <- function(input, output) {
      # This reactive environment can be accessed using data().
      data <- reactive({
        d1 <- matrix(nrow=100, ncol=512)
        for(i in 1:70){
          d1[i,] <- density(rnorm(1000),from = -3, to = 3)$y
        }
        for(i in 71:100){
          d1[i,] <- density(rnorm(1000, input$shift),from = -3, to = 3)$y
        }
        x <- density(rnorm(1000), from = -3, to = 3)$x
        list(x = x, d1 = d1)  # make sure that all objects are returned
      })
    
      output$distPlot <- renderPlot({
        matplot(data()$x, t(data()$d1), type = "l", lty = 1, col = c(rep(1,70),rep(2,30)))
      })
    }
    
    # Run the application
    shinyApp(ui = ui, server = server)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-26
      • 2020-01-31
      • 2021-05-21
      • 2018-10-19
      • 1970-01-01
      相关资源
      最近更新 更多