【问题标题】:Change column type of imported data set in shiny在闪亮中更改导入数据集的列类型
【发布时间】:2018-02-21 11:34:00
【问题描述】:

我想更改导入到日期的选定列类型。这些操作应该在闪亮的应用程序中完成。数据在 csv file 中。在这个数据集中,应该改变的列被称为“日期”,然而,理想情况下应该可以输入应该改变类型的列的名称。

我写的代码:

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)

它会产生以下错误:

警告 if (!is.na(attribValue)) { :
条件的长度 > 1,并且只使用第一个元素

charToRaw(enc2utf8(text)) 中的警告:
参数应该是长度为 1 的字符向量
除了第一个元素之外的所有元素都将被忽略

正文中的警告(有趣):参数不是函数

警告:评估错误:缺少参数“expr”,没有默认值
堆栈跟踪(最内层优先):
44: 评估
43: 生成函数
42: exprToFunction
41:观察事件
40:服务器 [#16]
4:
3:do.call
2:print.shiny.appobj
1:

eval 中的错误(调用(“function”,args,body),env):
缺少参数“expr”,没有默认值

【问题讨论】:

    标签: r csv import shiny reactive


    【解决方案1】:

    试试这个:

      modified_dset <- eventReactive(input$time.var, {
        dset.in()[, input$time.var] <- lubridate::date_decimal(dset.in()[,input$time.var])
      })
    

    并在其他任何地方使用这个modified_dset

    【讨论】:

      猜你喜欢
      • 2011-07-30
      • 2015-11-17
      • 2017-10-26
      • 1970-01-01
      • 2019-07-16
      • 2017-04-05
      • 1970-01-01
      • 1970-01-01
      • 2017-04-13
      相关资源
      最近更新 更多