Rshiny中的动态输入

时间:2018-06-19 16:06:37

标签: r dynamic shiny

我试图做出反应性输入以获得输出,然后将该输出用作输入来计算函数。这只是我想做的一个例子。我无法删除产品功能。

您能建议如何显示输出。 因此,乘积必须是小时和最大天数的结果,具体取决于天数的选择。

library(shiny)

ui = fluidPage(
  selectInput('p_id','ID:', c(111,222,333)),
  uiOutput('uiID'),
  uiOutput('uiID1'),
  # uiOutput('val'),
  textOutput("values4.5"),
  submitButton("apply_changes")

)

server = function(input, output, session) {

  maxdays <- reactive({
    if(input$p_id %in% c(111)){
      x = c(1,2)
    }else{
      if(input$p_id %in% c(222)){
        x = 2
      }else
        x = 3 
    }
    return(x)
  })

  hours <- reactive({
    if(input$p_id %in% c(111)){
      x = c(20,10)
    }else{
      if(input$p_id %in% c(222)){
        x = 20
      }else
        x = 30 
    }
    return(x)
  })

  output$uiID <- renderUI({
    selectInput('days','Days:', choices=maxdays())
  })

  output$uiID1 <- renderUI({
    selectInput('days','Days:', choices=hours())
  })

 product<-function(p_id,maxdays,hours){
   prod=p_id*maxdays*hours
   return(prod)
 }


complete_ans <-reactive({
   answer <- product(input$p_id,input$maxdays,input$hours)
   values4.5 = answer
output=list(values4.5=values4.5)

})

 output$values4.5 <- renderText({complete_ans()[['values4.5']]})
}

runApp(shinyApp(ui = ui, server = server))

2 个答案:

答案 0 :(得分:0)

我认为这比较干净:

library(shiny)

product <- function(p_id,maxdays,hours){
        prod <- as.numeric(p_id)*as.numeric(maxdays)*as.numeric(hours)
        return(prod)
}

ui = fluidPage(
        selectInput('p_id','ID:', c(111,222,333)),
        selectInput('maxdays','Days:', choices=NULL),
        selectInput('hours','Hours:', choices=NULL),

        uiOutput('uiID'),
        uiOutput('uiID1'),
        actionButton("apply_changes","apply_changes"),
        textOutput("values4_5")

)

server = function(input, output, session) {

        observeEvent(input$p_id,{
                if(input$p_id == 111){
                        v1 <- v2 <- c(1,2);

                }
                else if(input$p_id == 222){
                        v1 <- 2;v2 <- 20
                }
                else{
                        v1 <- 3;v2 <- 30
                }
                updateSelectInput(session,"maxdays",choices = v1)
                updateSelectInput(session,"hours",choices = v2)
        })

        complete_ans <-eventReactive(input$apply_changes,{
                answer <- product(input$p_id,input$maxdays,input$hours)
                list(values4.5=answer)
        })
        output$values4_5 <- renderText({complete_ans()[['values4.5']]})
}

runApp(shinyApp(ui = ui, server = server))

enter image description here

答案 1 :(得分:0)

您需要在'product'函数的参数中传递数字参数。这可以通过使用as.numeric()强制完成。另外,您应该与使用'<-'代替'='

library(shiny)

ui = fluidPage(
  selectInput('p_id','ID:', c(111,222,333)),
  uiOutput('uiID'),
  uiOutput('uiID1'),
  # uiOutput('val'),
  textOutput("values4.5"),
  submitButton("apply_changes")

)

server = function(input, output, session) {

  maxdays <- reactive({
    if(input$p_id %in% c(111)){
      x = c(1,2)
    }else{
      if(input$p_id %in% c(222)){
        x = 2
      }else
        x = 3 
    }
    return(x)
  })

  hours <- reactive({
    if(input$p_id %in% c(111)){
      x = c(20,10)
    }else{
      if(input$p_id %in% c(222)){
        x = 20
      }else
        x = 30 
    }
    return(x)
  })

  output$uiID <- renderUI({
    selectInput('days1','Days:', choices=maxdays())
  })

  output$uiID1 <- renderUI({
    selectInput('days2','Days:', choices=hours())
  })

  product <- function(p_id,maxdays,hours){
    prod <- p_id * maxdays * hours
    return(prod)
  }


  complete_ans <- reactive({
    answer <- product(as.numeric(input$p_id), as.numeric(maxdays()), as.numeric(hours()))
    values4.5 <- answer
    output1 <- list(values4.5=values4.5)
    return(output1)
  })

  output$values4.5 <- renderText({
    complete_ans()[['values4.5']]
    })
}

runApp(shinyApp(ui = ui, server = server))