Newbie to RShiny here, hoping for some guidance.
I am trying to build a simple shiny app that allows the user to upload two csv files, subtract specific columns of one dataframe (B) from another (A) [both with the same column structures], and incorporate these results into the corresponding columns for df A. It should then display the results for this updated dataframe in the UI.
When I try to run the app, it comes up with the following error:
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.)
I had thought the use of reactive statements when importing files A and B would mean that the remainder of the function is done inside a reactive expression... maybe I've got the wrong end of the stick. Grateful for any suggestions?
The server function is as follows:
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)
I've been using the following approach in the 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")
)
)