在 R shiny 中反应上传后编辑数据框
Editing data frame after reactive upload in R shiny
我正在尝试编辑使用 fileInput 上传的数据框 yjay 中的列,但是我一直收到错误 "Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)"。有人知道为什么我可能会收到此错误吗?不吝赐教,不胜感激!!
server = function(input, output) {
a1 = reactive({
if(is.null(input$myFile$datapath)) NULL
else{
read_excel(input$myFile$datapath)
}
})
x <- as.POSIXct(a1()$Month)
a1()$mo <- strftime(x, "%m")
a1()$yr <- strftime(x, "%Y")
a1()$qrt <- quarter(x, with_year = FALSE, fiscal_start = 01)
#subsetting data to display sales reps that hold a quota
newdata <- a1()[grepl("Y", a1()$`Quota Held Flag`),]
#fixing participation column into categorical for donut chart
newdata$Participation[is.na(newdata$Participation)] <- 0
newdata$Participation <- factor(newdata$Participation, labels =
c("0-99%","100%"))
#grouping data
newdata2 <- newdata %>%
group_by(yr, mo, qrt)
}
shinyApp(ui = ui, server = server)
server()
函数中的代码实际上应该只设置反应对象并响应反应事件。您不应该在 server()
函数本身的主体中包含任何数据操作数据,因为当它运行时数据尚不可用。这样的事情更有意义
ui <- fluidPage(
fluidPage(
titlePanel("Uploading Files"),
fileInput('myFile', 'Choose File'),
tableOutput('contents')
)
)
server <- function(input, output) {
a1 <- reactive({
req(input$myFile)
read_excel(input$myFile$datapath)
})
newdata <- reactive({
a1 <- a1()
x <- as.POSIXct(a1$Month)
a1$mo <- strftime(x, "%m")
a1$yr <- strftime(x, "%Y")
a1$qrt <- quarter(x, with_year = FALSE, fiscal_start = 01)
newdata <- a1[grepl("Y", a1$`Quota Held Flag`),]
#fixing participation column into categorical for donut chart
newdata$Participation[is.na(newdata$Participation)] <- 0
newdata$Participation <- factor(newdata$Participation, labels = c("0-99%","100%"))
#grouping data
newdata %>%
group_by(yr, mo, qrt)
})
output$contents <- renderTable(
newdata()
)
}
shinyApp(ui = ui, server = server)
注意 a1
如何读取用户上传的文件。然后 newdata
反应对象将在 a1
更新时更新并为您转换数据。然后我们可以将其连接到输出,以便实际处理它。
我正在尝试编辑使用 fileInput 上传的数据框 yjay 中的列,但是我一直收到错误 "Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)"。有人知道为什么我可能会收到此错误吗?不吝赐教,不胜感激!!
server = function(input, output) {
a1 = reactive({
if(is.null(input$myFile$datapath)) NULL
else{
read_excel(input$myFile$datapath)
}
})
x <- as.POSIXct(a1()$Month)
a1()$mo <- strftime(x, "%m")
a1()$yr <- strftime(x, "%Y")
a1()$qrt <- quarter(x, with_year = FALSE, fiscal_start = 01)
#subsetting data to display sales reps that hold a quota
newdata <- a1()[grepl("Y", a1()$`Quota Held Flag`),]
#fixing participation column into categorical for donut chart
newdata$Participation[is.na(newdata$Participation)] <- 0
newdata$Participation <- factor(newdata$Participation, labels =
c("0-99%","100%"))
#grouping data
newdata2 <- newdata %>%
group_by(yr, mo, qrt)
}
shinyApp(ui = ui, server = server)
server()
函数中的代码实际上应该只设置反应对象并响应反应事件。您不应该在 server()
函数本身的主体中包含任何数据操作数据,因为当它运行时数据尚不可用。这样的事情更有意义
ui <- fluidPage(
fluidPage(
titlePanel("Uploading Files"),
fileInput('myFile', 'Choose File'),
tableOutput('contents')
)
)
server <- function(input, output) {
a1 <- reactive({
req(input$myFile)
read_excel(input$myFile$datapath)
})
newdata <- reactive({
a1 <- a1()
x <- as.POSIXct(a1$Month)
a1$mo <- strftime(x, "%m")
a1$yr <- strftime(x, "%Y")
a1$qrt <- quarter(x, with_year = FALSE, fiscal_start = 01)
newdata <- a1[grepl("Y", a1$`Quota Held Flag`),]
#fixing participation column into categorical for donut chart
newdata$Participation[is.na(newdata$Participation)] <- 0
newdata$Participation <- factor(newdata$Participation, labels = c("0-99%","100%"))
#grouping data
newdata %>%
group_by(yr, mo, qrt)
})
output$contents <- renderTable(
newdata()
)
}
shinyApp(ui = ui, server = server)
注意 a1
如何读取用户上传的文件。然后 newdata
反应对象将在 a1
更新时更新并为您转换数据。然后我们可以将其连接到输出,以便实际处理它。