gpt4 book ai didi

用于以后计算的 Shiny 中的 react 变量

转载 作者:行者123 更新时间:2023-12-02 08:17:13 24 4
gpt4 key购买 nike

总新手在这里。

我有一个输出写入公式的结果,变量基于输入

output$text_calc <- renderText({
paste("The result is =", input$num_W * input$num_l - input$slider_a... )
})

为了避免我的大脑因长公式而爆炸,我如何将输入定义为单字母变量?我试过 l <- input$num_l这给了我

“在没有主动 react 上下文的情况下不允许操作。(你试图做一些只能从 react 表达式或观察者内部完成的事情。)”

reactive({})围绕代码给出

错误:无法将类型“闭包”强制转换为“字符”类型的向量

最佳答案

试试下面的例子。另外,请注意使用 reactive 中的输入。表达式
示例 1

#rm(list = ls())
library(shiny)
ui <- shinyUI(fluidPage(
mainPanel(
numericInput("num_W", "Observations:", 10,min = 1, max = 100),
numericInput("num_l", "Observations:", 10,min = 1, max = 100),
sliderInput("slider_a","test",value = 1,min=1,max=20,step=1),
textOutput("text_calc"))
))
server <- shinyServer(function(input, output,session){
output$text_calc <- renderText({
W <- input$num_W
L <- input$num_l
A <- input$slider_a
paste("The result is =", W*L-A)
})
})

shinyApp(ui = ui, server = server)
示例 2,使用 reactiveValues()
#rm(list = ls())
library(shiny)
ui <- shinyUI(fluidPage(
mainPanel(
numericInput("num_W", "Observations:", 10,min = 1, max = 100),
numericInput("num_l", "Observations:", 10,min = 1, max = 100),
sliderInput("slider_a","test",value = 1,min=1,max=20,step=1),
textOutput("text_calc"))
))
server <- shinyServer(function(input, output,session){

vals <- reactiveValues()
observe({
vals$W <- input$num_W
vals$L <- input$num_l
vals$A <- input$slider_a
})

output$text_calc <- renderText({
paste("The result is =", vals$W*vals$L-vals$A)
})
})

shinyApp(ui = ui, server = server)
enter image description here

关于用于以后计算的 Shiny 中的 react 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40997817/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com