在闪亮中更改导入数据集的列类型
Change column type of imported data set in shiny
我想更改导入日期的选定列类型。这些操作应该在闪亮的应用程序中完成。数据在 csv file 中。在此数据集中,应更改的列称为 "date",但是,理想情况下,应该可以输入应更改类型的列的名称。
我写的代码:
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 used
Warning in charToRaw(enc2utf8(text)) :
argument should be a character vector of length 1
all but the first element will be ignored
Warning 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
试试这个:
modified_dset <- eventReactive(input$time.var, {
dset.in()[, input$time.var] <- lubridate::date_decimal(dset.in()[,input$time.var])
})
并在其他地方使用这个modified_dset
。
我想更改导入日期的选定列类型。这些操作应该在闪亮的应用程序中完成。数据在 csv file 中。在此数据集中,应更改的列称为 "date",但是,理想情况下,应该可以输入应更改类型的列的名称。
我写的代码:
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 used
Warning in charToRaw(enc2utf8(text)) :
argument should be a character vector of length 1
all but the first element will be ignored
Warning 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
试试这个:
modified_dset <- eventReactive(input$time.var, {
dset.in()[, input$time.var] <- lubridate::date_decimal(dset.in()[,input$time.var])
})
并在其他地方使用这个modified_dset
。