动态地将selectInput值从UI传递到R中的Server代码

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了动态地将selectInput值从UI传递到R中的Server代码相关的知识,希望对你有一定的参考价值。

R闪亮脚本中服务器中的process_map()函数创建图图像,如下所示。我的要求是有两个属性“FUN”和“units”是performance()函数的一部分。它们具有标准的四个值,每个值在PickerInput ID的Case4和Case5下面的ui代码中可用。目前,我很难编写创建地图的值,你能帮助我在服务器代码中使用id并使其动态化,这样当我在PickerInput中选择值时,公式直接获取值。谢谢,请帮助。

library(shiny)
library(shinydashboard)
library(bupaR)
library(processmapR)
library(lubridate)
library(dplyr)
library(edeaR)
library(shinyWidgets)
library(DiagrammeR)
ui <- dashboardPage(
dashboardHeader(title = "Diagram Plot",titleWidth = 290),
dashboardSidebar(width = 0),
dashboardBody(
tabsetPanel(type = "tab",
            tabPanel("Overview", value = 1,

                     box(
                       column(1, 
                              dropdown(
                                pickerInput(inputId = "resources", 
                                            label = "", 
                                            choices = c("Throughput Time"), 
                                            choicesOpt = list(icon = c("fa fa-bars", 
                                                                       "fa fa-bars", 
                                                                       "fa fa-safari")), 
                                            options = list(`icon-base` = "")),
                                circle = FALSE, status = "primary", icon = icon("list", lib = "glyphicon"), width = "300px"
                              ),

                              conditionalPanel(
                                condition = "input.resources == 'Throughput Time' ",


                                tags$br(),
                                tags$br(),
                                tags$br(),
                                dropdown(
                                  pickerInput(inputId = "Case4", 
                                              label = "Select the Process Time Summary Unit", 
                                              choices = c("min","max","mean","median"), options = list(`actions-box` = TRUE), 
                                              multiple = F),
                                  circle = FALSE, status = "primary", icon = icon("eye-close", lib = "glyphicon"), width = "300px"
                                ),
                                tags$br(),
                                tags$br(),
                                tags$br(),
                                dropdown(
                                  pickerInput(inputId = "Case5", 
                                              label = "Select the Process Time Unit", 
                                              choices = c("mins","hours","days","weeks"), options = list(`actions-box` = TRUE), 
                                              multiple = F, selected  = "days"),
                                  circle = FALSE, status = "primary", icon = icon("eye-close", lib = "glyphicon"), width = "300px"
                                ))),
                       title = "Process Map", 
                       status = "primary",height = "575", width = "500",
                       solidHeader = T,
                       column(10,grVizOutput("State")),
                       align = "left")


                     ),

            id= "tabselected"
            )))
  server <- function(input, output) { 
  output$State <- renderDiagrammeR(
  {

    if(input$resources == "Throughput Time")
      patients %>% process_map(performance(FUN = mean,units = "days"))
    else
    return()
    })}
  shinyApp(ui, server)

Diagram Image

答案

测试一下:

output$State <- renderDiagrammeR({

      if(input$resources == "Throughput Time")
      {
        if(input$Case4=="mean"){
        patients %>% process_map(performance(FUN = mean,units = input$Case5))}
      else if(input$case4=="min"){
        patients %>% process_map(performance(FUN = min,units = input$Case5))
      }else if(input$case4=="max"){
        patients %>% process_map(performance(FUN = max ,units = input$Case5))
      }else{
        patients %>% process_map(performance(FUN = median ,units = input$Case5))
      }

        }else
        return()
  })

或者你可以使用这个:

patients %>% 
process_map(performance(FUN = eval(parse(text=input$Case4)) ,units = input$Case5))

请享用;)

这是一个示例:

library(shiny)

ui <- fluidPage(
selectInput(inputId = "func", label = "Choose The Function", choices = c("mean", "sum", "median"))
,
textOutput("text")
)


server <- function(input, output, session) {
  main_data <- reactive({
    data.frame(a= rnorm(100), b=rnorm(100) )
  })

  output$text <- renderText({
    df <- main_data()

   apply(df,2, FUN =  eval(parse(text=input$func)) )
  })

}
shinyApp(ui = ui, server = server)
另一答案

您可以使用do.call从其名称中调用函数,请参阅下面的示例。您可以通过在do.call函数的列表中添加参数来添加参数,例如list(x,units=input$Case5)


enter image description here


library(shiny)

x=c(1,2,3,4,5,6,7)

ui <- fluidPage(
 selectInput('select','Select Function: ', choices=c('mean','max','min','median')),
 textOutput('text')
)

server <- function(input,output)
{
  output$text <- renderText({
    result = do.call(input$select, list(x))
    paste0('The ', input$select, ' of [', paste(x,collapse=', '),'] is ', result)
  })
}

shinyApp(ui,server)

希望这可以帮助!

以上是关于动态地将selectInput值从UI传递到R中的Server代码的主要内容,如果未能解决你的问题,请参考以下文章

Xcode:如何轻松地将我的 Int 值从 UIview 传递到下一个 UIViewcontroller?

根据 R Shiny 中的其他选择动态更新两个 selectInput 框

通过 Laravel 中的 AJAX 发布请求将值从一个视图传递到另一个视图

错误:所有观察都在同一组中,同时从闪亮的ui列表中动态过滤R模型

在 Material UI 中,如何动态地将颜色传递给我的样式?

Rshiny:如何将 renderUI 输出传递给 Selectinput 中的 chocies 参数