尝试将单选按钮值传递给服务器时出现类错误

时间:2017-05-30 21:05:26

标签: html r rstudio shiny

我已经从我正在使用的代码中简化了下面的示例。我试图将用户从一组单选按钮中选择的值传递给进行的总分。我的代码工作正常,但现在我收到以下错误:

  

警告:as.character出错:无法强制键入'关闭'到#'字符'

的矢量      

堆栈跟踪(最里面的第一个):        1:runApp

     

错误:无法强制类型'关闭'到#'字符'

的矢量

听起来这是一个语法问题,但我想弄清楚这个问题。

UI.R

ui <- fluidPage(

  #title header
  titlePanel("This is My Form"),

  fluidRow(
    column(6,
           h3("BUTTON"),
           radioButtons("Button1","My First Button", choices = c("Yes" = "y","No" = "n","N/a" = "na"), selected = "na", inline = T),
           br(),
           h3("BUTTON 2 AND 3"),
           radioButtons("Button2","My Second Button", choices = c("Yes" = "y","No" = "n","N/a" = "na"), selected = "na", inline = T),
           br(),
           radioButtons("Button3","My Third Button", 
                        choices = c("Yes" = "y","No" = "n","N/a" = "na"), selected = "na", inline = T),
           br(),
           br()
    ),
    column(6,
           h3("BUTTON 4"),
           radioButtons("Button4","My Fourth Button", 
                        choices = c("Yes" = "y","No" = "n","N/a" = "na"), selected = "na", inline = T)
    )
  ),
  actionButton(inputId = "Submit", label = "Calculate"),
  (br),
  mainPanel(
    h1(textOutput('totals'), align = "center")
  )
)

SERVER.R

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

  button1 <- reactive({ ifelse(input$Button1 == "y", 50, ifelse(input$Button1 == "n", 25, 0)) })
  button2 <- reactive({ ifelse(input$Button2 == "y", 50, ifelse(input$Button2 == "n", 25, 0)) })
  button3 <- reactive({ ifelse(input$Button3 == "y", 50, ifelse(input$Button3 == "n", 25, 0)) })
  button4 <- reactive({ ifelse(input$Button4 == "y", 50, ifelse(input$Button4 == "n", 25, 0)) })

  output$totals <- renderText({

    if (input$Submit == 0)
      return(NULL)
    isolate({

      total <- as.numeric(Button1())+as.numeric(Button2())+as.numeric(Button3())+as.numeric(Button4())

      if (is.na(total)){
        print("Make Selections and Click Submit")
      } else 
        print(total)

    })  
  })
}

GLOBAL.R

library(shiny)
library(rsconnect)

1 个答案:

答案 0 :(得分:1)

您的用户界面中确实有拼写错误br() - &gt; actionButton(inputId = "Submit", label = "Calculate"), (br), mainPanel( h1(textOutput('totals'), align = "center") )

  actionButton(inputId = "Submit", label = "Calculate"),
  br(),
  mainPanel(
    h1(textOutput('totals'), align = "center")
  )

应该是

[10, 2, 3, 11, 4]