首页 > 解决方案 > R/Shiny 动态复选框和滑块生成数据框

问题描述

我正在学习 Shiny 并开发一个用于多元线性回归的小应用程序。但是,我遇到了一些障碍,可以使用一些专家指导。这是我正在尝试做的事情:

  1. 能够导入具有不同列数的 .csv 文件作为预测变量

  2. 动态生成复选框以允许用户选择他们想要包含的变量

  3. 为每个选定的变量生成滑块(目的是“假设”模拟。用于生成回归模型的数据是从 CSV 文件中提取的。)

  4. 根据滑块生成的值生成数据框。

我一直在从其他帖子中借用很多代码,但我仍然无法让 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)

加分的一些额外问题!

  1. 我似乎无法为动态滑块生成名称。
  2. 是否可以根据它所代表的变量的数据范围动态生成滑块的值?
  3. 如果我只想为发现具有统计意义的变量显示动态滑块怎么办?

感谢您对这项工作的指导和帮助。

标签: rshiny

解决方案


我今天能够部分回答第 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)))
})

有几个问题:

  1. 我似乎无法从数据框中获取列标题以应用于每个 numericInput
  2. 我似乎无法在 R 服务器代码的其他地方访问“pd”作为数据框。
  3. 如果不将重复计数作为新列添加到数据框中,那就太好了。

(另外,如果我选中或取消选中我拥有的复选框列表中的任何其他变量,则有一个稍微令人讨厌的方面,numericInput 框中的所有值都会重置)。


推荐阅读