【问题标题】:R: Using formula objects in Shiny to retrieve descriptive statisticsR:在 Shiny 中使用公式对象来检索描述性统计数据
【发布时间】:2019-10-29 23:50:08
【问题描述】:

我正在尝试使用 Shiny 构建一个响应式应用程序,以使用 R 包 onewaytests 检索描述性统计信息。

选择要在描述性统计中使用的列变量后,我无法成功使用它们来创建公式对象,例如describe(parktime ~ parkstop, test)。我试过renderDataTablerenderTablerenderTextrenderUI。没有任何效果,我收到错误消息:

警告:描述错误:组变量的名称不匹配 数据中的变量名称。组变量必须是一个因素。 [没有可用的堆栈跟踪]

这是我的错误代码:

library(shiny)
library(onewaytests)

# create test data
test <- data.frame("likert" = c(1,1,1,3,1,3,2,2,1,4,2,2,3,3,4,4,5,5), 
                   "parkspot" = c(1,1,1,4,1,2,1,3,1,4,3,3,2,2,1,1,2,4), 
                   "parktime" = c(5,10,5,13,5,1,10,5,1,15,2,43,2,3,4,2,11,1),
                   "walktime" = c(5,5,30,5,3,1,10,5,5,5,5,12,11,2,3,4,4,3))

# likert and parkspot to factor
test[, 1] <- as.factor(test[, 1])
test[, 2] <- as.factor(test[, 2])

# give names
levels(test$likert) <- list("Extremely familiar" = 1,
                            "Moderately familiar" = 2,
                            "Somewhat familiar" = 3,
                            "Slightly familiar" = 4,
                            "Not at all familiar" = 5)

levels(test$parkspot) <- list("On the side of street" = 1,
                              "Parking lot" = 2,
                              "Parking garage" = 3,
                              "Other" = 4)

# This runs fine outside of shinyApp
describe(walktime ~ parkspot, test)

# shinyApp code
server <- function(input, output){

  # some unsuccessful tests
  output$test1 <- renderTable({
    describe(input$resp ~ input$expl, test)
  })

  output$test2 <- renderText({
    paste(input$resp,
          input$expl)
  })

  output$test3 <- renderDataTable({
    describe(input$resp ~ input$expl, test)
  })
}

ui <- shinyUI(fluidPage(

  titlePanel("thing"),

  sidebarLayout(
    sidebarPanel(

      #walktime or parktime
      selectInput("resp", 
                  "response (continuous)", 
                  names(test[-c(1,2)])),

      # all others
      selectInput("expl", 
                  "explanatory (ordinal)", 
                  names(test[-c(3,4)]))
    ),

    mainPanel(
      tableOutput("test1"),
      verbatimTextOutput("test2"),
      dataTableOutput("test3")
    )
  )
))

shinyApp(ui = ui, server = server, options = list("test.mode"))

有可能吗?看起来 describe() 在应用程序内失败。在 Shiny 应用程序之外运行 onewaytests 的 describe() 完全正常,如下所示:

                      n     Mean  Std.Dev Median Min Max 25th 75th   Skewness Kurtosis NA
On the side of street 8 8.125000 9.109453      5   3  30 3.75 6.25  2.0290427 5.475618  0
Parking lot           4 4.500000 4.509250      3   1  11 1.75 5.75  0.9067530 2.122279  0
Parking garage        3 7.333333 4.041452      5   5  12 5.00 8.50  0.7071068 1.500000  0
Other                 3 4.333333 1.154701      5   3   5 4.00 5.00 -0.7071068 1.500000  0

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    您需要从input 中获取respexpl 的字符串并创建一个公式:

    describe(as.formula(paste(input$resp, '~', input$expl)), test)
    

    【讨论】:

    • 谢谢本。该应用程序适用于此修改。
    猜你喜欢
    • 2021-09-08
    • 1970-01-01
    • 2018-10-13
    • 1970-01-01
    • 2021-12-13
    • 1970-01-01
    • 1970-01-01
    • 2021-12-22
    • 1970-01-01
    相关资源
    最近更新 更多