我的代码能够上传csv /文本文件,但是当我按下“清理”操作按钮时却无法清理数据。我不确定是什么问题,当我在数据表和数据帧形式中都无法正常工作时。我的代码的目的是让用户:
我们将不胜感激!
数据表形式:
library(data.table)
library(DT)
runApp(shinyApp(
ui=(fluidPage(
titlePanel("Clean your data!"),
mainPanel(
fileInput("file", "Upload file"),
actionButton("Go", "Clean!"),
DT::dataTableOutput("df_data_out"),
downloadButton("downloadData","Download")
)
)),
server <- function(input, output){
myData <- reactive({
inFile <- input$file
if (is.null(inFile)) return(NULL)
data <- read.csv(inFile$datapath, header = TRUE)
data <- as.data.table(data)
})
eventReactive(input$Go, {
dt <- myData()
dt <- as.data.table(dt)
dt1 <- dt[, splitletter:=substr(DocRef,1,1)]
dt2 <- dt1[splitletter == "B" | splitletter == "I"]
dt3 <- dt2[DocDate <= 20171231 & DocDate >= 20160101]
dt3 <- dt3[AcCurWTaxAmt>0 & HomeWTaxAmt<0,
HomeWTaxAmt:=abs(HomeWTaxAmt)]
dt3 <- dt3[AcCurWTaxAmt<0 & HomeWTaxAmt>0, HomeWTaxAmt:=HomeWTaxAmt*
(-1)]
dt3 <- dt3[AcCurWTaxAmt==0 & HomeWTaxAmt!=0, HomeWTaxAmt:=0]
dt3 <- dt3[AcCurWTaxAmt!=0 & HomeWTaxAmt==0, HomeWTaxAmt:=NA]
dt4 <- na.omit(dt3)
dt4 <- dt4[, AcCrAmt := AcCrIsMinus1 * AcCurWTaxAmt]
dt4 <- dt4[, splitletter:= NULL]
})
output$df_data_out <- DT::renderDataTable({
DT::datatable(myData())})
output$downloadData <- downloadHandler(
filename = function() {
paste("data-", Sys.Date(), ".csv", sep="")
},
content = function(file) {
write.csv(myData(), file)
})
}))
数据框形式:
runApp(shinyApp(
ui=(fluidPage(
titlePanel("Clean your data!"),
mainPanel(
fileInput("file", "Upload file"),
actionButton("Go", "Clean!"),
tableOutput("df_data_out"),
downloadButton("downloadData","Download")
)
)),
server <- function(input, output){
myData <- reactive({
inFile <- input$file
if (is.null(inFile)) return(NULL)
df_data <- read.csv(inFile$datapath, header = TRUE)
df_data <- as.data.frame(df_data)
})
eventReactive(input$Go, {
dt <- myData()
dt <- as.data.frame(data)
dt1[, splitletter] <- substr(dt$DocRef,1,1)
dt2 <- (dt1$splitletter == "B" | dt1$splitletter == "I")
dt3 <- (dt2$DocDate <= 20171231 & dt2$DocDate >= 20160101)
while(dt3$AcCurWTaxAmt>0 & dt$HomeWTaxAmt<0){
dt3$HomeWTaxAmt <- abs(dt3$HomeWTaxAmt)
}
while(dt3$AcCurWTaxAmt<0 & HomeWTaxAmt>0){
dt3$HomeWTaxAmt <- dt3$HomeWTaxAmt*(-1)
}
while(dt3$AcCurWTaxAmt==0 & HomeWTaxAmt!=0){
dt3$HomeWTaxAmt <- 0
}
while(dt3$AcCurWTaxAmt!=0 & HomeWTaxAmt==0){
dt3$HomeWTaxAmt<- NA
}
dt3 <- na.omit(dt3)
dt3[,AcCrAmt] <- dt3$AcCrIsMinus1 * dt3$HomeWTaxAmt
dt3$splitletter <- NULL
})
output$df_data_out <- renderTable({
myData()
})
output$downloadData <- downloadHandler(
filename = function() {
paste("data-", Sys.Date(), ".csv", sep="")
},
content = function(file) {
write.csv(myData(), file)
})
}))
答案 0 :(得分:0)
下面是一个有效的示例。使用CSV
作为输入来复制内容非常困难,因为我们不知道您喜欢什么。此外,您在eventReactive
中按名称引用列,因此我假设您始终向其传递特定的CSV
。
eventReactive
很好,它是一个反应函数,只需要它也需要一个触发它的表达式。因此,它会像reactive
一样返回反应式表达式。您正在尝试像eventReactive
那样使用observeEvent
。
当人们单击按钮eventReactive
时,将触发该按钮并返回您不使用的reactive
。 reactive
表达式对象myData
保持不变。如果您未在任何输出中使用eventReactive
的输出(即output$df_data_out
),则反应式将永远不会运行...
还请注意,您的reactive
表达式没有明确的return
。
library(shiny)
ui <- fluidPage(
selectInput("select", "select data", choices = c("cars", "iris")),
actionButton("clean", "Clean data"),
fluidRow(
column(6, tableOutput("original")),
column(6, tableOutput("cleaned"))
)
)
server <- function(input, output, session) {
orig_data <- reactive({
if(input$select == "cars")
return(cars)
else
return(iris)
})
cleaned_data <- eventReactive(input$clean, {
orig_data() %>%
head(5)
})
output$original <- renderTable({
orig_data()
})
output$cleaned <- renderTable({
cleaned_data()
})
}
shinyApp(ui, server)