【问题标题】:Interactive plot updated during sliding in R在 R 中滑动期间更新的交互式绘图
【发布时间】:2020-04-19 18:17:39
【问题描述】:

我需要一个带有两个滑块的交互式绘图,我希望绘图在滑动时平滑更新,而无需释放鼠标按钮。你知道 R 中的任何其他解决方案能够这样做?

我找到了一个不错的库manipulate(参见下面的示例),但是只有释放鼠标按钮后才会更新绘图,所以我必须停止滑动才能看到更新的绘图(这使得体验不流畅)。我希望在滑动期间更新绘图。

library(manipulate)
manipulate(curve(amp*sin(freq*x), xlim = c(0,10), ylim =c(-1,1)), amp = slider(0.1,1), freq = slider(0.1,10))

PS:我习惯于只使用基本图形库,所以我更喜欢使用它的简单解决方案;但如果没有其他办法,ggplot/lattice 也不错:-))

【问题讨论】:

    标签: r


    【解决方案1】:

    感谢您在 base R 中寻找答案,但是对于 R 中的交互式绘图来说,闪亮通常是一个不错的选择。

    以下代码应该实现您想要的输出(并且您不必释放鼠标按钮来更新绘图):

    library(shiny)
    
    ui <- fluidPage(
    
      # Sidebar with a slider input
      sidebarLayout(
        sidebarPanel(
          sliderInput(
            inputId = "amp",
            label = "Amp:",
            min = 0.1,
            max = 1,
            value = 0.5
          ),
          sliderInput(
            inputId = "freq",
            label = "Freq:",
            min = 0.1,
            max = 10,
            value = 0.5
          )
        ),
    
        # Show a plot
        mainPanel(
          plotOutput("plot")
        )
      )
    )
    
    # Define server logic
    server <- function(input, output) {
      output$plot <- renderPlot({
        amp <- input$amp
        freq <- input$freq
    
        curve(amp * sin(freq * x), xlim = c(0, 10), ylim = c(-1, 1))
      })
    }
    
    # Run the application
    shinyApp(ui = ui, server = server)
    
    

    【讨论】:

    • 谢谢。但是由于某种原因,延迟非常慢(即使它在 localhost 和非常强大的 8 核计算机上),它甚至比之前释放鼠标按钮的解决方案还要慢......
    猜你喜欢
    • 1970-01-01
    • 2021-06-16
    • 1970-01-01
    • 1970-01-01
    • 2018-10-28
    • 1970-01-01
    • 2017-10-22
    • 2018-04-28
    • 2020-01-14
    相关资源
    最近更新 更多