0

I am quite new to R shiny and so have not been able to figure out the solution from similar questions posted on this site. I am trying to read and use the input that a user provides to R shiny to generate an output.

I am trying to create a simple GUI where a user selects the name of a person (from a drop-down menu) and then enters his/her weight. If the height is above a certain threshold the output recommendation is "Gain Weight", else it is "Loose Weight".

Everything seems to be working fine, except for the following error from the Server.R file:

 Error in `$.shinyoutput`(output, value_weight) : 
 Reading objects from shinyoutput object not allowed

How can I read and use the variable 'value_weight' in an if-then-else condition?

Main.R

library(shiny)
runApp()

Server.R

function(input, output) {

 # You can access the value of the widget with input$select, e.g.
 output$value_name   <- renderPrint({ input$select })
 output$value_weight <- renderPrint({ input$num })

 if(output$value_weight > 150)
 {
   output$value_recommendation <- "Loose Weight"
 }
 else{
   output$value_recommendation <- "Gain Weight"
 }

 }

UI.R

 names_list <- list("Adam", "Jenna","Peter")

 fluidPage(
 selectInput("select", label = h3("Select Name"), choices = names_list, selected = 1),

 hr(),
 fluidRow(column(3, verbatimTextOutput("value_name"))),
 numericInput("num", label = h3("Enter Weight"), value = 0),

 hr(),
 fluidRow(column(3, verbatimTextOutput("value_weight"))),

 hr(),
 fluidRow(column(3, verbatimTextOutput("value_recommendation")))

   )

2 Answers 2

2

The problem in your code is the line

if(output$value_weight > 150)

Generally speaking, outputs are write-only objects in the server, while inputs are readonly. If you replace output$value_weight with input$num, everything should work fine. You also need to use a render-function for outputs: in this case renderPrint or renderText (see the documentation for the difference between those two render functions).

## server.R
function(input, output) {
  # You can access the value of the widget with input$select, e.g.
  output$value_name   <- renderPrint({ input$select })
  output$value_weight <- renderPrint({ input$num })

  output$value_recommendation <- renderPrint({
    if(input$num > 150)
      "Loose Weight"
    else
      "Gain weight"
  })
}

Another way to do this is using a call to the function reactive

## server.R
function(input, output) {
  # You can access the value of the widget with input$select, e.g.
  output$value_name   <- renderPrint({ input$select })
  value_weight <- reactive({ input$num })
  output$value_weight <- renderPrint({ value_weight() })

  output$value_recommendation <- renderPrint({
    if(value_weight() > 150)
      "Loose Weight"
    else
      "Gain weight"
  })
}
Sign up to request clarification or add additional context in comments.

Comments

0

Using 'renderText' solved the issue!

Server.R

function(input, output) 
{

  output$value_market <- renderPrint({ input$select })
  output$value_demand <- renderPrint({ input$num })


  output$value_recommendation <- renderText({
  if(input$num > 150)
  {
    print("Loose Weight")
  }
  else{
    print("Gain Weight")
  }
  })
}

1 Comment

Oh! Just saw you solved it yourself. Hopefully, my answer gives more insight about why you encountered this specific error message.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.