【问题标题】:How to get these shinyApp parameters to be "reactive" to input changes如何让这些 shinyApp 参数对输入更改“反应”
【发布时间】:2016-05-26 09:07:27
【问题描述】:

我正在构建一个简单的 shinyApp,它在给定两个分位数(lbv 和 ubv)的情况下绘制正态分布,对应于 5% 和 95% 的概率(90% 的置信区间)。分位数是用户定义的输入。

为了获得普通 PDF 的均值和标准差,我使用了 rriskDistributions 包中的 get.norm.par(),如下所示:

dpar <- get.norm.par(p=c(0.05,0.95),q=c(lbv,ubv),plot=F)
mean <- dpar[1]
sd <- dpar[2]

如何让 shinyApp 对 UI 中的输入更改做出反应?我是 Shiny 的新手 - 似乎我必须使用 reactive() 和 refresh(?) 但我不知道在哪里/如何使用它。任何提示将不胜感激。

下面的代码生成了 shinyApp,但它对用户定义的输入变化没有“反应性”。

# Set libraries
library(shiny)
library(rriskDistributions)

# Global variables can go here
lb <- 0.05
ub <- 0.95
lbv <- 200
ubv <- 1000
dpar <- get.norm.par(p=c(lb,ub),q=c(lbv,ubv),plot=F)
mean <- dpar[1]
sd <- dpar[2]

x1 <- lbv - (ubv-lbv)/2 # set my x-axis left bound
x2 <- ubv + (ubv-lbv)/2 # set my x-axis right bound

xseq<-seq(x1,x2,.1)

densities<-dnorm(xseq, mean,sd)

ui <- fluidPage(
        titlePanel("Parameters"),
        sidebarLayout(
                sidebarPanel(
                        numericInput('lbv', 'Lower Bound Value', lbv),
                        numericInput('ubv', 'Upper Bound Value', ubv)

                ),
                mainPanel(
                        plotOutput('plot')
                )
        )
)

server <- function(input, output) {    
        output$plot <- renderPlot({
        plot(xseq, densities, col="darkgreen", xlab="", ylab="Density", type="l",lwd=2, cex=2, main="Normal Density", cex.axis=.8)
        })
}

shinyApp(ui = ui, server = server)

我已尝试根据示例 here 进行以下更改,我可以判断它不起作用但不确定要进行哪些更改....

 sidebarPanel(
                      numericInput('lbv', 'Lower Bound Value', lbv),
                      numericInput('ubv', 'Upper Bound Value', ubv),
                      actionButton(inputId = "refresh", label = "Refresh" , 
                                      icon = icon("fa fa-refresh"))

dataInput <- reactive({
    get.norm.par(p=c(lb,ub),q=c(input$lbv,input$ubv),plot=F)
    mean <- get.norm.par(p=c(lb,ub),q=c(input$lbv,input$ubv),plot=F)[1]
    sd <- get.norm.par(p=c(lb,ub),q=c(input$lbv,input$ubv),plot=F)[2]

    x1 <- input$lbv - (input$ubv-input$lbv)/2 # set my x-axis left bound
    x2 <- input$ubv + (input$ubv-input$lbv)/2 # set my x-axis right bound

    xseq<-seq(x1,x2,.1)

    densities<-dnorm(xseq, mean,sd)
})

【问题讨论】:

  • ui.R 中定义的所有 全局 变量,将它们移动到 server.R 并使其具有反应性。本质上,xseqdensities 必须是响应式对象。
  • @zx8754:只有一个文件,app.R。我是否只是在反应({})中定义有问题的变量?我试试……
  • 我的意思是在服务器位。
  • @zx8754:不仅仅是将它们移动到服务器位中。我这样做了,但仍然没有区别。我该如何继续调用这些变量?有什么特殊的语法吗?

标签: r reactive-programming shiny


【解决方案1】:

我们需要使依赖input$的对象反应,见下文:

# Set libraries
library(shiny)
library(rriskDistributions)

# Global variables can go here
lb <- 0.05
ub <- 0.95


ui <- fluidPage(
  titlePanel("Parameters"),
  sidebarLayout(
    sidebarPanel(
      numericInput('lbv', 'Lower Bound Value', 200),
      numericInput('ubv', 'Upper Bound Value', 1000)
    ),
    mainPanel(
      plotOutput('plot')
    )
  )
)

server <- function(input, output) {    

  xseq <- reactive({
    x1 <- input$lbv - (input$ubv - input$lbv)/2 # set my x-axis left bound
    x2 <- input$ubv + (input$ubv - input$lbv)/2 # set my x-axis right bound
    # return
    seq(x1, x2, 0.1)
  })

  densities <- reactive({
    dpar <- get.norm.par(p = c(lb, ub), q = c(input$lbv, input$ubv), plot = FALSE)
    mean <- dpar[1]
    sd <- dpar[2]
    # return
    dnorm(xseq(), mean, sd)
  })

  output$plot <- renderPlot({
    plot(xseq(), densities(),
         col = "darkgreen", xlab="", ylab="Density", type="l",lwd=2, cex=2,
         main="Normal Density", cex.axis=.8)
  })
}

shinyApp(ui = ui, server = server)

【讨论】:

    猜你喜欢
    • 2021-11-09
    • 2021-06-23
    • 2021-02-12
    • 1970-01-01
    • 1970-01-01
    • 2018-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多