【问题标题】:how to make my ui in my shinyapp interactive如何使我的闪亮应用程序中的用户界面具有交互性
【发布时间】:2023-12-05 10:30:01
【问题描述】:

我的应用程序的交互式用户界面只能组合使用。当我想更改 category_1 或 category_2 的数据集的值时,我总是必须更改“Info”字段的 select_Input。如果我只想替换 Category_1 或 Category_2 的值而不更改 Info_input 应用程序崩溃。所以我永远无法在不更改 Info_input 的情况下计算出有趣的值。但为什么呢?

library(shiny)

a <- c("E","D","E","E")
b <- c(10,20,10,10)
d <- c("aa","a","aa","b")
dd<- c(2,22,2, 54)
e <- c("house","car", "color","house")
f <- c(1,3,4,1)


df <- data.frame(cat1 =a, cat1_value=b, info=d, info_value= dd, cat2=e, cat2_value=f)


ref <- c("aaa","aa","a", "bb","b", "c")
val <- c(0.4, 2, 22, 43, 54, 0.6)

df_reference <- data.frame(reference =ref, value=val)


ui <- fluidPage(
  
  titlePanel("Test Explorer"),
  sidebarLayout(
    sidebarPanel(
      selectInput("Cat2", "Category_2",
                  c("",unique(df$cat2))),
      
      uiOutput("ui2"),
      selectInput("Cat1", "Category_1",
                        c("",unique(df$cat1))),
      
      uiOutput("ui3"),
    
      selectInput("Info", "Infoo",
                        c("",unique(df$info))),
      
      column(3, actionButton("re", label = "replace"),
                         
                         
           actionButton("calc", label = "calculate"))),
    mainPanel(
      verbatimTextOutput("result")
    )
  )
)

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

  
  output$ui2 <- renderUI({
    if (is.null(input$Cat2)) # 
      return()
    
    switch(input$Cat2,
           "house" = numericInput("tt", "tt in %",
                                          100),
           "car" = numericInput("tt", "tt in %",
                                     40),
           "color" = numericInput("tt", "tt in %",
                                         60)
           
    )
  })
  
  
  
  
  output$ui3 <- renderUI({
    if (is.null(input$Cat1)) #
      return()
    
    
    switch(input$Cat1,
           
           "E" = numericInput("bb", "bbin %",
                                   10),
           "D" = numericInput("bb", "bb in %",
                                        20)
           
           
    )
  })
  
  
  observeEvent(input$re, ({            ## mit reactive und reactiveValues lösen ## Warum so dumm?
    
    
    df$cat2_value[df$cat2 == input$Cat2] <<- input$tt/100   
    df$cat1_value[df$cat1 == input$Cat1] <<- input$bb/100
    df$info <<- input$Info
    df$info_value <<- df_reference[df_reference$reference %in% input$Info,]$value 
  
  
  
  
}))
  
  observeEvent(input$calc, ({
    
    
    output$result <- renderText({
      
      sum(df$info_value * df$cat1_value * df$cat2_value)
      
    })
    
  }))
  
}
    

shinyApp(ui, server)

【问题讨论】:

  • 我想问题是,如果你不为info 选择任何东西,input$infoNULL 什么会破坏 df 中的分配。你到底想用你的应用程序做什么?计算/反应对我来说似乎有点奇怪,直接用原始数据修改全局 data.frame。我会为此使用额外的变量(可能是反应式或反应式值)。
  • 但它也应该与isolate(input$info) 一起使用,但它没有。是的,我可以使用反应式,但这也不能解决问题。
  • 不,isolate(input$info) 只是防止反应性依赖于input$info,这无关紧要,因为它位于依赖于另一个输入的observeEvent 中。 input$info 仍然是 NULL
  • 我不知道如何解决它

标签: r user-interface shiny interactive


【解决方案1】:

如果我这样做 - 现在应用程序不会对 Category_2 做出反应。有完整的工作解决方案吗?

library(shiny)

a <- c("E","D","E","E")
b <- c(10,20,10,10)
d <- c("aa","a","aa","b")
dd<- c(2,22,2, 54)
e <- c("house","car", "color","house")
f <- c(1,3,4,1)


df <- data.frame(cat1 =a, cat1_value=b, info=d, info_value= dd, cat2=e, cat2_value=f)


ref <- c("aaa","aa","a", "bb","b", "c")
val <- c(0.4, 2, 22, 43, 54, 0.6)

df_reference <- data.frame(reference =ref, value=val)


ui <- fluidPage(
  
  titlePanel("Test Explorer"),
  sidebarLayout(
    sidebarPanel(
      selectInput("Cat2", "Category_2",
                  c("",unique(df$cat2))),
      
      uiOutput("ui2"),
      selectInput("Cat1", "Category_1",
                        c("",unique(df$cat1))),
      
      uiOutput("ui3"),
    
      selectInput("Info", "Infoo",
                        c("",unique(df$info))),
      
      column(3, actionButton("re", label = "replace"),
                         
                         
           actionButton("calc", label = "calculate"))),
    mainPanel(
      verbatimTextOutput("result")
    )
  )
)

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

  
  output$ui2 <- renderUI({
    if (is.null(input$Cat2)) # 
      return()
    
    switch(input$Cat2,
           "house" = numericInput("tt", "tt in %",
                                          100),
           "car" = numericInput("tt", "tt in %",
                                     40),
           "color" = numericInput("tt", "tt in %",
                                         60)
           
    )
  })
  
  
  
  
  output$ui3 <- renderUI({
    if (is.null(input$Cat1)) #
      return()
    
    
    switch(input$Cat1,
           
           "E" = numericInput("bb", "bbin %",
                                   10),
           "D" = numericInput("bb", "bb in %",
                                        20)
           
           
    )
  })
  

  
  
  observeEvent(input$re, ({           
    
   # a <<- ifelse(is.null(input$Info),df$info, input$Info)
    
    df$cat2_value[df$cat2 == input$Cat2] <<- input$tt/100   
    df$cat1_value[df$cat1 == input$Cat1] <<- input$bb/100
   # df$info_value <<- df_reference[df_reference$reference %in% a,]$value

  
}))
  
  observeEvent(c(input$re,input$Info), ({
     req(input$Info) 
    df$info_value <<- df_reference[df_reference$reference %in% input$Info,]$value
    
  })  
  )  
  
  
  observeEvent(input$calc, ({
    
    output$result <- renderText({
      

      sum(df$info_value * df$cat1_value * df$info_value)
      
    })
    
  }))
  
}
    

shinyApp(ui, server)

【讨论】:

    最近更新 更多