【问题标题】:How can I use a for loop in an R shiny app如何在 R 闪亮的应用程序中使用 for 循环
【发布时间】:2021-04-01 12:22:31
【问题描述】:

瞄准

我想在闪亮的应用程序中使用 for 循环。下面的代码可以在没有 for 循环的情况下工作,但随后无法使用它。

尝试

library(shiny)
library(dplyr)
library(lubridate)

#create table
dates <- seq(as.Date("2020-01-01"), as.Date("2022-04-01"), by="months")
supply <- floor(runif(28, min = 80000, max = 100000))
demand <- rep(0, length(dates))
shortfall <- ifelse(demand - supply > 0, demand -supply, 0)
df <- bind_cols(dates, supply, demand, shortfall)
names(df) <- c("dates", "supply", "demand", "shortfall")

# Define UI ----
ui <- navbarPage(
  
  titlePanel("Supply & Demand"),
  
  tabPanel(title = "Demand", 
           mainPanel(h3("Demand", align = "center"), 
                     plotOutput(outputId = "demand")), 
           sidebarPanel(sliderInput(inputId = "A", label = "A", min = 0, max = 50000, value = 5000), 
                        sliderInput(inputId = "B", label = "B", min = 0, max = 50000, value = 5000), 
                        sliderInput(inputId = "C", label = "C", min = 0, max = 50000, value = 5000))), 
  

  tabPanel(title = "Table", 
           mainPanel(h3("Table", align = "center"), 
                     tableOutput(outputId = "table")))
  
)

# Define server logic ----
server <- function(input, output) {

  df_ <- reactive({
    df %>% mutate(demand = input$A + input$B + input$C, 
                  shortfall = rep(0, 28),
                  returning_shortfall = c(10000, rep(0, 27)),
                  stored_shortfall = rep(0, 28),
                  total_demand = rep(0, 28))
  })
  
  df2_ <- reactive({
    
  for (i in 2:length(df_()$dates)) {
    df_()$total_demand[i] = df_()$demand[i] + df_()$returning_shortfall[i-1]
    df_()$shortfall[i] = ifelse(df_()$total_demand[i] - df_()$supply[i] > 0, df_()$total_demand[i] - df_()$supply[i], 0)
    df_()$returning_shortfall[i] = df_()$shortfall[i-1] * 0.15
    df_()$stored_shortfall[i] = df_()$stored_shortfall[i-1] + df_()$shortfall[i] * 0.15
  }
    
  })
  
  #Table
  output$table <- renderTable(df2_())

}
  
# Run the app ----
shinyApp(ui = ui, server = server)

错误信息

Warning: Error in =: invalid (NULL) left side of assignment
  113: <reactive:df2_> [C:/Users/user/filename]
   97: df2_
   96: renderTable
   95: func
   82: renderFunc
   81: output$table
    1: runApp

寻求结果

主要挑战是创建一个列,其中每个值都依赖于前一行的值。 “for 循环”似乎解决了这个问题,但它在闪亮时不起作用。

【问题讨论】:

  • 您不能访问反应性块(例如*reactiveobserve*render*等)之外的反应性组件(例如df_)。在df_ 初始定义reactive 中添加您的逻辑,或者创建第二个使用df_() 并创建新框架的反应块(例如,df2_ &lt;- reactive(...))。
  • 感谢 r2evans,我已将 for 循环放入第二个响应式循环中,现在应用程序已加载。但是,我现在收到错误消息:“分配的无效(NULL)左侧”而不是显示的表格。有什么想法吗?
  • 你这个闪亮的应用程序没有更新,所以它可能是很多事情。请使用当前代码更新您的问题。谢谢!

标签: r for-loop shiny


【解决方案1】:

你只需要一个反应对象。试试这个

library(shiny)
library(dplyr)
library(lubridate)

#create table
dates <- seq(as.Date("2020-01-01"), as.Date("2022-04-01"), by="months")
supply <- floor(runif(28, min = 8000, max = 20000))
demand <- rep(0, length(dates))
shortfall <- ifelse(demand - supply > 0, demand -supply, 0)
df1 <- bind_cols(dates, supply, demand, shortfall)
names(df1) <- c("dates", "supply", "demand", "shortfall")

# Define UI ----
ui <- navbarPage(
  
  titlePanel("Supply & Demand"),
  
  tabPanel(title = "Demand", 
           mainPanel(h3("Demand", align = "center"), 
                     plotOutput(outputId = "demand")), 
           sidebarPanel(sliderInput(inputId = "A", label = "A", min = 0, max = 50000, value = 5000), 
                        sliderInput(inputId = "B", label = "B", min = 0, max = 50000, value = 5000), 
                        sliderInput(inputId = "C", label = "C", min = 0, max = 50000, value = 5000))), 
  
  
  tabPanel(title = "Table", 
           mainPanel(h3("Table", align = "center"), 
                     tableOutput(outputId = "table")))
  
)

# Define server logic ----
server <- function(input, output) {
  
  
  df2 <- reactive({
    
    df <- df1 %>% mutate(demand = input$A + input$B + input$C, 
                  shortfall = rep(0, 28),
                  returning_shortfall = c(10000, rep(0, 27)),
                  stored_shortfall = rep(0, 28),
                  total_demand = rep(0, 28))
    
    for (i in 2:length(df$dates)) {
      df$total_demand[i] <- df$demand[i] + df$returning_shortfall[i-1]
      df$shortfall[i] <- ifelse(df$total_demand[i] - df$supply[i] > 0, df$total_demand[i] - df$supply[i], 0)
      df$returning_shortfall[i] <- df$shortfall[i-1] * 0.15
      df$stored_shortfall[i] <- df$stored_shortfall[i-1] + df$shortfall[i] * 0.15
    }
    df
  })
  
  #Table
  output$table <- renderTable(df2())
  
}

# Run the app ----
shinyApp(ui = ui, server = server)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-25
    • 2018-02-16
    • 1970-01-01
    • 1970-01-01
    • 2021-01-19
    • 2016-10-23
    • 2022-01-26
    相关资源
    最近更新 更多