[英]R - Shiny - convert checkboxInput to numeric
我正在学习和开发一个有点闪亮的应用程序。 太棒了!
我有一个checkboxInput返回true / false,我需要将它转换为数字1/0并在表格内的公式中使用它。 我的现实生活中的例子有点复杂,但我在下面提取了一个stackoverflow-life示例。 帮助赞赏!
在下面的应用程序中,用户首先选择要播放的checkboxInput,然后在左侧面板中输入一个数字。 主面板显示输入编号表,输出编号(本例中输入的平方)和checkboxInput的“status”,可以是True或False。
我想返回1或0而不是True或False。
在现实生活中,我的左侧面板采用数字列表以及True / False语句,公式从中返回要在表格中显示的数字。 True / False语句就像虚拟变量。
这是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
这是服务器.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
这是一个截图:
as.integer
分别将TRUE
和FALSE
转换为1和0。
更一般地说,您可以使用if
或ifelse
函数来完成此任务。
play <- TRUE # or FALSE
r3 <- ifelse(play, "You bet!", "No way!")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.