Change column type of imported data set in shiny
我想更改导入日期的所选列类型。该操作应在闪亮的应用程序中完成。数据在csv文件中。在此数据集中,应更改的列称为" date ",但是,理想情况下,应该可以输入应更改类型的列的名称。
我编写的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | ui <- fluidPage( titlePanel("Test"), mainPanel( fluidRow( column(5, fileInput('file1',"Input a csv file:"), accept=c('text/csv', 'text/comma-separated-values,text/plain', '.csv'), checkboxInput('header', 'Header', TRUE)), column(3, radioButtons('sep', 'Separator', c(Comma=',', Semicolon=';', Tab='\\t'), ',') ), column(3, radioButtons('quote', 'Quote', c(None='', 'Double Quote'='"', 'Single Quote'="'"), '"') ) ), tableOutput("first.two"), fluidRow( column(4, textInput("time.var", h3("Input time variable:"), value ="")) ), p(strong("Variables to choose from:"), textOutput("possible.variables")), p(strong("Classes of variables"), textOutput("variable.classes")) ) ) server <- function(input, output){ dset.in <- reactive({ infile <- input$file1 if(is.null(infile)){ return(NULL) } df <- read.csv(infile$datapath, header = input$header, sep = input$sep,quote = input$quote) return(df)} ) # Change column type to date # When three lines below are commented out, the code works dset.in()$date.input <- observeEvent({ dset.in()[,input$time.var] <- lubridate::date_decimal(dset.in()[,input$time.var]) }) output$first.two <- renderTable({head(dset.in(), 2)}) output$possible.variables <- renderText( names(dset.in()) ) output$variable.classes <- renderText( sapply(dset.in(), class) ) } shinyApp(ui = ui, server = server) |
它会产生以下错误:
Warning in if (!is.na(attribValue)) { :
the condition has length > 1 and only the first element will be usedWarning in charToRaw(enc2utf8(text)) :
argument should be a character vector of length 1
all but the first element will be ignoredWarning in body(fun) : argument is not a function
Warning: Error in eval: argument"expr" is missing, with no default
Stack trace (innermost first):
44: eval
43: makeFunction
42: exprToFunction
41: observeEvent
40: server [#16]
4:
3: do.call
2: print.shiny.appobj
1:Error in eval(call("function", args, body), env) :
argument"expr" is missing, with no default
尝试使用此:
1 2 3 | modified_dset <- eventReactive(input$time.var, { dset.in()[, input$time.var] <- lubridate::date_decimal(dset.in()[,input$time.var]) }) |
并在其他任何地方使用此