r - R/Shiny - 将复选框标签转移到功能字段
问题描述
我需要选中的复选框标签来填写“[,c("checkbox1","checkbox2")]" 字段(其中写有“checkbox1”和“checkbox2”)。谢谢你。
这是我的代码。
library(shiny)
library(shinydashboard)
library(shinyWidgets)
library(readxl)
library(tidyverse)
library(readxl)
library(stringr)
ui <- fluidPage(
br(),
fileInput("archive", "Upload file", accept = c(
".xlsx")),
# Itens Selection
checkboxGroupInput("additem", "Select Items", choices = NULL)
)
box(
width = 2000,
verbatimTextOutput("calfa")
)
server <- function(input, output, session) {
# Upload Data Sheet
csv <- reactive({
req(input$archive)
inFile <- input$archive
df <- read_xlsx(inFile$datapath)
return(df)
})
#reactive value that will hold the name of the file
reactive_my_path <- reactive({
# Test if file is selected
req(input$archive)
return(sub(".xlsx$", "", basename(input$archive$name)))
})
observe({
input$archive
# update the choices in input$additem
updateCheckboxGroupButtons(session,
"additem",
paste('Column names in:', reactive_my_path()),
choices = names(csv()))
})
# Alpha
output$calfa <-
renderPrint({
int<-csv()[,c("checkbox1","checkbox2")]
int <- na.omit(int)
psych::alpha(int, check.keys = TRUE)
})
}
# App
shinyApp(ui, server)
解决方案
您可以使用input$additem
包含所有选中的复选框。此外,我还包含了另一个条件,psych::alpha
仅当有多个列时才运行代码。
library(shiny)
library(shinydashboard)
library(shinyWidgets)
library(readxl)
library(tidyverse)
library(readxl)
library(stringr)
ui <- fluidPage(
br(),
fileInput("archive", "Upload file", accept = c(
".xlsx")),
# Itens Selection
checkboxGroupInput("additem", "Select Items", choices = NULL),
box(
width = 2000,
verbatimTextOutput("calfa")
)
)
server <- function(input, output, session) {
# Upload Data Sheet
csv <- reactive({
req(input$archive)
inFile <- input$archive
df <- read_xlsx(inFile$datapath)
return(df)
})
#reactive value that will hold the name of the file
reactive_my_path <- reactive({
# Test if file is selected
req(input$archive)
return(sub(".xlsx$", "", basename(input$archive$name)))
})
observe({
input$archive
# update the choices in input$additem
updateCheckboxGroupButtons(session,
"additem",
paste('Column names in:', reactive_my_path()),
choices = names(csv()))
})
# Alpha
output$calfa <-
renderPrint({
req(length(input$additem) > 1)
int<-csv()[,input$additem]
int <- na.omit(int)
psych::alpha(int, check.keys = TRUE)
})
}
# App
shinyApp(ui, server)
推荐阅读
- php - CakePHP 3.8.0 中使用 AJAX 的依赖(链式)下拉菜单
- c# - 是否可以使用别名映射到枚举?
- visual-studio-2015 - EF5 代码优先迁移是否可以在 Visual Studio 之外搭建?
- react-native - React Native 和原生基础编辑数据页面
- android - Maven:com.android.support:support-annotations:28.0.0 的依赖收敛错误
- sails.js - 无法获取新创建的记录sails js的id
- html - 为什么我不能将鼠标悬停在
- c++ - 如果删除了未使用的 ref 类,则 DLL 无法加载
- javascript - 仅当我从项目文件夹中删除 all.js 时,字体真棒图标才会变成正方形
- unix - 如何使用 awk 或 sed 根据分隔符复制列并拆分另一列?