admin管理员组文章数量:1122832
I’m planning to transform a large Excel model into R Shiny. Before starting to code, I’d like to clarify a few aspects. One of these involves translating the dependency logic from Excel, and I’ll explain it using a minimal example below.
Let’s assume we have one input, Input 1
, and a simple 1 × 5 data frame
. The value for Year 1
is based on real data.
- The values for the subsequent years depend on previous years. This relationship is demonstrated in the formula view in Excel below:
- Additionally,
Input 1
also influences these years:
Given that this method will apply to more than 200 variables, I’m wondering about the best practices for creating reactive values that:
- Depend on prior data (e.g.,
Year 2
) or reactive data (e.g., fromYear 3
onwards). - Are also affected by
Input 1
.
I hope I’m not overthinking it—any feedback or suggestions would be much appreciated.
I’m planning to transform a large Excel model into R Shiny. Before starting to code, I’d like to clarify a few aspects. One of these involves translating the dependency logic from Excel, and I’ll explain it using a minimal example below.
Let’s assume we have one input, Input 1
, and a simple 1 × 5 data frame
. The value for Year 1
is based on real data.
- The values for the subsequent years depend on previous years. This relationship is demonstrated in the formula view in Excel below:
- Additionally,
Input 1
also influences these years:
Given that this method will apply to more than 200 variables, I’m wondering about the best practices for creating reactive values that:
- Depend on prior data (e.g.,
Year 2
) or reactive data (e.g., fromYear 3
onwards). - Are also affected by
Input 1
.
I hope I’m not overthinking it—any feedback or suggestions would be much appreciated.
Share Improve this question asked Nov 21, 2024 at 10:20 Grasshopper_NZGrasshopper_NZ 7555 silver badges14 bronze badges 3 |1 Answer
Reset to default 1A direct translation would be to convert each formula cell into a reactive:
library(shiny)
ui <- fluidPage(
sliderInput("input_1", "Input 1", min = 0.1, max = 2, value = 1.1),
numericInput("year_1", "Year 1", value = 1),
verbatimTextOutput("result"),
)
server <- function(input, output, session) {
year_1 <- reactive(input$year_1)
year_2 <- reactive(year_1() * input$input_1)
year_3 <- reactive(year_2() * input$input_1)
year_4 <- reactive(year_3() * input$input_1)
output$result <- renderPrint({
c(year_1(), year_2(), year_3(), year_4())
})
}
shinyApp(ui, server)
This is the most general approach but may not be particularly efficient.
If you have a recursive formula it may be better to use a single reactive vector:
library(shiny)
ui <- fluidPage(
sliderInput("input_1", "Input 1", min = 0.1, max = 2, value = 1.1),
numericInput("year_1", "Year 1", value = 1),
verbatimTextOutput("result"),
)
server <- function(input, output, session) {
years <- reactive({
values <- numeric(4)
values[1] <- input$year_1
for (i in seq_along(values)[-1]) {
values[i] <- values[i - 1] * input$input_1
}
values
})
output$result <- renderPrint(years())
}
shinyApp(ui, server)
But a more idiomatic approach in R would be to find an explicit formula, if available:
library(shiny)
ui <- fluidPage(
sliderInput("input_1", "Input 1", min = 0.1, max = 2, value = 1.1),
numericInput("year_1", "Year 1", value = 1),
verbatimTextOutput("result"),
)
server <- function(input, output, session) {
years <- reactive({
input$year_1 * input$input_1^(seq_len(4) - 1)
})
output$result <- renderPrint(years())
}
shinyApp(ui, server)
There's tradeoffs to each approach, depending on the details of your application.
本文标签: Create a chain of reactive values in R ShinyStack Overflow
版权声明:本文标题:Create a chain of reactive values in R Shiny - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736311854a1934888.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
Input1
, one for all of row 5. The row 5 variable depends onInput1
. So it's not really as complicated when you put it in R. – user2554330 Commented Nov 21, 2024 at 10:46Reduce(`*`, c(year1, rep(input1, 4L)))
. If you need the intermediate values, add the argumentaccumulate = TRUE
. And of course there’s a closed formula for compound interest:year1 * input1 ^ 4
. – Konrad Rudolph Commented Nov 26, 2024 at 15:38