简体   繁体   English

R / Shiny动态复选框和滑块可生成数据框

[英]R/Shiny dynamic checkboxes and sliders generate a dataframe

I'm learning Shiny and developing a small app for multiple linear regression. 我正在学习Shiny,并正在开发用于小型线性回归的小型应用程序。 However, I've hit a bit of a wall and could use some expert guidance. 但是,我遇到了一些困难,可以使用一些专家指导。 Here's what I'm trying to do: 这是我想做的事情:

  1. be able to import .csv files with a varying number of columns as predictor variables 能够导入具有不同列数的.csv文件作为预测变量

  2. dynamically generate checkboxes to allow the user to select which variables they would like to include 动态生成复选框,以允许用户选择他们想要包括的变量

  3. generate sliders for each selected variable (the purpose is for "what if" simulation. The data for generating the regression model is pulled from the CSV file.) 为每个选定变量生成滑块(目的是“假设条件”模拟。用于生成回归模型的数据从CSV文件中提取。)

  4. produce a dataframe based on the values generated from the slider. 根据滑块产生的值产生一个数据框。

I've been borrowing a lot of code from other posts, but I still can't get number 4 to work. 我一直在从其他帖子中借用很多代码,但是仍然无法使4号工作。 Here's my code: 这是我的代码:

# 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)]]
  }))
  })

Here is the R.UI code: 这是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)

A few additional problems for bonus points! 奖励积分还有一些其他问题!

  1. I cannot seem to generate names for the dynamic sliders. 我似乎无法为动态滑块生成名称。
  2. Is is possible to dynamically generate the values of the sliders based on the data range of the variable it represents? 是否可以根据其代表的变量的数据范围动态生成滑块的值?
  3. What if I only wanted to show dynamic sliders for variables that were found to to be statistically significant? 如果我只想显示发现具有统计意义的变量的动态滑块该怎么办?

I appreciate your guidance and help on this effort. 感谢您的指导和帮助。

I was able to partially answer number 4 today. 我今天能够部分回答4号。 However, there are some issues. 但是,有一些问题。 Here is the code I wrote: 这是我写的代码:

#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)))
})

There are a couple of issues: 有几个问题:

  1. I can't seem to get the column headers from the dataframe to be applied to each numericInput 我似乎无法从数据框中获取要应用于每个numericInput的列标题
  2. I can't seem to access "pd" as a dataframe elsewhere in the R server code. 我似乎无法在R服务器代码中的其他位置将“ pd”作为数据框访问。
  3. It would be nice if the repeat count were not added as a new column to the dataframe. 如果没有将重复计数作为新列添加到数据帧中,那将是很好的。

(also, there is a slightly annoying aspect that if I check or uncheck any other variables in the list of checkboxes that I have, all the values in the numericInput boxes reset). (另外,还有一个令人讨厌的方面,如果我选中或取消选中我所拥有的复选框列表中的任何其他变量,则会重置numericInput框中的所有值)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM