【问题标题】:Displaying an updated variable after clicking an action button in Shiny单击 Shiny 中的操作按钮后显示更新的变量
【发布时间】:2017-04-05 14:02:58
【问题描述】:

我是闪亮的新手,并试图使用操作按钮完成一项相当简单的任务:

  • 用户点击一个按钮,一个函数被调用
  • 此函数使用输入变量进行一些计算并更新/创建多个全局变量(reactiveValues,可能在 observe 块内?)
  • 我想在 UI 上显示这些值(使用 render* 函数)
  • 每当用户更改输入值时,UI 都会自动更新

相关代码位为:

server.R

...
rv <- reactiveValues() 
observe({
 if(input$run){
  rv$a <- someFunc(input$aa)
 }
})
output$msg = renderText({ rv$a })
...

ui.R

...
selectInput("aa", ...)
...
actionButton("run", "Run")
...
textOutput("msg")

如何根据每次用户点击按钮时输入的aa 更改msg

【问题讨论】:

  • 代码中的...s 不是很有帮助。尝试制作reproducible example。准确描述您想要将msg 值更改为什么以及如何涉及aa。我真的不明白这里有什么问题。您现在观察到的具体行为是什么。
  • 就上面指定的代码而言,renderText 已经更改了 UI.r 中的“msg”。是否要根据输入更改textOutput 的实际措辞?
  • 对我有什么反馈吗?
  • @MrFlick 是的,我知道,但是我的代码非常复杂,无法像那样复制。感谢您的评论。

标签: r shiny reactive-programming


【解决方案1】:

我不相信我理解你想要什么,但我想它是这样的:

library(shiny)
u <- fluidPage(
  titlePanel("Simple Selectable Reactive Function"),
    sidebarLayout(
      sidebarPanel(
        sliderInput("vv", "Choose a value",min=-3.14,max=3.14,value=0),
        selectInput("aa", "Choose a function", choices=c("sin","cos","exp")),
        actionButton("run", "Change Function and Run")
      ),
     mainPanel(
      h2("Results"),
      verbatimTextOutput("msg")
 )))
s <- function(input,output){

  rv <- reactiveValues(func=NULL) 

  observeEvent(input$run,{   rv$func <- input$aa })

  funcval <- reactive({
    v <- 0
    if (rv$func=="sin") v <- sin(input$vv)
    if (rv$func=="cos") v <- cos(input$vv)
    if (rv$func=="exp") v <- exp(input$vv)
    v
  })
  output$msg = renderPrint({
       if (is.null(rv$func)) return("not running")
       fv <- funcval()
       sprintf("%s(%.3f)=%.3f",rv$func,input$vv,fv)
    })
}
shinyApp(ui=u,server=s)

产生这个:

请注意,当最小值和最大值不均匀时,滑块输入值的格式会非常糟糕。不知道对此能做些什么。

【讨论】:

  • 我正在寻找一个简单的例子,你的成功了!
猜你喜欢
  • 2020-01-03
  • 2018-11-14
  • 2017-10-30
  • 1970-01-01
  • 2021-02-10
  • 2013-01-02
  • 1970-01-01
  • 2021-06-18
  • 1970-01-01
相关资源
最近更新 更多