简体   繁体   English

如何在R Shiny应用程序中对许多输出变量的输入变量执行相同的计算?

[英]How do I perform the same calculation on an input variable for many output variables in an R shiny app?

Situation 情况

I have a shinyApp with one slider input and two text outputs. 我有一个带有一个滑块输入和两个文本输出的ShinyApp。 The App does some calculation on the input and returns the output in static text fields. 该应用程序对输入进行一些计算,然后在静态文本字段中返回输出。 Here is my current code: 这是我当前的代码:

library(shiny)

ui=fluidPage(
  sliderInput("slider", "Slide Me", 0, 100,1),
  textOutput("result01"),
  textOutput("result02")
)

server=function(input,output){

output$result01=renderText({
  MYVARIABLE=(input$slider)^12
  MYVARIABLE+34543
  })

output$result02=renderText({
  MYVARIABLE=(input$slider)^12
  MYVARIABLE+67544
})

}

shinyApp(ui, server)

Problem 问题

The code is inefficent, because it does the same calculation TWICE: 该代码无效,因为它执行两次相同的计算:

MYVARIABLE=(input$slider)^12

This is okay for the moment, because the calculation is not terribly complicated and because I only have two outputs. 目前还可以,因为计算并不十分复杂,并且我只有两个输出。 In the future however, I want to do a more complicated calculation for many more outputs at the same time. 但是,在将来,我想对更多的输出同时进行更复杂的计算。

Wish 希望

To only do the same calculation once and not multiple times, I would like to do something like this on the server side: 为了只一次执行相同的计算,而不是多次,我想在服务器端执行以下操作:

server=function(input,output){

MYVARIABLE=(input$slider)^12

output$result01=renderText({
  MYVARIABLE+34543
  })

output$result02=renderText({
  MYVARIABLE+67544
})

}

However, this gives the following error message: 但是,这给出了以下错误消息:

Error in .getReactiveEnvironment()$currentContext() : 
  Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)

Question

What do I need to change my code to, to make this work? 要使此工作有效,我需要将代码更改为什么? (Thanks in advance.) (提前致谢。)

We can place that inside reactive 我们可以将其放置在reactive

library(shiny)
options(scipen = 999) 
ui <- fluidPage(
  sliderInput("slider", "Slide Me", 0, 100,1),
  textOutput("result01"),
  textOutput("result02")
)

server <- function(input,output){

  MYVARIABLE <- reactive({(input$slider)^12})

  output$result01 <- renderText({
    MYVARIABLE()+34543
  })

  output$result02 <- renderText({
    MYVARIABLE()+67544
  })

}

shinyApp(ui, server)

-output - 输出

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 R Shiny App 中的简单计算给出了不同于 Rstudio 中相同计算的另一个输出 - Simple Calculation in R Shiny App gives another output than the same calculation in Rstudio R Shiny应用程序-当用户下次触发应用程序刷新时,如何自动将最新输出用作输入的一部分? - R Shiny app - How do I automatically use my most recent output as part of the input when the user next triggers the app to refresh? 如何在 R 中为 Shiny 应用程序输入变量文件? - How do I have variable file inputs for a Shiny app in R? R Shiny:如何根据CSV和用户输入(滑块)的值执行计算? - R Shiny: How to perform calculation based on values from a CSV and user input (slider)? R Shiny:使用输出中的变量在函数之外进行进一步的计算 - R Shiny: Using variables from output for further calculation outside of function 如何根据滑块输入值的增量为R Shiny绘图的输出设置动画? - How do I animate my R Shiny plot's output based on the increments of slider input value? 如何根据输入显示可变数量的R闪亮的ggplots? - How do I display variable number of ggplots in R shiny, depending on input? 对动态列数执行相同的计算,每个列的R中都有新变量 - Perform Same Calculation on Dynamic Number of Columns, Each with New Variable in R 如何让我的 R Shiny 应用程序停止获取变量的无效类型(列表)? - How do I get my R Shiny app to stop getting an invalid type (list) for a variable? 如何在R Shiny中将insertUI()输出添加到renderText()输出? - How do I add insertUI() output to renderText() ouput in R Shiny?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM