这是一个简单的闪亮应用程序,可以正常工作:ui.R
library(shiny)
shinyUI(bootstrapPage(
numericInput("mymun", "enter a number",0),
textOutput("mytext")
))
server.R
library(shiny)
test <- function(input, output){
renderText(paste("this is my number:",input$mymun))
}
shinyServer(function(input, output) {
output$mytext <- test(input, output)
})
但是,如果我将函数调用的结果放在一个临时变量中,则该应用程序将失败
server.R
library(shiny)
test <- function(input, output){
renderText(paste("this is my number:",input$mymun))
}
shinyServer(function(input, output) {
tmp <- test(input, output)
output$mytext <- tmp
})
错误消息是:
Error in substitute(value)[[2]] :
object of type 'symbol' is not subsettable
有谁能提供线索说明为什么第二个失败了,为什么第一个没有失败?
我猜这是由于表达式处理和闪亮的服务器的响应逻辑引起的,但是我不清楚。
最佳答案
真正弄清楚这一点的最佳方法是重新阅读http://www.rstudio.com/shiny/lessons/Lesson-6/ + http://www.rstudio.com/shiny/lessons/Lesson-7/ + http://rstudio.github.io/shiny/tutorial/#scoping,以确保您完全了解反应性和作用域(将原始教程文本发布在SO中毫无意义)。
为了使内容接近您想要做的事情,您需要在server.R
中执行以下操作:
library(shiny)
shinyServer(function(input, output) {
test <- reactive({
return(paste("this is my number:",input$mymun))
})
output$mytext <- renderText({
tmp <- test()
return(tmp)
})
})
关于r - Shiny :为什么在服务器端使用临时变量进行输出失败?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22831287/