我正在学习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)
一些额外的奖励积分问题!
感谢您的指导和帮助。
答案 0 :(得分:0)
我今天能够部分回答数字4。但是,有一些问题。 这是我写的代码:
textView2
有两个问题:
(另外,还有一个令人烦恼的方面,如果我选中或取消选中复选框列表中的其他任何变量,则会重置numericInput框中的所有值)。