I'm trying to output a total from using a modularized function in R and want to know how to access the variable returned from the function which is in `source("slider.R").
The following shiny app runs, but doesn't output a total of the four sliders:
#app.R
library(shinydashboard)
library(shiny)
# function code for slider
source("slider.R")
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(disable = FALSE),
dashboardBody(
# Foucs of Part One of Tutorial
SliderTextUI("slider1"),
SliderTextUI("slider2"),
SliderTextUI("slider3"),
SliderTextUI("slider4"),
h1(textOutput("total"))
)
)
server <- function(input, output) {
callModule(SliderText, "slider1")
callModule(SliderText, "slider2")
callModule(SliderText, "slider3")
callModule(SliderText, "slider4")
output$total <- renderText({
## Four variables to add for total
## These are the values I can't figure out
slider1 + slider2 + slider3 + slider4
})
}
shinyApp(ui, server)
And the function for the slider is:
# Return Shiny UI Elements to build UI
SliderTextUI <- function(id) {
#namespace function
ns <- NS(id)
tagList(
#wrap slider id with namespace, will return id-slider
sliderInput(ns("slider"), "Slide Me", 0,100,1),
box(
#wrap text output with namespace, will return id-number
textOutput(ns("number"))
)
)
}
SliderText <- function(input, output, session) {
output$number <- renderText({input$slider})
}
I tried slider1-number + slider2-number + slider3-number + slider4-number but this gets interpreted as a "-" operator rather than a variable name. Here is a screenshot, the red text is supposed to output the total from the four slider numbers:

You should pass IDs as character strings. There are two ways of doing it:
output$total <- renderText({
input[["slider1-slider"]] + input[["slider2-slider"]] +
input[["slider3-slider"]] + input[["slider4-slider"]]
})
or
output$total <- renderText({
input$'slider1-slider' + input$'slider2-slider' +
input$'slider3-slider' + input$'slider4-slider'
})
Full code:
#app.R
library(shinydashboard)
library(shiny)
# Return Shiny UI Elements to build UI
SliderTextUI <- function(id) {
#namespace function
ns <- NS(id)
tagList(
#wrap slider id with namespace, will return id-slider
sliderInput(ns("slider"), "Slide Me", 0,100,1),
box(
#wrap text output with namespace, will return id-number
textOutput(ns("number"))
)
)
}
SliderText <- function(input, output, session) {
output$number <- renderText({input$slider})
}
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(disable = FALSE),
dashboardBody(
# Foucs of Part One of Tutorial
SliderTextUI("slider1"),
SliderTextUI("slider2"),
SliderTextUI("slider3"),
SliderTextUI("slider4"),
h1(textOutput("total"))
)
)
server <- function(input, output) {
callModule(SliderText, "slider1")
callModule(SliderText, "slider2")
callModule(SliderText, "slider3")
callModule(SliderText, "slider4")
output$total <- renderText({
## Four variables to add for total
## These are the values I can't figure out
input[["slider1-slider"]] + input[["slider2-slider"]] +
input[["slider3-slider"]] + input[["slider4-slider"]]
})
}
shinyApp(ui, server)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With