Home > Mobile >  Setting initial values for a linked function in R shinydashboard
Setting initial values for a linked function in R shinydashboard

Time:04-13

The answer to the question posed here has solved my initial problem of creating a linked sliderInput() and numericInput(), but when I implement this solution I get an error claiming that the default value has not been set, which means the sliderInput() does not render.

As soon as you change or enter a number in the numericInput(), the whole thing begins to render correctly, so it is obviously working fine as soon as the value is updated by the updateSliderInput() and updateNumericInput() functions.

What I cannot figure out is how to set the initial value?

The code from that question is replicated below.

"ui.R"

library(shiny)

shinyUI(fluidPage(

  uiOutput("Param_s"),
  uiOutput("Param_n")

))

"server.R"

library(shiny)

shinyServer(
function(input,output,session) {

# Mutually dependent  slider and numeric inputs 
output$Param_s = renderUI({
sliderInput(inputId = "param_slide",
            label= "My input parameter",
            value= input$param_numeric,
            min=1,
            max=200)
})

output$Param_n = renderUI({
numericInput(inputId = "param_numeric",
             label= "My input parameter",
             value= input$param_slide,
             min=1,
             max=200)
})

updateSliderInput(session,"param_slide", value = 60)
updateNumericInput(session,"param_numeric", value = 60 )

})

CodePudding user response:

You should try to avoid re-rendering if possible. It's faster to update existing inputs:

library(shiny)

ui <- fluidPage(
  sliderInput(
    inputId = "param_slide",
    label = "My input parameter",
    value = 60,
    min = 1,
    max = 200
  ),
  numericInput(
    inputId = "param_numeric",
    label = "My input parameter",
    value = 60,
    min = 1,
    max = 200
  )
)

server <- function(input, output, session) {
  observeEvent(input$param_numeric, {
    updateSliderInput(session, "param_slide", value = input$param_numeric)
  }, ignoreInit = TRUE)
  observeEvent(input$param_slide, {
    updateNumericInput(session, "param_numeric", value = input$param_slide)
  }, ignoreInit = TRUE)
}

shinyApp(ui, server)

Btw. using inter-related inputs in shiny you're skating on thin ice. Please see this and this.

CodePudding user response:

That's a clever way of linking them. I've done it with observeEvent functions which was significantly clumsier.

As for the problem, I think it is caused by there being no input$param_value when the slider is first created.

You can replace the value line for the slider with this:

value= if(!is.na(input$param_numeric)){input$param_numeric}else{60}

which works for me and lets you get rid of your update statements at the end. Although for me your actual code seems to work too, although it gives an error message in the console.

--

Edit: I was mistaken about what is going on. It works for me either way and the if statement in my answer wasn't helping. My if version works with exists instead:

value= if(exists('input$param_numeric')){input$param_numeric}else{60}

But given your original code works for me this isn't much proof of anything

  • Related