【问题标题】:R Shiny: how to make input value conditionally reactive (on another input)?R Shiny:如何使输入值有条件地反应(在另一个输入上)?
【发布时间】:2016-09-07 19:54:12
【问题描述】:

我希望应用使用默认值加载一次,但只有在用户输入正确密码时才会响应。为简单起见,让我们使用 Rstudio 模板(经过精心编辑):

ui.R

library(shiny)

shinyUI(fluidPage(  
   sidebarLayout(
      sidebarPanel(              
          passwordInput("pw", "Password:"),

          sliderInput("nbins",
                      "Number of bins:",
                      min = 1,
                      max = 50,
                      value = 30)
      ),    
    mainPanel(
    plotOutput("histo")
    )
)))

server.R

PASSWORD <- "test"

library(shiny)

shinyServer(function(input, output) {

    output$histo <- renderPlot({
        x    <- faithful[, 2]
        bins <- seq(min(x), max(x), length.out = input$nbins + 1)
        hist(x, breaks = bins, col = 'darkgray', border = 'white')
    })
})

有两个反应输入 pwnbins。我的问题是:如何扩展代码以使 nbins(行为)在 reactivenon-reactive 之间切换,具体取决于输入 pw 等于 @ 987654327@?

【问题讨论】:

  • conditionalPanel() 是您要找的吗?
  • @VanceLopez 谢谢,但不,我正在寻找如何在服务器端执行此操作。如果那是可能的。您将如何使用 conditionalPanel() 解决此问题?
  • 我想我不确定您所说的“在反应式和非反应式之间切换”是什么意思。您是否要阻止用户与 nbins 交互,除非他们拥有正确的密码,但仍希望根据输入的原始值显示 output$histo
  • @VanceLopez 完全正确!

标签: r reactive-programming shiny


【解决方案1】:

基于 Valter 的回答,您可以使用 shinyjs 启用/禁用与输入小部件的交互性。

ui.R

library(shiny)
library(shinyjs) # install shinyjs

shinyUI(fluidPage(  
  useShinyjs(), # activate
  sidebarLayout(
    sidebarPanel(              
      passwordInput("pw", "Password:"),

      sliderInput("nbins",
                  "Number of bins:",
                  min = 1,
                  max = 50,
                  value = 30)
    ),    
    mainPanel(
      plotOutput("histo")
    )
  )))

server.R

library(shiny)
library(shinyjs)

shinyServer(function(input, output) {

  observe({
    if(input$pw != "PASSWORD") shinyjs::hide("nbins") else shinyjs::show("nbins")
  })

  output$histo <- renderPlot({
    x    <- faithful[, 2]

    # will 'reset' bins to original value if incorrect pw
    if(input$pw != "PASSWORD") {
      bins <- seq(min(x), max(x), length.out = 30 + 1)
    } else {
      bins <- seq(min(x), max(x), length.out = input$nbins + 1)
    }

    hist(x, breaks = bins, col = 'darkgray', border = 'white')
  })
})

【讨论】:

    【解决方案2】:

    这个解决方案怎么样:

    PASSWORD <- "test"
    
        library(shiny)
    
        shinyServer(function(input, output) {
                bins <- eventReactive(input$nbins, {
                        if (input$pw == PASSWORD) {
                        bins <- seq(min(faithful[, 2]), max(faithful[, 2]), length.out = input$nbins + 1) 
                        } else {
                        bins <- seq(min(faithful[, 2]), max(faithful[, 2]), length.out = 30 + 1)      
                        }
                })
    
                output$histo <- renderPlot({
                        x <- faithful[, 2]                       
                        hist(x, breaks = bins(), col = 'darkgray', border = 'white')
                })
        })
    

    【讨论】:

      猜你喜欢
      • 2020-06-27
      • 1970-01-01
      • 1970-01-01
      • 2019-06-30
      • 1970-01-01
      • 1970-01-01
      • 2016-10-11
      • 2017-01-03
      • 2016-10-02
      相关资源
      最近更新 更多