Pat*_*ckT 1 binary r numeric shiny
我正在学习和开发一个闪亮的小应用程序。太棒了!
我有一个返回 true/false 的 checkboxInput,我需要将其转换为数字 1/0 并在表内的公式中使用它。我的现实生活示例有点复杂,但我在下面提取了一个 stackoverflow-life 示例。帮助表示感谢!
在下面的应用程序中,用户首先选择要播放的 checkboxInput,然后在左侧面板中输入一个数字。主面板显示输入数字、输出数字(本例中输入的平方)和 checkboxInput 的“状态”(为 True 或 False)的表格。
我想返回 1 或 0 而不是 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
Run Code Online (Sandbox Code Playgroud)
这是服务器。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
Run Code Online (Sandbox Code Playgroud)
这是一个屏幕截图:

as.integer恰好将TRUE和FALSE分别转换为 1 和 0。
更一般地,您可以使用iforifelse函数来完成此操作。
play <- TRUE # or FALSE
r3 <- ifelse(play, "You bet!", "No way!")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2647 次 |
| 最近记录: |