Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R:通过循环添加到绘图_R_Shiny - Fatal编程技术网

R:通过循环添加到绘图

R:通过循环添加到绘图,r,shiny,R,Shiny,我正在尝试创建一个应用程序,该应用程序使用Shiny来显示采样方式的动画。类似于所示示例的内容 下面是一些简单的代码,显示了我遇到问题的部分。这不是我使用的数据,而是一个可复制的示例数据集 library(shiny) library(ggplot2) data <- data.frame(ID=1:60, x=sort(runif(n = 60)), y=sort(runif(n = 60)+rnorm(6

我正在尝试创建一个应用程序,该应用程序使用Shiny来显示采样方式的动画。类似于所示示例的内容

下面是一些简单的代码,显示了我遇到问题的部分。这不是我使用的数据,而是一个可复制的示例数据集

library(shiny)
library(ggplot2)

data <- data.frame(ID=1:60, 
                   x=sort(runif(n = 60)), 
                   y=sort(runif(n = 60)+rnorm(60)))

ui <- fluidPage(
    sidebarPanel(
        sliderInput("n",
                    "Number of samples:",
                    min = 10,
                    max = 100,
                    value = 20),

        sliderInput("surveys",
                    "Number of surveys:",
                    min = 10,
                    max = 100,
                    value = 20),

        checkboxInput("replacement", 
                      "Sample with replacement?"),

        actionButton("button", "Go!")
    ),
    # Show the plot
    mainPanel(
        plotOutput("plot1")
    )
)

server <- function(input, output) {
    output$plot1 <- renderPlot({
        plot1 <- ggplot(data, aes(x=x, y=y)) + geom_point(colour="red") + theme_bw()
        plot1 <- plot1 + geom_vline(xintercept = mean(data$x), size=1.1, colour="red")
        plot1

        for(i in 1:20){
            data$sampled <- "red"
            sample.rows <- sample(data$ID, 20, replace = F)
            data$sampled[sample.rows] <- "green"

            plot1 <- plot1 + geom_point(x=data$x, y=data$y, colour=data$sampled, size=2)

            sample.mean.x <- mean(data$x[sample.rows])

            plot1 <- plot1 + geom_vline(xintercept = sample.mean.x, colour="green")

            print(plot1)
            Sys.sleep(1.5)
        }
    })
}

# Run the application 
shinyApp(ui = ui, server = server)
库(闪亮)
图书馆(GG2)

数据您可以使用
reactiveTimer
执行此操作。我已经修改了代码的服务器部分。在下面的代码中,我将计时器设置为两秒,以便绘图每两秒更新一次

  server <- function(input, output) {

    autoInvalidate <- reactiveTimer(2000)
    plot1 <- NULL

    output$plot1 <- renderPlot({
      plot1 <<- ggplot(data, aes(x=x, y=y)) + geom_point(colour="red") + theme_bw()
      plot1 <<- plot1 + geom_vline(xintercept = mean(data$x), size=1.1, colour="red")
      plot1
    })

    observeEvent(input$button,{

      output$plot1 <- renderPlot({
        autoInvalidate()
        data$sampled <- "red"
        sample.rows <- sample(data$ID, 20, replace = F)
        data$sampled[sample.rows] <- "green"

        plot1 <<- plot1 + geom_point(x=data$x, y=data$y, colour=data$sampled, size=2)

        sample.mean.x <- mean(data$x[sample.rows])

        plot1 <<- plot1 + geom_vline(xintercept = sample.mean.x, colour="green")

        plot1

      })
    })
  }

server您可以使用
reactiveTimer
执行此操作。我已经修改了代码的服务器部分。在下面的代码中,我将计时器设置为两秒,以便绘图每两秒更新一次

  server <- function(input, output) {

    autoInvalidate <- reactiveTimer(2000)
    plot1 <- NULL

    output$plot1 <- renderPlot({
      plot1 <<- ggplot(data, aes(x=x, y=y)) + geom_point(colour="red") + theme_bw()
      plot1 <<- plot1 + geom_vline(xintercept = mean(data$x), size=1.1, colour="red")
      plot1
    })

    observeEvent(input$button,{

      output$plot1 <- renderPlot({
        autoInvalidate()
        data$sampled <- "red"
        sample.rows <- sample(data$ID, 20, replace = F)
        data$sampled[sample.rows] <- "green"

        plot1 <<- plot1 + geom_point(x=data$x, y=data$y, colour=data$sampled, size=2)

        sample.mean.x <- mean(data$x[sample.rows])

        plot1 <<- plot1 + geom_vline(xintercept = sample.mean.x, colour="green")

        plot1

      })
    })
  }

服务器可能会看这个问题:。您的策略不会起作用,因为在函数返回之前不会有任何更新。请看以下问题:。你的策略不会起作用,因为在函数返回之前不会有任何更新。太棒了!非常感谢你!只是一个后续问题:我应该把第二个代码块(即编辑后的第一个代码块)放在哪里?没关系,我已经计算出了那一位。但是,如何让绘图首先显示?在第一个版本(没有20限制)中,它首先显示绘图,但在开始之前不会循环!按钮按下了。第二个版本不会首先显示绘图…也解决了这个问题;)必须将
observeEvent(input$按钮,{
移动到第二个
renderPlot之前({
调用。你是对的,但是如果你将“renderPlot”移到
observeEvent
input$按钮的
input$之外,你可能会遇到一个问题。在这种情况下,你可能想把renderPlot`留在
observeEvent
内。太棒了!非常感谢你h!只是一个后续问题:我应该把第二个代码块(即编辑后的第一个代码块)放在哪里?没关系,算出了那一点。如何让绘图首先显示?在第一个版本中(没有20个限制),它首先显示绘图,但在按下Go!按钮之前不会循环。第二个版本不会首先显示绘图,但…也解决了这一问题;)必须将
observeEvent(输入$button,{
移动到第二个
渲染图之前({
调用。你是对的,但是如果你将“renderPlot”移到
observeEvent
input$按钮的
input$之外,你需要计算一个可能会遇到的问题。因此,在这些情况下,你可能希望将renderPlot`留在
observeEvent
内。
server <- function(input, output, session) {

count = 0
plot1 <- NULL


  output$plot1 <- renderPlot({
    plot1 <<- ggplot(data, aes(x=x, y=y)) + geom_point(colour="red") + theme_bw()
    plot1 <<- plot1 + geom_vline(xintercept = mean(data$x), size=1.1, colour="red")
    plot1
  })

observeEvent(input$button,{
 count <<- 0
  output$plot1 <- renderPlot({

    count <<- count+1
    invalidateLater(1500, session,  count < 20)
    data$sampled <- "red"
    sample.rows <- sample(data$ID, 20, replace = F)
    data$sampled[sample.rows] <- "green"

    plot1 <<- plot1 + geom_point(x=data$x, y=data$y, colour=data$sampled, size=2)

    sample.mean.x <- mean(data$x[sample.rows])

    plot1 <<- plot1 + geom_vline(xintercept = sample.mean.x, colour="green")

    plot1

  })
})


 }