[英]How to get these shinyApp parameters to be "reactive" to input changes
我正在构建一个简单的 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 的新手 - 似乎我必须使用 react() 和 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)
我已经尝试根据此处的示例进行以下更改,我可以说它不起作用但不确定要进行哪些更改....
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)
})
我们需要使依赖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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.