简体   繁体   English

R - Shiny - 将checkboxInput转换为数字

[英]R - Shiny - convert checkboxInput to numeric

I'm learning & developing a little shiny app. 我正在学习和开发一个有点闪亮的应用程序。 Awesome stuff! 太棒了!

I have a checkboxInput that returns true/false, I need to convert it to numeric 1/0 and use it in a formula inside a table. 我有一个checkboxInput返回true / false,我需要将它转换为数字1/0并在表格内的公式中使用它。 My real-life example is somewhat more convoluted, but I have extracted a stackoverflow-life example below. 我的现实生活中的例子有点复杂,但我在下面提取了一个stackoverflow-life示例。 Help appreciated! 帮助赞赏!

In the app below, the user first selects the checkboxInput to play, and then inputs a number in the left panel. 在下面的应用程序中,用户首先选择要播放的checkboxInput,然后在左侧面板中输入一个数字。 The main panel displays a table of the input number, the output number (the square of the input in this example) and the "status" of the checkboxInput, which is either True or False. 主面板显示输入编号表,输出编号(本例中输入的平方)和checkboxInput的“status”,可以是True或False。

I'd like to return 1 or 0 instead of True or False. 我想返回1或0而不是True或False。

In real life, my left panel takes a list of numbers as well as True/False statements, from which a formula returns a number to be displayed in the table. 在现实生活中,我的左侧面板采用数字列表以及True / False语句,公式从中返回要在表格中显示的数字。 The True/False statements are like dummy variables. True / False语句就像虚拟变量。

Here is the ui.R 这是ui.R

# ui.R
library("shiny")
# Define UI for slider demo application
shinyUI(
  pageWithSidebar(
    # Title
    headerPanel("Input-Output Shiny App")
     ,
     # User Input in sidebar panel
     sidebarPanel(
      # Yes/No
      wellPanel(
        checkboxInput("play", "Do you want to play?", FALSE)
         , 
         conditionalPanel(
           condition="input.play==true"
          , 
          numericInput("myinput", "My Input:", 0)
         )#end conditionalPanel
       )#end wellPanel
    )# end sidebarPanel
    ,
    # Output in main panel
    # See output$myResults in server.R
    mainPanel(
      tableOutput("myResults")
    )# end mainPanel
  )#end pageWithSidebar
)#end shinyUI 

And here is the server.R 这是服务器.R

# server.R
library("shiny")
shinyServer(
  function(input, output) {
    myFunction <- function(myinput)
      {
      # compute output
      myoutput <- myinput*myinput
      myoutput
      }
    # Show computed values
    output$myResults <- renderTable(
      {
        r1 <- c("My Input",input$myinput)
        r2 <- c("My Output",myFunction(input$myinput))
        r3 <- c("Are you playing?",input$play)
        myTable <- do.call(rbind, list(r1,r2,r3))
        myTable
      }
      , align = c("lcc")
      , include.rownames = FALSE
      , include.colnames = FALSE
      , sanitize.text.function = function(x) x
    )#end renderTable         
  }#end function(input,output)
)#end shinyServer

Here's a screenshot: 这是一个截图:

在此输入图像描述

as.integer happens to convert TRUE and FALSE to 1 and 0, respectively. as.integer分别将TRUEFALSE转换为1和0。

More generally, you could use an if or ifelse function to accomplish this. 更一般地说,您可以使用ififelse函数来完成此任务。

play <- TRUE # or FALSE
r3 <- ifelse(play, "You bet!", "No way!")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM