【问题标题】:Why Is my R Shiny app not displaying properly?为什么我的 R Shiny 应用程序无法正常显示?
【发布时间】:2022-02-26 09:57:12
【问题描述】:

我显然在这里遗漏了一些东西,但我对 Shiny 应用程序还很陌生(我之前只做过几个),而且我仍在学习它们的原理。

此应用(将自行运行)适用于输入端(滑块和文本输入),但输出(应该是表格)不会显示。

代码如下:

# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
#
# Find out more about building applications with Shiny here:
#
#    http://shiny.rstudio.com/
#

library(shiny)

ui <- fluidPage(

    # Application title
    titlePanel("CHD Risk Calculator"),

    sidebarLayout(
        sidebarPanel(
            sliderInput("BMI",
                        "Your BMI (kg/m^2 OR (703*lbs)/in^2):",
                        min = 10,
                        max = 70,
                        value = 24),
            textInput("Age",
                      "Your Age:")
        ),

        mainPanel(
           tableOutput("")
        )
    )
)

server <- function(input, output) {

    inputdata <- reactive({
        data <- data.frame(
            MyBMI = as.integer(input$BMI),
            MyAge = as.integer(input$age))
        data
    })
    
    output$result <- renderTable({
        data = inputdata()
        chdrisk = -6.293 + (0.0292*data$BMI) + (0.07409*data$age)
        resultTable = data.frame(
            Result = "Your risk of Coronary Heart Disease (CHD) is",
            Risk = chdrisk)
        resultTable
        
    })
}

# Run the application 
shinyApp(ui = ui, server = server)

我在这里错过了什么?

谢谢!

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    这里发生了一些事情

    1. 您的tableOutput() 已分配给outputID="";将此更改为"result"
    2. 滑块和文本的输入称为 BMI 和年龄,但在反应式中,您将它们称为 BMI 和年龄
    3. reactive 中的数据框有两列,MyBMIMyAge,但后来你这样引用它们:data$BMIdata$age

    这是一个修正版

    # This is a Shiny web application. You can run the application by clicking
    # the 'Run App' button above.
    #
    # Find out more about building applications with Shiny here:
    #
    #    http://shiny.rstudio.com/
    #
    
    library(shiny)
    
    ui <- fluidPage(
      
      # Application title
      titlePanel("CHD Risk Calculator"),
      
      sidebarLayout(
        sidebarPanel(
          sliderInput("BMI",
                      "Your BMI (kg/m^2 OR (703*lbs)/in^2):",
                      min = 10,
                      max = 70,
                      value = 24),
          textInput("Age",
                    "Your Age:")
        ),
        
        mainPanel(
          tableOutput("result")
        )
      )
    )
    
    server <- function(input, output) {
      
      inputdata <- reactive({
        data <- data.frame(
          MyBMI = as.integer(input$BMI),
          MyAge = as.integer(input$Age))
        data
      })
      
      output$result <- renderTable({
        data = inputdata()
        chdrisk = -6.293 + (0.0292*data$MyBMI) + (0.07409*data$MyAge)
        resultTable = data.frame(
          Result = "Your risk of Coronary Heart Disease (CHD) is",
          Risk = chdrisk)
        resultTable
        
      })
    }
    
    # Run the application 
    shinyApp(ui = ui, server = server)
    

    【讨论】:

    • 非常感谢!我非常感谢。发布此内容后,我注意到您的第 2 点和第 3 点中的命名问题,但我很高兴它们是正确的!您的第 1 点是我根本没有注意到的,我只是没有按照我正确使用的教程进行操作。现在,我必须修复函数产生的数字,但它可以工作,所以,再次感谢您!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-24
    • 2020-10-13
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    • 2021-10-05
    相关资源
    最近更新 更多