【问题标题】:Guess the correlation - Is there an R function in shiny to show the next plot? data collection with shiny猜猜相关性 - 是否有一个 R 函数闪亮显示下一个情节?闪亮的数据收集
【发布时间】:2019-10-31 10:46:19
【问题描述】:

我想用下一步按钮显示三个不同的散点图。任务是猜测每个图中相关性的强度。问题是我只能看到相同的情节。我尝试使用下一个按钮来查看下一个情节。

library(shiny)

# 3 different dataframes/data for scatter plots
data1 <- data.frame(a <- c(20,30,35,45,50,60,80),
                    b <- c(60,70,72,77,82,88,90))
data2 <- data.frame(a <- c(20,30,35,45,50,60,80),
                    b <- c(60,70,68,77,82,88,70))
data3 <- data.frame(a <- c(35,40,38,50,52,51,30),
                    b <- c(60,70,72,64,82,88,90))

ui = fluidPage(
  sidebarPanel(
    sliderInput
    (inputId = "fit", label = "estimated correlation",
     min = 0, max = 1,     value = 0),
     actionButton("newplot", "next")),
  mainPanel(plotOutput("plot")))

server = function(input, output) {
output$plot <- renderPlot({
input$newplot
plot(data1)
plot(data2)
plot(data3)
  })
  }

shinyApp(ui = ui, server = server)

【问题讨论】:

    标签: r plot shiny


    【解决方案1】:

    我不相信有任何内置方法可以做到这一点,但您可以轻松地保留一个变量来跟踪您想要显示的绘图,然后使用switch() 来绘制该绘图。例如

    cycle <- function(x, max) {
      if(x() < max) {
        x(x() + 1)
      } else {
        x(1)
      }
    }
    
    server <- function(input, output) {
      index <- reactiveVal(1)
      observeEvent(input$newplot, {
        cycle(index, 3)
      })
      output$plot <- renderPlot({
        switch(index(),
          plot(data1),
          plot(data2),
          plot(data3)
        )
      })
    }
    

    这里我刚刚编写了cycle 辅助函数,以便在增加索引时更容易循环回1。但基本上它只是取值 1 到 3。然后在 renderPlot 中,它看起来是当前索引并且只绘制该图。

    【讨论】:

    • 谢谢。作品。唯一的问题:每个新图的滑块输入起始值现在具有上一个图的最后一个答案/的位置。有什么办法可以避免吗?例如:总是为零?
    • 我不确定我是否理解。您的滑块在您的示例中似乎没有做任何事情
    • 当我猜测第一个图中的相关性时,假设我使用滑块选择 50,然后单击“下一步”查看第二个图,然后我会看到 50 作为起始值.假设我会为第二个情节选择 70,然后单击下一步,我会将 70 视为起始位置。相反,我的目标是让起始值始终位于同一位置,例如始终为 0。
    • 您可以使用updateSliderInput() 更改滑块的值。你可以把它放在你观察到按钮点击的地方。
    猜你喜欢
    • 1970-01-01
    • 2017-01-21
    • 1970-01-01
    • 2014-09-12
    • 2014-05-13
    • 2021-07-08
    • 1970-01-01
    • 1970-01-01
    • 2017-11-07
    相关资源
    最近更新 更多