r - R/Shiny 动态复选框和滑块生成数据框
问题描述
我正在学习 Shiny 并开发一个用于多元线性回归的小应用程序。但是,我遇到了一些障碍,可以使用一些专家指导。这是我正在尝试做的事情:
能够导入具有不同列数的 .csv 文件作为预测变量
动态生成复选框以允许用户选择他们想要包含的变量
为每个选定的变量生成滑块(目的是“假设”模拟。用于生成回归模型的数据是从 CSV 文件中提取的。)
根据滑块生成的值生成数据框。
我一直在从其他帖子中借用很多代码,但我仍然无法让 4 号工作。这是我的代码:
# Define server logic to read selected file ----
server <- function(input, output, session) {
# Read file ----
df <- reactive({
req(input$uploaded_file)
read.csv(input$uploaded_file$datapath,
header = input$header,
sep = input$sep)
})
# Dynamically generate UI input when data is uploaded ----
output$checkbox <- renderUI({
checkboxGroupInput(inputId = "select_var",
label = "Select variables",
choices = names(df()),
selected = names(df()))
#Dynamically create the number of sliders##################################################
output$input_ui <- renderUI({
num <- df()
lapply(num, function(i) {
numericInput(paste0("n_input_", i), label = names(num), value = 0) #this creates numeric input or sliders
})
})
output$table <- renderTable({
num <- as.integer(paste0("n_input_", i))
data.frame(lapply(1:num, function(i) {
input[[paste0("n_input_", i)]]
}))
})
这是 R.UI 代码:
# Define UI for data upload app ----
ui <- fluidPage(
# App title ----
titlePanel(title = h1("Dynamic Variable Selection!!! (not working yet)", align = "center")),
# Sidebar layout with input and output definitions ----
sidebarLayout(
# Sidebar panel for inputs ----
sidebarPanel(
# Input: Select a file ----
fileInput("uploaded_file", "Choose CSV File",
multiple = TRUE,
accept = c("text/csv",
"text/comma-separated-values,text/plain",
".csv")),
# Horizontal line ----
sliderInput("months", "Months:",
min = 0, max = 60,
value = 1),
tags$hr(),
# Input: Checkbox if file has header ----
checkboxInput("header", "Header", TRUE),
# Input: Select separator ----
radioButtons("sep", "Separator",
choices = c(Semicolon = ";",
Comma = ",",
Tab = "\t"),
selected = ","),
# Horizontal line ----
tags$hr(),
# Input: Select number of rows to display ----
radioButtons("disp", "Display",
choices = c(All = "all",
Head = "head"),
selected = "all"),
# Select variables to display ----
uiOutput("checkbox"),
uiOutput("input_ui")
),
# Main panel for displaying outputs ----
mainPanel(
tabsetPanel(
id = "dataset",
tabPanel("FILE", dataTableOutput('rendered_file'),tableOutput("table"))
)
)
)
)
# Create Shiny app ----
shinyApp(ui, server)
加分的一些额外问题!
- 我似乎无法为动态滑块生成名称。
- 是否可以根据它所代表的变量的数据范围动态生成滑块的值?
- 如果我只想为发现具有统计意义的变量显示动态滑块怎么办?
感谢您对这项工作的指导和帮助。
解决方案
我今天能够部分回答第 4 点。但是,也存在一些问题。这是我写的代码:
#Dynamically create the number of sliders##################################################
output$input_ui <- renderUI({
pvars <- df_sel()
varn = names(df_sel())
lapply(seq(pvars), function(i) {
numericInput(inputId = paste0("range", pvars[i]),
label = names(df_sel()),
value = 0)
})
})
output$table <- renderTable({
pvars <- df_sel()
num = as.integer(ncol(pvars))
print(num)
pred <- data.frame(lapply(1:num, function(i) {
input[[paste0("range", pvars[i])]]
}))
n = input$months
pd = data.frame(pred, i=rep(1:n,ea=NROW(input$months)))
})
有几个问题:
- 我似乎无法从数据框中获取列标题以应用于每个 numericInput
- 我似乎无法在 R 服务器代码的其他地方访问“pd”作为数据框。
- 如果不将重复计数作为新列添加到数据框中,那就太好了。
(另外,如果我选中或取消选中我拥有的复选框列表中的任何其他变量,则有一个稍微令人讨厌的方面,numericInput 框中的所有值都会重置)。
推荐阅读
- odoo - Odoo search_default_product_id 通过在上下文中传递字符串
- swift - RealmSwift:存储带有时间戳的元素
- java - 如何将我的多行 if 语句转换为单个流?
- .htaccess - 为什么我的 htaccess 重定向会改写我的 URL?
- python - Django 模型过滤器以 JSONField 为目标,其中键包含连字符/破折号
- ag-grid - 是否有 ag-grid 标题单元格类?
- r - nls sslogis:使用虚拟变量作为交互项来比较两个 sigmoid 模型
- twilio - Twillio 通话耳语和录音
- javascript - 尝试下载到对象的 csv javascript 数组
- azure - 无法授权 Azure 订阅在基于 YAML 的管道中使用