根据反应列定义新变量

时间:2019-02-26 20:14:15

标签: r shiny shiny-reactivity

我正在尝试定义一个变量time_var_use,它将是用户上传到Shiny应用程序的数据的另一列。对于初学者,此变量可以等于用户上传的此数据中的另一个变量。问题在于此变量应等于用户在input$time_var中指定的列,因此其值可以根据用户选择的列名而改变。

该应用的代码很简单:

 # 1. UI Design

ui <- dashboardPage(

    dashboardHeader(title = "My app"),

    dashboardSidebar(

    sidebarMenu(

    menuItem("Upload Data", tabName = "data_upload", icon = icon("folder- 
    open"))

       )

    ),

     dashboardBody(

      tags$head(tags$style(HTML('.main-header .logo {

                          font-family: "Bliss Pro Light", Times, "Times New Roman", serif;

                          font-weight: bold;

                          font-size: 30px;

                          }

                          '))),

tabItems(

  tabItem(tabName = "data_upload",

          fluidRow(

            box(title = "Modelling data", status = "primary",

                fileInput(inputId = "main_data", label = "Upload the data for modelling", accept = c('.csv', 'text/comma-separated-values,text/plain', 'text/csv', 'csv')),

                checkboxInput(inputId = "header", label = "The data has headers", value = TRUE),

                radioButtons(inputId = "sep", label = "Delimiter:", choices = c("Comma" = ",","Semicolon" = ";","Tab" = "\t"), selected = ";")

            )
          ),
          fluidRow(

            box(title = "Divide modelling data into estimation & validation sample", status = "primary", width = 12,

                selectizeInput(inputId = "time_var", label = "Select the time variable", choices = "", multiple = FALSE),

                selectizeInput(inputId = "time_threshold", label = "Select time threshold for estimation and validation", choices = "", multiple = FALSE)

            )
          )
  )
)
)
)


 # 2. Server Design

server <- function(input, output, session) {

   model_data <- reactive({

     infile <- input$main_data



 if (is.null(infile))

  return(NULL)

read.csv(infile$datapath, header = input$header, sep = input$sep, 
 stringsAsFactors = FALSE)

 })

   # update time_var choices
  observe({

vchoices <- names(model_data())

updateSelectizeInput(session = session, inputId = "time_var", choices = 
   vchoices)

 })

model_data_use <- reactive({

if (is.null(input$time_var))

  return(NULL)

df <- model_data()

df$time_var_use <- df[, input$time_var]

df

 })

  observe({

tchoices <- names(model_data_use())

updateSelectizeInput(session = session, inputId = "time_threshold", choices 
  = tchoices)

    })




 }


# 3. Create ShinyApp

shinyApp(ui, server)

当我尝试定义一个新变量df$time_var_use <- df[, input$time_var]等于用户在time_var_use中选择的列时,问题似乎出在表达式input$time_var上,该列可以是数据集。您能建议如何用这种方式定义新列吗?

我正在使用示例CSV数据集:

     time var1 var2      var3
  1  2015q1    8    1 0.6355182
  2  2015q1   12    0 0.5498784
  3  2015q1   23    1 0.9130934
  4  2015q1  152    1 0.8938210
  5  2015q2  563    1 0.2335470
  6  2015q3    8    0 0.5802677
  7  2015q4    2    0 0.8514926
  8  2016q1    1    1 0.4712101
  9  2016q1   14    0 0.9747804
  10 2016q1   13    1 0.8571699
  11 2016q1   14    1 0.8738486
  12 2016q2   53    0 0.8467971
  13 2016q3   75    0 0.3191140
  14 2016q4   15    0 0.9608926

0 个答案:

没有答案