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

时间:2018-12-04 15:57:05

标签: 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. 如果我只想显示被发现具有统计意义的变量的动态滑块,该怎么办?

感谢您的指导和帮助。

1 个答案:

答案 0 :(得分:0)

我今天能够部分回答数字4。但是,有一些问题。 这是我写的代码:

textView2

有两个问题:

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

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

相关问题