其他无功变量更改后,无功R / Shiny应用程序“忘记”变量状态

时间:2018-10-23 12:49:33

标签: r shiny reactive-programming reactive

以下是可重现的示例。在直方图区域中单击时,将出现一个文本框,显示该单击的位置。但是,当您在侧面板中移动滑块时,该文本框将消失,您必须再次单击直方图以使其重新出现。如何确保一旦单击图,即使移动了滑块,文本框仍会保留(并保持相同的内容),直到下次单击为止。

ui <- fluidPage(

  titlePanel("Hello Shiny!"),

  sidebarLayout(

    sidebarPanel(

      sliderInput(inputId = "bins",
            label = "Number of bins:",
            min = 1,
            max = 50,
            value = 30)

    ),

    mainPanel(

      plotOutput(outputId = "distPlot",click = "plot_click"),
      verbatimTextOutput("click_x")
    )
  )
)

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

  output$click_x <- renderText({
    input$plot_click$x
  })


  output$distPlot <- renderPlot({

    x    <- faithful$waiting
    bins <- seq(min(x), max(x), length.out = input$bins + 1)

    hist(x, breaks = bins, col = "#75AADB", border = "white",
       xlab = "Waiting time to next eruption (in mins)",
       main = "Histogram of waiting times")

  })

}

shinyApp(ui, server)

1 个答案:

答案 0 :(得分:1)

简单的条件输出即可解决问题:

assert mock_instance.call_count == 1
call_args = mock_instance.call_args[0]
call_kwargs = mock_instance.call_args[1]
pd.testing.assert_frame_equal(call_kwargs['dataframe'], pd.DataFrame())

如果要存储先前的值,可以将条件应用于变量归因。您可以使用observeEvent:

output$click_x <- renderText({
    if(is.null(input$plot_click$x)) "Select point in graph" else input$plot_click$x
})