【问题标题】:why is my shiny application refuses to render?为什么我的闪亮应用程序拒绝呈现?
【发布时间】:2023-03-15 21:21:01
【问题描述】:

我闪亮的应用程序不起作用,我不知道为什么: 这会尝试获取输入并计算输出并渲染结果 在输出 $mortgagepayment。 当我运行应用程序时,它会给我以下消息:

.getReactiveEnvironment()$currentContext() 中的错误: 如果没有活动的反应上下文,则不允许操作。 (你试图做一些只能在反应式表达式或观察者内部完成的事情。)

library(shiny)

# Define UI ----
ui <- fluidPage(
    titlePanel("Basic widgets"),

     fluidRow(

         # column(3,
         #       h3("Buttons"),
         #        actionButton("action", "Action"),
         #        br(),
         #        br(),
         #        submitButton("Submit")),

         # column(3,
         #        h3("Single checkbox"),
         #        checkboxInput("checkbox", "Choice A", value = TRUE)),

         column(3,
                checkboxGroupInput("Fixed",
                                   h3("Mortgage Year"),
                                   choices = list("30 year" = 30,
                                                  "15 year" = 15,
                                                  "10 year" = 10),
                                   selected = 1)),

         # column(3,
         #       dateInput("date",
         #                 h3("Date input"),
         #                  value = "2014-01-01"))
     ),

    fluidRow(

        # column(3,
        #        dateRangeInput("dates", h3("Date range"))),
        # 
        # column(3,
        #        fileInput("file", h3("File input"))),



        column(3, 
               numericInput("housePrice", 
                            h3("Housing Price"), 
                            value = 1)),   


        column(3, 
               numericInput("percentageDown", 
                            h3("Percentage Down"), 
                            value = 1),min=0,max=1) ,  

        column(3, numericInput("mortgageYield", h3("Mortgage Yield"), value=0.0, min = 0, max = 0.1, step = NA,
                              width = NULL)),
        ),

    fluidRow(
        column(3, h3("Mortgage Payment"), verbatimTextOutput("mortgagepayment")))
)



# Define server logic ----
server <- function(input, output) {
   housePrice = input$housePrice
   downPayment = input$percentageDown    
   mortgageYield = input$mortgageYield 
   mortgageAmount = housePrice*(1-downPayment)
   years = session$fixed
   mortgagePayment = (mortgageAmount*mortgageYield)/(1-1/(1+mortgageYield)^(12*years))
   output$mortgagepayment <-renderText({mortgagePayment})
   }

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

【问题讨论】:

    标签: r shiny rendering shinyapps


    【解决方案1】:

    有几处需要纠正。

    1. 所有对input$... 变量的引用都必须在反应组件内;在这种情况下,它可能会在renderText(...) 内。

    2. session$fixed 错了,你的意思可能是input$fixed

    3. 您声明checkboxGroupInput("Fixed",...)(大写“F”)但引用...$fixed(小写“f”),R 区分大小写。

    4. 你尝试直接使用复选框的值,但复选框的值是字符串;您需要使用years &lt;- as.numeric(input$fixed)(假设您继续将复选框选择有意义的数字命名为趋势)。

    您当前的默认驱动器为 0 的付款(即,percentageDown=1 表示全额为首付,因此无需任何欠款)。

    我认为你的服务器组件应该是这样的:

    server <- function(input, output) {
      output$mortgagepayment <- renderText({
        # prevent this block from trying to calculate when other fields are
        # empty or invalid
        req(input$housePrice, input$percentageDown, input$mortgageYield, input$Fixed)
        # message("calculate!") # just advises when this block fires
        housePrice = input$housePrice
        downPayment = input$percentageDown    
        mortgageYield = input$mortgageYield 
        mortgageAmount = housePrice*(1-downPayment)
        years = as.numeric(input$Fixed)
        mortgagePayment = (mortgageAmount*mortgageYield)/(1-1/(1+mortgageYield)^(12*years))
        mortgagePayment
      })
    }
    

    【讨论】:

      猜你喜欢
      • 2018-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多