从反应表达式中的不同数据框中减去列
Subtracting columns from different dataframes inside reactive expression
这里是RShiny的新手,希望得到一些指导。
我正在尝试构建一个简单的闪亮应用程序,允许用户上传两个 csv 文件,从一个数据框 (B) 中减去另一个数据框 (A) 的特定列 [两者都具有相同的列结构],并合并这些结果进入 df A 的相应列。然后它应该在 UI.
中显示此更新数据框的结果
当我尝试 运行 应用程序时,出现以下错误:
Error in .getReactiveEnvironment()$currentContext() :
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.)
我原以为在导入文件 A 和 B 时使用反应式语句意味着该函数的其余部分是在反应式表达式中完成的……也许我搞错了。感谢您提出任何建议?
服务器功能如下:
server <- function(input, output) {
#fileA import
A <- reactive(
read.csv(
input$fileA$datapath,
header = input$header,
sep = input$sep,
quote = input$quote
)
)
#FileB import
B <- reactive(
read.csv(
input$fileB$datapath,
header = input$header,
sep = input$sep,
quote = input$quote
)
)
##subtract B from A (columns 2 and 4)
A()[,2] <- as.numeric(A()[,2])
B()[,2] <- as.numeric(B()[,2])
A()[,4] <- as.numeric(A()[,4])
B()[,4] <- as.numeric(B()[,4])
Finalcosts_AB2 <- A()[,2]-B()[,2]
Finalcosts_AB4 <- A()[,4]-B()[,4]
A()[,2] <- Finalcosts_AB2
A()[,4] <- Finalcosts_AB4
output$ABdiff <- renderTable({
req(input$filaA)
req(input$fileB)
return(A())})
}
#Run the application
shinyApp(ui = ui, server = server)
我在 UI 中使用了以下方法:
ui <- fluidPage(
titlePanel("testfile"),
sidebarPanel(
#Input: select file A----
fileInput(
"fileA",
"Upload fileA results (.CSV format)",
multiple = FALSE,
accept = c("text/csv", "text/comma-separated-values,
text/plain", ".csv")
),
#Horizontal line
tags$hr(),
#Input: select file B----
fileInput(
"fileB",
"Upload fileB results (.CSV format)",
multiple = FALSE,
accept = c("text/csv", "text/comma-separated-values,
text/plain", ".csv")
),
#Horizontal line
tags$hr(),
#settings
# Input: Checkbox if file has header ----
checkboxInput("header", "Header", TRUE),
# Input: Select separator ----
radioButtons(
"sep",
"Separator",
choices = c(
Comma = ",",
Semicolon = ";",
Tab = "\t"
),
selected = ","
),
# Input: Select quotes ----
radioButtons(
"quote",
"Quote",
choices = c(
None = "",
"Double Quote" = '"',
"Single Quote" = "'"
),
selected = '"'
),
# Horizontal line ----
tags$hr(),
# Input: Select number of rows to display ----
radioButtons(
"disp",
"Display",
choices = c(Head = "head",
All = "all"),
selected = "head"
)
),
mainPanel(
#header
h3("Results"),
#Output: HTML table of results----
tableOutput("ABdiff")
)
)
请尽量避免修改反应性数据帧。如下所示修改服务器代码后,它就可以正常工作了。
server <- function(input, output) {
#fileA import
A <- reactive({
da <- read.csv(
req(input$fileA$datapath),
header = input$header,
sep = input$sep,
quote = input$quote
)
da[,2] <- as.numeric(da[,2])
da[,4] <- as.numeric(da[,4])
da
})
#FileB import
B <- reactive({
db <- read.csv(
req(input$fileB$datapath),
header = input$header,
sep = input$sep,
quote = input$quote
)
db[,2] <- as.numeric(db[,2])
db[,4] <- as.numeric(db[,4])
db
})
output$ABdiff <- renderTable({
req(A(),B())
Finalcosts_AB2 <- A()[,2]-B()[,2]
Finalcosts_AB4 <- A()[,4]-B()[,4]
AA <- A()
AA[,2] <- Finalcosts_AB2
AA[,4] <- Finalcosts_AB4
return(AA)
})
}
这里是RShiny的新手,希望得到一些指导。
我正在尝试构建一个简单的闪亮应用程序,允许用户上传两个 csv 文件,从一个数据框 (B) 中减去另一个数据框 (A) 的特定列 [两者都具有相同的列结构],并合并这些结果进入 df A 的相应列。然后它应该在 UI.
中显示此更新数据框的结果当我尝试 运行 应用程序时,出现以下错误:
Error in .getReactiveEnvironment()$currentContext() :
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.)
我原以为在导入文件 A 和 B 时使用反应式语句意味着该函数的其余部分是在反应式表达式中完成的……也许我搞错了。感谢您提出任何建议?
服务器功能如下:
server <- function(input, output) {
#fileA import
A <- reactive(
read.csv(
input$fileA$datapath,
header = input$header,
sep = input$sep,
quote = input$quote
)
)
#FileB import
B <- reactive(
read.csv(
input$fileB$datapath,
header = input$header,
sep = input$sep,
quote = input$quote
)
)
##subtract B from A (columns 2 and 4)
A()[,2] <- as.numeric(A()[,2])
B()[,2] <- as.numeric(B()[,2])
A()[,4] <- as.numeric(A()[,4])
B()[,4] <- as.numeric(B()[,4])
Finalcosts_AB2 <- A()[,2]-B()[,2]
Finalcosts_AB4 <- A()[,4]-B()[,4]
A()[,2] <- Finalcosts_AB2
A()[,4] <- Finalcosts_AB4
output$ABdiff <- renderTable({
req(input$filaA)
req(input$fileB)
return(A())})
}
#Run the application
shinyApp(ui = ui, server = server)
我在 UI 中使用了以下方法:
ui <- fluidPage(
titlePanel("testfile"),
sidebarPanel(
#Input: select file A----
fileInput(
"fileA",
"Upload fileA results (.CSV format)",
multiple = FALSE,
accept = c("text/csv", "text/comma-separated-values,
text/plain", ".csv")
),
#Horizontal line
tags$hr(),
#Input: select file B----
fileInput(
"fileB",
"Upload fileB results (.CSV format)",
multiple = FALSE,
accept = c("text/csv", "text/comma-separated-values,
text/plain", ".csv")
),
#Horizontal line
tags$hr(),
#settings
# Input: Checkbox if file has header ----
checkboxInput("header", "Header", TRUE),
# Input: Select separator ----
radioButtons(
"sep",
"Separator",
choices = c(
Comma = ",",
Semicolon = ";",
Tab = "\t"
),
selected = ","
),
# Input: Select quotes ----
radioButtons(
"quote",
"Quote",
choices = c(
None = "",
"Double Quote" = '"',
"Single Quote" = "'"
),
selected = '"'
),
# Horizontal line ----
tags$hr(),
# Input: Select number of rows to display ----
radioButtons(
"disp",
"Display",
choices = c(Head = "head",
All = "all"),
selected = "head"
)
),
mainPanel(
#header
h3("Results"),
#Output: HTML table of results----
tableOutput("ABdiff")
)
)
请尽量避免修改反应性数据帧。如下所示修改服务器代码后,它就可以正常工作了。
server <- function(input, output) {
#fileA import
A <- reactive({
da <- read.csv(
req(input$fileA$datapath),
header = input$header,
sep = input$sep,
quote = input$quote
)
da[,2] <- as.numeric(da[,2])
da[,4] <- as.numeric(da[,4])
da
})
#FileB import
B <- reactive({
db <- read.csv(
req(input$fileB$datapath),
header = input$header,
sep = input$sep,
quote = input$quote
)
db[,2] <- as.numeric(db[,2])
db[,4] <- as.numeric(db[,4])
db
})
output$ABdiff <- renderTable({
req(A(),B())
Finalcosts_AB2 <- A()[,2]-B()[,2]
Finalcosts_AB4 <- A()[,4]-B()[,4]
AA <- A()
AA[,2] <- Finalcosts_AB2
AA[,4] <- Finalcosts_AB4
return(AA)
})
}