闪亮的反应式表达调用本地函数

时间:2015-10-16 22:59:57

标签: r function shiny

使用Shiny构建应用程序,我对reactive调用本地定义的函数存在问题。 一个模拟的例子:

  <server.R>
  shinyServer(function(input, output) { 
  myfunc <- function(x) x+1
  myreac <- reactive({
          y <- myfunc(input$var)
          y
  })  
  # print y value test
  output$text <- renderText({ 
  myreac <- myreac()
  paste("This is your output:",myreac)
  })  

Shiny UI

  shinyUI(fluidPage(
  titlePanel("New App"),  
  sidebarLayout(
  sidebarPanel(
  helpText("My App"),      
  selectInput("var", 
              label = "Choose an option:",
              choices = list("1", "2",
                             "3"),
              selected = "1"),
      ),

mainPanel(
  textOutput("text"),
  )   
 )
))  

我得到的输出:基本上是空白的:

This is your output:

似乎没有任何东西被反应输出。我能得到的任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:1)

还有一些额外的逗号,但除此之外,如果您将x转换为数字,它应该有效,

library(shiny)
shinyApp(
    shinyUI(fluidPage(
        titlePanel("New App"),  
        sidebarLayout(
            sidebarPanel(
                helpText("My App"),      
                selectInput("var", 
                            label = "Choose an option:",
                            choices = list("1", "2", "3"),
                            selected = "1")
            ),
            mainPanel(
                textOutput("text")
            )
        )
    )),
    shinyServer(function(input, output) { 
        myfunc <- function(x) as.numeric(x)+1
        myreac <- reactive({
            y <- myfunc(input$var)
            y
        })

        ## print y value test
        output$text <- renderText({ 
            myreac <- myreac()
            paste("This is your output:", myreac)
        })
    })
)