The below code runs fine so long as the line in server section v <- reactiveValues(results=tibble(Scenario = 1, data()))
is commented out. When I uncomment it the App crashes and I get the error message: Warning: Error in : Can't access reactive value 'input1' outside of reactive consumer. i Do you need to wrap inside reactive() or observer()?
I'm trying to create a vector using tibble
for another function to be added. What am I doing wrong here in my use of tibble
? I'm trying to capture via tibble
, as a vector, the values generated by my custom interpol
function when it takes inputs from matrix input2
through the data()
function below. I also tried c()
, as.vector()
, etc., to make sure input2
is converted to a vector but I still get the same error.
I'm completely new to tibbles
and tidyverse
etc.
Code:
library(shiny)
library(shinyMatrix)
library(tidyverse) # < ADDED
interpol <- function(a,b){ # a = periods, b = matrix inputs
c <- rep(NA,a)
c[1] <- b[1]
c[a] <- b[2]
c <- approx(seq_along(c)[!is.na(c)],c[!is.na(c)],seq_along(c))$y # this interpolates
return(c)
}
ui <- fluidPage(
sidebarLayout(
sidebarPanel(uiOutput("panel"),actionButton("showInput2","Modify/add interpolation")),
mainPanel(plotOutput("plot"))
)
)
server <- function(input, output, session){
data <- function(){
if(!isTruthy(input$input1)){interpol(6,matrix(c(1,5)))} else {
if(!isTruthy(input$input2)){interpol(input$periods,
matrix(c(input$input1[1,1],input$input1[1,2])))} else {
interpol(input$periods,matrix(c(input$input2[1,1],input$input2[1,2])))}}
}
# v <- reactiveValues(results=tibble(Scenario = 1, data()))
output$panel <- renderUI({
tagList(
sliderInput('periods','Interpolate over periods (X):',min=2,max=12,value=6),
uiOutput("input1"))
})
output$input1 <- renderUI({
matrixInput("input1",
label = "Interpolation 1 (Y values):",
value = matrix(if(isTruthy(input$input2)){c(input$input2[1],input$input2[2])}
else {c(1,5)}, # matrix values
1, 2, # matrix row/column count
dimnames = list(NULL,c("Start","End"))), # matrix column header
rows = list(names = FALSE), class = "numeric")
})
observeEvent(input$showInput2,{
showModal(
modalDialog(
matrixInput("input2",
label = "Automatically numbered scenarios (input into blank cells to add):",
value = if(isTruthy(input$input2)){input$input2}
else if(isTruthy(input$input1)){input$input1},
rows = list(names = FALSE),
cols = list(extend = TRUE, delta = 2, delete = TRUE, multiheader=TRUE),
class = "numeric"),
footer = modalButton("Close")
))
})
observe({
req(input$input2)
mm <- input$input2
colnames(mm) <- paste(trunc(1:ncol(mm)/2) 1, " (start|end)")
isolate(updateMatrixInput(session, "input2", mm))
})
output$plot<-renderPlot({plot(data(),type="l",xlab="Periods (X)", ylab="Interpolated Y values")})
}
shinyApp(ui, server)
CodePudding user response:
The reason for the error and solution actually is present in the error message itself. You cannot access reactive
variable (data()
) outside reactive context. Try wrapping the v
output in reactive
.
v <- reactive({tibble(Scenario = 1, data())})