简体   繁体   中英

R/Shiny dynamic checkboxes and sliders generate a dataframe

I'm learning Shiny and developing a small app for multiple linear regression. 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

  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.)

  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. 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:

# 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. 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
  2. I can't seem to access "pd" as a dataframe elsewhere in the R server code.
  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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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