2
我有15选择(输入类型)字段。我需要将它传递给服务器功能做预测并显示结果输出。 我不想自动更新,当用户为一个输入字段设置值时,而是我希望用户为所有(15个输入字段)设置值,然后按某种类型的按钮以获取输出。从提交或动作按钮获取输入从Shiny UI应用程序到服务器
如何实现?这是我第一个闪亮的UI应用程序。
mycode的
library(shiny)
dataset <- diamonds
marks <- 0:100
grades <- c("A","B","C","D","E","F")
ui <- fluidPage(
tags$head(tags$style(HTML("
h2 {
text-align: center;
}
h3 {
text-align: center;
}
h6 {
text-align: center;
color:red;
}
#goButton
{
width: 100%;
}
")
)
),
verticalLayout
(
wellPanel
(
titlePanel("Get Recommendation for Year 4 or 5 Courses"),
h6("* Set the value of input field as 'NA', if you received a remark of Absent (ABS), Medical Circumstances (MC), Exemption (EX), Synoptic Course in absence (NC), Discretionary credits (DC), or any other reason")
)
),
fluidRow
(
column(2,
wellPanel(
radioButtons("type", label = h3("Select Type"),
choices = list("Grades" = 'grades', "Marks" = 'marks'),
selected = 'grades')
)
),
conditionalPanel
(
condition = "input.type == 'grades'",
column
(2,
wellPanel
(
h3("Year 1"),
selectInput('a', 'A',c('NA', grades)),
selectInput('b', 'B',c('NA', grades)),
selectInput('c', 'C',c('NA', grades)),
selectInput('d', 'D',c('NA', grades)),
selectInput('e', 'E',c('NA', grades))
)
),
column
(2,
wellPanel
(
h3("Year 2"),
selectInput('f', 'F',c('NA', grades)),
selectInput('g', 'G',c('NA', grades)),
selectInput('h', 'H',c('NA', grades)),
selectInput('i', 'I',c('NA', grades)),
selectInput('j', 'J',c('NA', grades))
)
),
column
(2,
wellPanel
(
h3("Year 3"),
selectInput('k', 'K',c('NA', grades)),
selectInput('l', 'L',c('NA', grades)),
selectInput('m', 'M',c('NA', grades)),
selectInput('n', 'N',c('NA', grades)),
selectInput('o', 'O',c('NA', grades))
)
)
),
conditionalPanel
(
condition = "input.type == 'marks'",
column
(2,
wellPanel
(
h3("Year 1"),
selectInput('a', 'A',c('NA', marks)),
selectInput('b', 'B',c('NA', marks)),
selectInput('c', 'C',c('NA', marks)),
selectInput('d', 'D',c('NA', marks)),
selectInput('e', 'E',c('NA', marks))
)
),
column
(2,
wellPanel
(
h3("Year 2"),
selectInput('f', 'F',c('NA', marks)),
selectInput('g', 'G',c('NA', marks)),
selectInput('h', 'H',c('NA', marks)),
selectInput('i', 'I',c('NA', marks)),
selectInput('j', 'J',c('NA', marks))
)
),
column
(2,
wellPanel
(
h3("Year 3"),
selectInput('k', 'K',c('NA', marks)),
selectInput('l', 'L',c('NA', marks)),
selectInput('m', 'M',c('NA', marks)),
selectInput('n', 'N',c('NA', marks)),
selectInput('o', 'O',c('NA', marks))
)
)
),
column
(4,
actionButton("goButton", "Submit"),
wellPanel
(
h3("Results"),
verbatimTextOutput("value")
)
)
)
)
server <- function(input, output)
{
#Do Prediction
#Get Results
new_vector = c()
if (input.type == 'marks'){
new_vector <- append(new_vector, input$f27sa, 1)
new_vector <- append(new_vector, input$f27sb, 2)
new_vector <- append(new_vector, input$f27cs, 3)
new_vector <- append(new_vector, input$f27is, 4)
new_vector <- append(new_vector, input$f27px, 5)
new_vector <- append(new_vector, input$f28in, 6)
new_vector <- append(new_vector, input$f28da, 7)
new_vector <- append(new_vector, input$f28pl, 8)
new_vector <- append(new_vector, input$f28sd, 9)
new_vector <- append(new_vector, input$f28dm, 10)
new_vector <- append(new_vector, input$f28ai, 11)
new_vector <- append(new_vector, input$f28fa, 12)
new_vector <- append(new_vector, input$f28fb, 13)
new_vector <- append(new_vector, input$f28oc, 14)
new_vector <- append(new_vector, input$f28pd, 15)
}else{
new_vector <- append(new_vector, input$f27sa2, 1)
new_vector <- append(new_vector, input$f27sb2, 2)
new_vector <- append(new_vector, input$f27cs2, 3)
new_vector <- append(new_vector, input$f27is2, 4)
new_vector <- append(new_vector, input$f27px2, 5)
new_vector <- append(new_vector, input$f28in2, 6)
new_vector <- append(new_vector, input$f28da2, 7)
new_vector <- append(new_vector, input$f28pl2, 8)
new_vector <- append(new_vector, input$f28sd2, 9)
new_vector <- append(new_vector, input$f28dm2, 10)
new_vector <- append(new_vector, input$f28ai2, 11)
new_vector <- append(new_vector, input$f28fa2, 12)
new_vector <- append(new_vector, input$f28fb2, 13)
new_vector <- append(new_vector, input$f28oc2, 14)
new_vector <- append(new_vector, input$f28pd2, 15)
}
results <- eventReactive(input$goButton,{
return (new_vector)
})
output$value <- renderPrint({ results() })
}
shinyApp(ui = ui, server = server)
我感谢您的帮助,一个简单的问题,之前我拉的输入值,并将其存储在一个载体,我需要找到,如果用户选择的类型为“等级”或“商标”,因为那时我知道是否使用输入$ f27sa或输入$ f27sa2。我该如何检查?我这样做:if(input.type =='marks'){...} else {...}但给出错误:object not found'input.type'。总之如何检查哪个单选按钮被选中。 –
让我看看。 –
我认为你需要使用“输入$类型”。 “。”字符在R中没有特别的意义 - 比如Javascript。这只是另一个角色。 $是R中的一个选择器,所以我认为这就是你的意思。 –