R Shiny 的自定义功能

标签 r shiny

我试图在我的自定义函数中调用 react 函数,但我做不到。执行此操作的最佳方法是什么,这是简化的代码,我不能将所有内容都作为功能参数传递,因为传递的内容太多了。

  library(shiny); 
  myf<-function(){ return(psd()$y) }
  shinyApp( ui = textOutput("test2"), 
      server = function(input, output) {
          psd<-reactive({ return(data.frame(x=10,y=30)) })
          output$test2 <- renderText({ myf() })
        }
      )

此代码生成错误:myf() 中的错误:找不到函数“psd” 调用使用 shinyserver 中其他函数的自定义函数的最佳方法是什么?

最佳答案

这取决于您是否希望您的函数也具有反应性。

在 react 性使用的情况下:

myf <- reactive({return(psd()$y})

如果你不希望它是 react 性的使用

 myf <- function() {return(isolate(psd$y))}

您的响应式(Reactive)表达式没有反应式值(如 input$)。因此它不会被创建。要更好地了解 react 性,请阅读 this awesome instruction .


此外,也许您会尝试避免硬编码并尝试在您的函数中添加您想要调用的响应式函数作为参数,如下所示:

myf<-function(func){ return(func()$y) }

然后:

output$test2 <- renderText({ myf(psd) })

关于R Shiny 的自定义功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32462335/

相关文章:

使用 XLConnect 读取包含缺失值的 .xls 文件

r - 使用 "scale_y_continuous(trans = ' 反向时无法在 ggplot 中使用 ylimit')"

r - 如何在 shiny 中使用带有 reactiveValues 的 debounce

r - 使用带有 NA 的 checkboxGroupInput 选择

css - R : How to change default CSS cluster classes 的传单

javascript - 使用 R 从 js 源或 HTML 标签中提取值?

r - R 中用于文本分类的特征散列

R:基于因子或数字的聚合

r - 基于 R Shiny "html template"的应用程序中的 highcharter 集成

r - 数据表 : apply different formatStyle to each column