访问R中模块化函数内部的值

时间:2016-08-07 16:48:46

标签: r shiny

我试图在R中使用模块化函数输出总数,并想知道如何访问从`source(" slider.R")中的函数返回的变量。

以下闪亮的应用程序会运行,但不会输出总共四个滑块:

#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)

滑块的功能是:

# 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})
}

我尝试了slider1-number + slider2-number + slider3-number + slider4-number,但这被解释为&#34; - &#34;运算符而不是变量名。这是一个截图,红色文本应该从四个滑块号输出总数:

这些是我无法弄清楚的值

enter image description here

1 个答案:

答案 0 :(得分:3)

您应该将ID作为字符串传递。有两种方法:

output$total <- renderText({ 
    input[["slider1-slider"]] + input[["slider2-slider"]] + 
    input[["slider3-slider"]] + input[["slider4-slider"]]
  })

output$total <- renderText({ 
    input$'slider1-slider' + input$'slider2-slider' + 
    input$'slider3-slider' + input$'slider4-slider' 
})

完整代码:

#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)