【问题标题】:R Shiny: Error in max - min : non-numeric argument to binary operatorR Shiny:最大 - 最小错误:二元运算符的非数字参数
【发布时间】:2015-10-09 08:39:13
【问题描述】:

我正在尝试创建一个简单的闪亮应用程序,用于在 Amazon ec2 上运行的 rstudio 内绘制直方图,以将结果数据存储为 .RData 文件。我已经仔细检查了一切。看起来不错。 我还检查了数据类型:都是数字。

我的代码:

ui.R

    # shiny plots on top_docs data
library(shiny)
shinyUI(fluidPage(
 # Header title
  titlePanel(title = h4("Top doctors plots - a histograms", align = "center")),

  # Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
sliderInput(
selectInput("var", "1, Select the variables from top doc summary file", 
choices =c( "service_total" = 1, 
              "ben_total" = 2,
              "payment" = 3, 
              "charged" = 4, 
              "allowed" = 5, 
              "unique_services_per_patient" = 6, 
              "duplicates_per_service" = 7, 
              "services_per_patient" = 8), selected= 1 ),
        br(),
        sliderInput("bins", "2, Select the number of BINs for histogram", min = 5, max = 40, value=20),
        br(),
        radioButtons("color", "3, Select the color of histogram", choices =c("Green", "Red", "Yellow"), selected= "Green")

    )),

    # Show a plot of the generated distribution
    mainPanel(
      plotOutput("myhist")
    )
  ))
)

server.R

`library(ggplot2)
library(shiny)
options(shiny.error=browser)

load("/******/top_docs.RData", envir=.GlobalEnv)
shinyServer(function(input, output) {

  output$myhist <- renderPlot({

    # generate var based on input$var from ui.R
    col <- as.numeric(input$var)
    hist(top_docs[,col], breaks = seq(0, max(top_docs[,col], l = input$bins+1), col=input$color, main="Histogram of Top docs", xlab=names(top_docs[col])))

  })

})
`

【问题讨论】:

  • 无法重现示例。你应该放一些数据样本而不是load(".RData")。无论如何,seq(0, max(top_docs[,col], l = input$bins+1) 中可能出现错误,其中 max() 函数缺少“)”
  • 嗨,我纠正了缺失的 ")" 运算符,但我仍然收到同样的错误。
  • 样本数据:可取top_docs = data.frame( service_total = sample(1:100000, 40, replace=T), ben_total = sample(1:100000, 40, replace=T), payment = sample(1:100000, 40, replace=T), charged = sample(1:100000, 40, replace=T), allowed = sample(1:100000, 40, replace=T), unique_services_per_patient = runif(40, 1, 5.2), duplicates_per_service = runif(40, 1.1, 16.2), services_per_patient = runif(40, 1.3, 70) )

标签: r amazon-ec2 shiny shiny-server


【解决方案1】:

找到了...所有sidebarPanel 内容都在sliderInput 函数中,该函数给出错误

top_docs = data.frame( service_total = sample(1:100000, 40, replace=T),
                       ben_total = sample(1:100000, 40, replace=T)
                       , payment = sample(1:100000, 40, replace=T), charged = sample(1:100000, 40, replace=T)
                       , allowed = sample(1:100000, 40, replace=T), unique_services_per_patient = runif(40, 1, 5.2)
                       , duplicates_per_service = runif(40, 1.1, 16.2), services_per_patient = runif(40, 1.3, 70) )

# shiny plots on top_docs data
library(shiny)
ui <- fluidPage(
  # Header title
  titlePanel(title = h4("Top doctors plots - a histograms", align = "center")),

  # Sidebar with a slider input for number of bins
  sidebarLayout(
    sidebarPanel(
      # sliderInput(            < -------------  HERE
        selectInput("var", "1, Select the variables from top doc summary file", 
                    choices =c( "service_total" = 1, 
                                "ben_total" = 2,
                                "payment" = 3, 
                                "charged" = 4, 
                                "allowed" = 5, 
                                "unique_services_per_patient" = 6, 
                                "duplicates_per_service" = 7, 
                                "services_per_patient" = 8), selected= 1 ),
        br(),
        sliderInput("bins", "2, Select the number of BINs for histogram", min = 5, max = 40, value=20),
        br(),
        radioButtons("color", "3, Select the color of histogram", choices =c("Green", "Red", "Yellow"), selected= "Green")

      # )            < -------------  HERE
    ),

    # Show a plot of the generated distribution
    mainPanel(
      plotOutput("myhist")
    )
  ))

server <- shinyServer(function(input, output) {

  output$myhist <- renderPlot({

    # generate var based on input$var from ui.R
    col <- as.numeric(input$var)
    hist(top_docs[,col], breaks = seq(0, max(top_docs[,col]), l = input$bins+1)
                                      , col=input$color, main="Histogram of Top docs", xlab=names(top_docs[col]))

  })

})

shinyApp(ui, server)

【讨论】:

  • 非常感谢@Andriy Tkachenko,现在一切正常!
  • 很高兴你帮到你!您可以将答案标记为解决方案,这样问题就不会显示为未解决
猜你喜欢
  • 2015-08-16
  • 2020-09-15
  • 2018-01-20
  • 2016-07-23
  • 2016-11-15
  • 1970-01-01
  • 2021-03-24
  • 1970-01-01
相关资源
最近更新 更多