R 对局部函数的表达式调用

R 对局部函数的表达式调用,r,function,shiny,R,Function,Shiny,使用Shiny构建应用程序时,我遇到了对本地定义函数的被动调用问题。 一个仿真示例: <server.R> shinyServer(function(input, output) { myfunc <- function(x) x+1 myreac <- reactive({ y <- myfunc(input$var) y }) # print y value test output$tex

使用Shiny构建应用程序时,我遇到了对本地定义函数的
被动调用问题。
一个仿真示例:

  <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)
  })  
我得到的输出:基本上为空:

This is your output:

似乎没有任何东西是由反应器输出的。如果我能得到任何帮助,我将不胜感激。

还有几个额外的逗号,但除此之外,如果您将
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)
        })
    })
)
库(闪亮)
shinyApp(
shinyUI(fluidPage)(
titlePanel(“新应用程序”),
侧边栏布局(
侧栏面板(
帮助文本(“我的应用”),
选择输入(“变量”,
label=“选择一个选项:”,
选项=列表(“1”、“2”、“3”),
已选择=“1”)
),
主面板(
文本输出(“文本”)
)
)
)),
shinyServer(函数(输入、输出){

myfunc@bunk:spot on.在原始代码中,选择是字符,只是认为类中没有隐含的假设,所以必须使用
quote
。下一个问题是,所有选择都是同时计算的……这有什么原因吗?我不确定你的意思。你看到输出了吗?是的,输出同时打印所有三个选择同时。在上面的示例中,
这是您的输出:2,这是您的输出:3,这是您的输出:4
,尽管
选择input
设置为
1
(在用户界面上).当你复制并粘贴上面的内容时,你得到了吗?我一次只看到一个输出,在实际代码中得到了它。我在
myfunc
中使用了grep,它确实抱怨
模式的长度>1…
是一条警告消息。似乎所有3个“选项”都同时得到了评估?
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)
        })
    })
)