r - 尝试从 Shiny 中的 checkboxGroupInput 中提取单个值
问题描述
我对 Shiny 和 R 很陌生,我遇到了问题。我正在编写一个应用程序,它读取一个 csv 文件,其中包含运动员姓名列表及其所有收集的信息,即速度、身高、体重等。
我正在尝试允许更新该信息。因此,我创建了一个向量,其中包含所选运动员行中的 5 个列值。这些信息是他们的姓名、年级、运动、教练和入学日期。我有 checkBoxGroupInput 工作。问题是一旦我从复选框中获得了返回值,我就无法拆分信息以便能够从数据框中选择该特定行进行更新。
所以这里有一些重要的代码:
ui <- fluid panel
#other code is here that is working. Just focusing on this code for this issue
tabPanel(title = "Update Info", value = "updateAth",
tags$h3("Update Athlete's Information"),
sidebarLayout(
sidebarPanel(
selectizeInput("updtAth","Choose Ahtlete",choices = unique(athletes$ATHLETE),options = list(placeholder = '', onInitialize = I('function() {this.setValue("");}'))),
br(),
#action button to select the patient
actionButton("selectAth","Select"),
br(),
uiOutput("nextSteps")
), #end sidebarPanel
mainPanel(
uiOutput("moreSteps"),
) #end main panel
)
)
)
server <- function(input,output,session) {
observeEvent(input$selectAth, {
athDF <- data.frame(subset.data.frame(athletes,athletes$ATHLETE == input$updtAth, select = c("CLASSMAN","SPORT","COACH","DATE")))
rws <- nrow(athDF)
values <- vector(length = rws)
#create the list of choices in a for loop
for (i in 1:rws) {
values[i] <- paste(input$updtAth,athDF[i,1],athDF[i,2],athDF[i,3],athDF[i,4])
} # end for loop
output$nextSteps <- renderUI({
tagList(
br(),
checkboxGroupInput("row","Choose the row to update",choices = as.list(values)),
br(),
actionButton("updateRow","Update Row")
) #end tagList
}) # end output renderUI
################ this is the first thing I tried and I get this as my output -- "this is it NA Harry, Barry"
rowToString <- renderText(input$row)
updtclass <- rowToString()[2] #this value should be 3
#####################When I try this I get this as my output --- "this is it Dolphin, Barry "
updtclass <- input$row[2] #this value should be 4
output$text <- renderText({paste("this is the it", updtclass," ",input$updtAth)})
observeEvent(input$updateRow, {
output$moreSteps <- renderUI({
fluidRow (
#dataTableOutput('getTable'),
textOutput('text'),
br(),
tags$h3("Only fill in the attributes that need to be changed"),
column(3,
textInput("upDtht","Height",value = NULL,width = '100px', placeholder = "in inches"),
textInput("upDtwt","Weight", value = NULL, width = '100px'),
textInput("upDtsport","Sport", value = NULL, width = '100px'),
textInput("upDtspeed","Speed", value = NULL, width = '100px'),
textInput("upDtagility","Agility", value = NULL, width = '100px')
), #end column
column(
4,offset = -1,
textInput("upDtcmj","CMJ",value = NULL, width = '100px'),
textInput("upDtsj","SJ",value = NULL, width = '100px'),
textInput("upDtnotes","Notes", value = NULL, width = '800px'),
textInput("upDtdate","Date", value = format(Sys.Date(),format="%m/%d/%Y"), width = '100px'),
br(),
actionButton("update","Update")
), #end next column
) #end fluid row
}) # end renderUI moreSteps
}) #end observe Event updateRow
observeEvent(input$update, {
#i'll work this out later
) #end observe Event update
} #end work inside first observeEvent
) #end observeEvent update info
}
我只是不知道如何从复选框中提取每个值。复选框中的数据将返回的示例是:
“哈利,巴里”“3”“篮球”“琼斯教练”“2019 年 7 月 15 日”
我的目标是提取每个单独的值并将其放入一个变量中,用于将特定行从数据框中拉出,这样我就可以让最终用户更新它。运动员 = 哈利,巴里,学年 = 3,运动 = 篮球,教练 = 琼斯教练,更新日期 = 07/15/2019
我希望这是有道理的。
解决方案
input$checkBoxGroupInput[1]
会给你第一个,input$checkBoxGroupInput[2]
第二个等等。总之, a 的输出checkBoxGroupInput
是一个向量。
这是一个最小的例子:
library(shiny)
ui <- fluidPage(
checkboxGroupInput("checkboxGroupInput","checkboxGroupInput", choices = c(1, 2, 3))
)
server <- function(input, output, session) {
#Runs when checkboxGroupInput is changed
observeEvent(input$checkboxGroupInput, {
#Clear console
cat("\014")
#Print the selection(s) of checkboxGroupInput to the console
print(paste('1st value:', input$checkboxGroupInput[1]))
print(paste('2nd value:', input$checkboxGroupInput[2]))
print(paste('3rd value:', input$checkboxGroupInput[3]))
})
}
shinyApp(ui, server)
我无法运行您的代码,但在这种情况下,听起来 acheckBoxGroupInput
可能不是正确的输入。也许一系列下拉框会更有用。
推荐阅读
- vue.js - Vue CLI:将默认样式语言设置为 scss
- ubuntu - 调用 freeze_bdev() 会导致内核错误
- python - setup.py:添加安装所需的依赖项
- docker - 是否有 `docker up` 命令,例如 `vagrant up`?
- powershell - 在for循环中循环多个变量
- javascript - ASP.NET MVC 控制器在返回响应之前等待模型
- puppet - 在 Puppet 中循环多个变量
- angular - Angular6 提交表单的 iframe
- linux - Python - 处理图像时 Spyder 上的 CV2 奇怪行为
- ember.js - 什么是灰烬金属?