繁体   English   中英

R Studio 中的动态函数 - 在 R Shiny 上部署

[英]Dynamic Function in R Studio - Deployment on R Shiny

我在 R 中有一个完全可用的函数,它接受一些输入参数并返回一个矩阵。 我想在 Shiny 应用程序上构建相同的应用程序。 函数的输入参数应该是滑块输入,输出应该是动态的。

我尝试对不同的输入使用反应函数。

     names<- c("A", "B", "C", "D", "E")
     ages<- c(25,24,23,22,21)
     salary<- c(60000,32000,37000,15000,15000)
     data<- data.frame(names,ages, salary)
     header<- dashboardHeader(title="Dynamic")

     sidebar<- dashboardSidebar(sidebarMenu(
     id="id",
     sliderInput("age","AGE",min = 0, max = 50, value = "20")
     ))

     body<- dashboardBody(box(
     dataTableOutput("view")
     ))

     ui<- dashboardPage(header, sidebar, body)

     try_function<- function(age){
     to_show<- data %>% filter(ages>age)
     return(to_show) 
     }

    server<- function(input, output, session) {   
      output$view <- renderDataTable({
        reactive({try_function(input$age)
        })    
      })     
    }

    shinyApp(ui, server)

任何人都可以对此提供帮助。 假设我有多个输入,那么这段代码应该如何变化。

我希望输出会根据闪亮的应用程序上的参数而有所不同。 这不会给我任何错误,但也不会在 Shiny 上显示任何输出。

一般来说,我们永远不应该在render表达式中使用reactive调用。 当我们对返回值感兴趣或函数没有“副作用”时,通常会使用反应式表达式。 删除reactive()调用,解决了这里的问题。

library(shiny)
library(tidyverse)
library(shinydashboard)

names<- c("A", "B", "C", "D", "E")
ages<- c(25,24,23,22,21)
salary<- c(60000,32000,37000,15000,15000)
data<- data.frame(names,ages, salary)
header<- dashboardHeader(title="Dynamic")

sidebar<- dashboardSidebar(sidebarMenu(
  id="id",
  sliderInput("age","AGE",min = 0, max = 50, value = 20)
))

body<- dashboardBody(box(
  dataTableOutput("view")
))

ui<- dashboardPage(header, sidebar, body)

try_function<- function(age){
  to_show<- data %>% filter(ages>age)
  return(to_show) 
}

server<- function(input, output, session) {   

  output$view <- renderDataTable({
    try_function(input$age)
  })     

}

shinyApp(ui, server)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM