Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 动态选择和缩放闪亮应用程序中的变量_R_Shiny_Dplyr - Fatal编程技术网

R 动态选择和缩放闪亮应用程序中的变量

R 动态选择和缩放闪亮应用程序中的变量,r,shiny,dplyr,R,Shiny,Dplyr,我有一个闪亮的应用程序,我希望用户能够选择在最终数据框中保留哪些变量,然后还可以选择将哪些变量缩放为百分比。我有这个工作,但我遇到了一个小难题。问题是,如果用户决定添加或删除一个附加变量,他们必须重新进行缩放。如果我的用户有许多他们正在处理的列,这可能是一个问题。如何保持用户已经完成的缩放工作,同时允许从最终数据帧中添加或删除变量 library(shiny) library(tidyverse) library(DT) # Define UI ui <- fluidPage( c

我有一个闪亮的应用程序,我希望用户能够选择在最终数据框中保留哪些变量,然后还可以选择将哪些变量缩放为百分比。我有这个工作,但我遇到了一个小难题。问题是,如果用户决定添加或删除一个附加变量,他们必须重新进行缩放。如果我的用户有许多他们正在处理的列,这可能是一个问题。如何保持用户已经完成的缩放工作,同时允许从最终数据帧中添加或删除变量

library(shiny)
library(tidyverse)
library(DT)

# Define UI 
ui <- fluidPage(
  checkboxGroupInput("select_var", label = "Select Variables"),
  selectInput("scalescore", label = NULL, choices = c("")),
  actionButton("scale", "Scale Scores"),
  DT::dataTableOutput("table")

)

# Define server 
server <- function(session, input, output) {
  # define the reactive values
  values <- reactiveValues(df_final = NULL)

  # dynamically generate the variable names
  observe({
    vchoices <- names(mtcars)
    updateCheckboxGroupInput(session, "select_var", choices = vchoices)
  })

  # dynamically generate the variables to scale
  observe({
    vchoices <- names(values$df_final)
    updateSelectInput(session, "scalescore", choices = vchoices)
  })

  # select the variables based on checkbox
  observe({
    req(input$select_var)
    df_sel <- mtcars %>% select(input$select_var) 
    values$df_final <- df_sel
  })

  observeEvent(input$scale, {
    name <- rlang::sym(paste0(input$scalescore, "_scaled"))
    values$df_final <- values$df_final %>% mutate(!!name := round(!!rlang::sym(input$scalescore)/max(!!rlang::sym(input$scalescore), na.rm = TRUE)*100, 1))})

 output$table <- DT::renderDataTable(values$df_final)
}

# Run the application 
shinyApp(ui = ui, server = server)

我们需要维护一个向量,该向量跟踪变量是否缩放。这是怎么做到的

library(shiny)
library(tidyverse)
library(DT)

# Define UI 
ui <- fluidPage(
  checkboxGroupInput("select_var", label = "Select Variables"),
  selectInput("scalescore", label = NULL, choices = c("")),
  actionButton("scale", "Scale Scores"),
  DT::dataTableOutput("table")

)

server = function(input,output,session){
  #Column names are static
  names = colnames(mtcars)

  # data scructure to store if the variable is scaled
  is_scaled = logical(length(names))
  names(is_scaled) = names #Set the names of the logical vector to the column names 

  #Update the checkbox with the column names of the dataframe
  observe({
    updateCheckboxGroupInput(session, "select_var", choices = names)
  })

  # Update the list of choices but dont include the scaled vaiables
  observe({
    vchoices <- names(data())
    vchoices = vchoices[vchoices %in% names]
    updateSelectInput(session, "scalescore", choices = vchoices)
  })

  #When the scle button is pressed, the vector which contains the list of scaled variables is updated 
  observeEvent(input$scale,{
    if(is_scaled[[input$scalescore]]){
      is_scaled[[input$scalescore]] <<- FALSE
    }else{
      is_scaled[[input$scalescore]] <<- TRUE
    }
  })

  #Function to scale the variables
  scale = function(x){
    return(round(x/max(x,na.rm = T)*100,1))
  }

  data = reactive({
    req(input$select_var)
    input$scale #simply to induce reactivity

    #Select the respective columns
    df = mtcars%>%
      select(input$select_var)

    if(any(is_scaled[input$select_var])){
      temp_vec = is_scaled[input$select_var] #Get a list of variables selected
      true_vec = temp_vec[which(temp_vec)] #Check which ones are scaled
      true_vec_names = names(true_vec) #Get the names of the variables scales

      #Scale the variables respectively
      df = df%>%
        mutate_at(.vars = true_vec_names,.funs = funs(scaled = scale(.)))
    }

    return(df)
  })

  output$table = DT::renderDataTable(data())
}

# Run the application 
shinyApp(ui = ui, server = server)
是否缩放跟踪特定列是否缩放。稍后选择时,如果该向量中的值为真,则将缩放该值


如果按两次“缩放”按钮,则缩放列将被删除。

感谢您提供的创造性解决方案!我试图弄清楚变量在哪里被重新命名为_scaled,mutate_在添加它吗?此外,当选择了多个变量但只缩放了一个变量时,缩放列被称为“简单缩放”,这使得您很难知道它属于哪些变量。是的,您可以使用mutate_at函数来修复此问题。我不知道如何在mutate_at中实现这一点,但我使用了一个if语句来检查已缩放的变量名,然后重命名它:ifscaled%in%colnamedf{df%rename!!paste0true_vec_names,\u scaled:=scaled}