【问题标题】:R Shiny stop for user input and display all plotsR Shiny stop 用于用户输入并显示所有图
【发布时间】:2019-06-12 13:13:46
【问题描述】:

我有一个脚本要添加到我的 Shiny 应用程序中,原始脚本可以简化为以下内容:

plot(c(1:3),c(2,4,6), main ="This is first plot I want displayed")
a <- menu(c(1:5), title="what would you like to change the first point to?")
plot(c(1:3),c(a,4,6), main ="This is second plot I want displayed")
b <- menu(c(1:5), title="what would you like to change the second point to?")
plot(c(1:3),c(a,b,6), main ="This is second plot I want displayed")

上面的脚本绘制了第一个图,然后在绘制第二个之前等待用户输入,并在绘制第三个之前再次等待用户输入。

但是,当我尝试将其转换为如下所示的闪亮应用程序时,它永远不会更新第一个或第二个图,并且我试图让它停止以供用户输入显示的东西没有奏效。

我尝试过使用 req() 但它似乎完全停止了脚本,所以最后的事情根本没有运行,你必须重新启动整个脚本。

那么,我怎样才能让它按顺序显示所有的情节,我怎样才能让脚本在继续之前停止并等待输入?

if(interactive()){
ui <- fluidPage(
  actionButton("button","Click me"),
  selectInput("input", "Input", c(1:10)),
  textOutput("text"),
  plotOutput("plot")
)

server <- function(input, output) {
a<-1
observeEvent(input$button, {
output$plot <- renderPlot(plot(c(1:3),c(2,4,6), main ="This is first plot I want displayed"))
output$text <- renderText("Please select a number to multiply the first point with")
#This is where I want the script to wait for user input
output$plot <- renderPlot(plot(c((1),(2),(3)),c((input$input),(a),(3)), main="This is plot the second plot"))
a<-a+1
#Here I want the script to wait for user input again
output$plot <- renderPlot(plot(c((1),(2),(3)),c((input$input),(a),(3)), main="This is plot the third plot"))
    })
}

shinyApp(ui=ui, server=server)
}

目标是当它们在代码中呈现时更新绘图,并等待用户输入直到脚本继续,而不是继续执行。

【问题讨论】:

  • 我想澄清一下

标签: r shiny


【解决方案1】:

也许这就是你想要的。 req 用于仅在变量可用时显示。你需要在服务器中创建第二个renderUI,因为你不能在ui部分使用req

if(interactive()){
  ui <- fluidPage(
    plotOutput("plot1"),
    numericInput ("num1", "Please select a number to multiply the first point with", NA, 1, 10),
    plotOutput("plot2"),
    uiOutput("num2"),
    plotOutput("plot3")
  )

  server <- function(input, output) {

    output$plot1 <- renderPlot(plot(c(1:3),c(2,4,6), main ="This is first plot I want displayed"))

    output$plot2 <- renderPlot({
      req(input$num1)
      plot(c(1:3),c(2*(input$num1),4,(6)), 
           main="This is plot the second plot"
           )
    }
    )


    output$plot3 <- renderPlot({
      req(input$num1, input$num2)
      plot(c(1:3),c(2*(input$num1)+(input$num2),4,(6)), 
           main="This is plot the third plot"
      )
    }
    )



    output$num2 <- renderUI({
      req(input$num1)
      numericInput ("num2", "Please select a number to add to first point", NA, 1, 10)
    })



  }

  shinyApp(ui=ui, server=server)
}

【讨论】:

    【解决方案2】:

    说实话,我不能 100% 确定我是否知道您的期望,即使在阅读您的文字 5 次之后也是如此。我有一个猜测;-)。

    您的pause 函数可以使用invalidateLater 完成,该函数会导致绘图一步一步地呈现。这必须在 reactiveValue 中“存在”。我不确切知道这个函数的创建者是谁,我把它保存在我的 sn-ps 中(所有荣耀归于不知名的人)。

    要根据用户的输入渲染绘图或运行脚本,请尝试使用if 语句来捕获它。

    希望这会有所帮助:-)。

    library(shiny)
    if(interactive()){
        ui <- fluidPage(
            selectInput("input", "Input", c(1:10)),
            actionButton("apply", "Apply"),
            textOutput("text"),
            plotOutput("plot")
        )
    
        server <- function(input, output, session) {
    
            rv <- reactiveValues(i = 0)
            maxIter <- 10
    
            observeEvent(input$apply, {
                rv$i <- 0
                observe({
                    isolate({
                        rv$i <- rv$i + 1
                    })
    
                    if (isolate(rv$i) < maxIter){
                        invalidateLater(2000, session)
                    }
                })
            })
    
            output$plot <- renderPlot( {
                if(rv$i > 0) {
                    if(input$input <= 4) {
                        plot(c((rv$i*1),(rv$i*2),(rv$i*3)),c((1),(2),(3)), main = sprintf("Input <= 4; Round %i", rv$i), type = "l")  
                    } else {
                        plot(c((rv$i*1),(rv$i*5),(rv$i*4)),c((1),(2),(3)), main = sprintf("Input > 4; Round %i", rv$i), type = "l")  
                    }
    
                } else {
                    plot(c(1:3),c(2,4,6), main ="This is first plot")
                }
            })
    
        }
    
        shinyApp(ui=ui, server=server)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-26
      • 2018-01-31
      • 1970-01-01
      • 2019-03-26
      • 1970-01-01
      • 2021-04-13
      • 2014-12-06
      • 1970-01-01
      相关资源
      最近更新 更多