Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shiny: Why using a temporay variable for output on the server side fails?

Here is a simple shiny app that works :

ui.R

library(shiny)
shinyUI(bootstrapPage(
  numericInput("mymun", "enter a number",0),
  textOutput("mytext") 
))

server.R

library(shiny)
test <-  function(input, output){
  renderText(paste("this is my number:",input$mymun))
}

shinyServer(function(input, output) { 
  output$mytext <- test(input, output)
})

However if I put the result of the function call in a temporary variable the app fails

server.R

library(shiny)
test <-  function(input, output){
  renderText(paste("this is my number:",input$mymun))
}

shinyServer(function(input, output) { 
  tmp <- test(input, output)
  output$mytext <- tmp
})

The error message is :

Error in substitute(value)[[2]] : 
 object of type 'symbol' is not subsettable

Does anyone could gives clues about why the second one fails and why the first does not ? I guess this is due to expression processing and the reactive logic of shiny server, but I am not clear about it.

like image 849
user2147028 Avatar asked Jan 25 '26 05:01

user2147028


1 Answers

The best way to actually figure this out is to re-read http://www.rstudio.com/shiny/lessons/Lesson-6/ + http://www.rstudio.com/shiny/lessons/Lesson-7/ + http://rstudio.github.io/shiny/tutorial/#scoping to ensure you fully understand reactivity and scoping (no sense posting the seminal tutorial text in SO).

To get something close to what you're trying to do, you'd need to do the following in server.R:

library(shiny)

shinyServer(function(input, output) { 

  test <- reactive({
    return(paste("this is my number:",input$mymun))
  })

  output$mytext <- renderText({
    tmp <- test()
    return(tmp)
  })

})
like image 130
hrbrmstr Avatar answered Jan 26 '26 22:01

hrbrmstr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!