Here I have created a reactive value object y. So whenever a value of y changes as per RHS, it should get updated. But in this sample, the output I get is 61 instead of 90 . Can anyone explain why this is happening
library(shiny)
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
# titlePanel("Old Faithful Geyser Data"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(),
# Show a plot of the generated distribution
mainPanel(
textOutput("text")
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
output$text <- renderText({
y <- reactiveVal(0)
#x <- reactiveVal(0)
x = 61
y = x
x = 90
y
})
}
# Run the application
shinyApp(ui = ui, server = server)
#
# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
#
# Find out more about building applications with Shiny here:
#
# http://shiny.rstudio.com/
#
library(shiny)
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
# titlePanel("Old Faithful Geyser Data"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(),
# Show a plot of the generated distribution
mainPanel(
actionButton("but", "Submit"),
textOutput("text"),
textOutput("text1")
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output, session) {
event_reactive <- FALSE
if (event_reactive){
asd <- eventReactive(input$but,{
#browser()
y <- reactiveVal(a = 0)
#x <- reactiveVal(0)
x <- 61
y$a <- x
x <- 90
y$a
})
output$text <- renderText({
asd()
})
} else {
observeEvent(input$but,{
y <- reactiveVal(a = 0)
#x <- reactiveVal(0)
x <- 61
y$a <- x
x <- 90
y$a
output$text <- renderText({
y$a
})
})
}
}
# Run the application
shinyApp(ui = ui, server = server)
Error below
Warning: Error in reactiveVal: unused argument (a = 0)
1: runApp
You shouldn't put a reactiveVal inside an else clause. It should be defined at the top before you begin the if construct. The following code works on my end.
library(shiny)
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
# titlePanel("Old Faithful Geyser Data"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(),
# Show a plot of the generated distribution
mainPanel(
actionButton("but", "Submit"),
textOutput("text"),
textOutput("text1")
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output, session) {
y <- reactiveVal(0)
event_reactive <- FALSE
if (event_reactive){
asd <- eventReactive(input$but,{
#browser()
# y <- reactiveVal(a = 0)
#x <- reactiveVal(0)
x <- 61
y <- x
x <- 90
y
})
output$text <- renderText({
asd()
})
} else {
observeEvent(input$but,{
#x <- reactiveVal(0)
x <- 61
y <- x
x <- 90
y
output$text <- renderText({
y
})
})
}
}
shinyApp(ui = ui, server = server)
a reactiveVal, is a container you can put things into, and read things out from, it is reactive in the same way reactive()'s are because if you have a reactive() or render* that depends on it, when its value changes those that rely on it will change. You can see that here that the y text printer changes when we use y() to give a new value to reactiveVal y.
library(shiny)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
sliderInput("x_slider", "X",
min = 1, max = 5, value = 3),
actionButton("yset_btn", "set Y to X")
),
mainPanel(
fluidPage(
fluidRow(
column(3, verbatimTextOutput("xtext")),
column(3, verbatimTextOutput("ytext"))
)
)
)
)
)
server <- function(input, output) {
y <- reactiveVal(0)
observeEvent(input$yset_btn,{
y(input$x_slider) # <<<< Giving y() new contents here !
})
output$xtext <- renderPrint({
req(input$x_slider)
})
output$ytext <- renderPrint({
req(y()) # <<<<< when y() changes the print will happen
})
}
shinyApp(ui = ui, server = server)