You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Shiny应用中fileInput()结合mutate()处理数据时触发错误

解决Shiny上传CSV后提示object 'prοcess' not found的问题

错误原因

代码中存在隐藏的字符拼写错误:你在mutate管道开头写的是prοcess,其中第二个字符是希腊字母ο(omicron),而非正常的英文小写字母o。R无法识别这个拼写错误的变量名,因此抛出找不到对象的错误。

修复方案

  1. 将管道中的prοcess替换为正确的变量名process
  2. 优化时间转换逻辑,避免重复对timestamp进行格式转换

修正后的完整代码

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

pr5915<-structure(list(case_id = c("WC4120721", "WC4120667", "WC4120689"
), lifecycle = c(110, 110, 110), action = c("WC4120721-CN354877", 
                                            "WC4120667-CN354878", "WC4120689-CN356752"), activity = c("Forged Wire, Medium (Sport)", 
                                                                                                      "Forged Wire, Medium (Sport)", "Forged Wire, Medium (Sport)"), 
resource = c("3419", "3216", "3409"), timestamp = structure(c(1606964400, 
                                                              1607115480, 1607435760), tzone = "", class = c("POSIXct", 
                                                                                                             "POSIXt"))), row.names = c(NA, -3L), class = c("tbl_df", 
                                                                                                                                                            "tbl", "data.frame"))

ui <- fluidPage(
  
  fileInput("file1", "Upload eventlog in csv format",
            accept = c(
              "text/csv",
              "text/comma-separated-values,text/plain",
              ".csv")),
  radioButtons("separator","Separator: ",choices = c(";","."), selected=";",inline=TRUE),
  dataTableOutput("table"),
  dataTableOutput("table2")
  
)

server <- function(input, output, session) {
  
  dataset<-reactive({
    inFile <- input$file1
    
    if (is.null(inFile))
      return(NULL)
    
    process<-read.csv(inFile$datapath, header = T,sep = input$separator)
    
    # 统一时间转换逻辑,避免重复操作
    process %>% 
      mutate(
        timestamp = ymd_hms(as.character(timestamp), tz = ""),
        Date = paste0(
          month(timestamp, label = T, abbr = F), 
          "-", 
          substr(year(timestamp), 3 , 4)
        )
      )
  })
  
  output$table<-renderDataTable({
    dataset()
  })
  
  output$table2<-renderDataTable({
    pr5915 %>% 
      mutate(
        Date = paste0(
          month(timestamp, label = T, abbr = F), 
          "-", 
          substr(year(timestamp), 3 , 4)
        )
      )
  })
}

shinyApp(ui = ui, server = server)

额外说明

  • 原代码中对timestamp进行了多次重复转换(as.POSIXct -> as.character -> as_datetime -> ymd_hms),优化后直接用ymd_hms解析字符格式的时间戳即可,更简洁高效
  • 内置数据集pr5915的timestamp已经是POSIXct类型,无需再次转换,直接生成Date列即可

内容的提问来源于stack exchange,提问作者firmo23

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 05:21:29