【问题标题】:R Shiny: eventReactive and renderUI interactionR Shiny:eventReactive 和 renderUI 交互
【发布时间】:2017-11-26 17:44:34
【问题描述】:

我正在尝试做一个与 Shiny 类似的应用程序:动态数据帧构造; renderUI,观察,reactiveValues。我想创建一个选项来选择例如列表中的气缸数。这样表格在选择数字后会自动更新。有人可以向我解释我做错了什么吗?正如你所看到的图形程序不能正常工作。

我的代码:

用户界面:

library(shiny)

hw <- mtcars

shinyUI(fluidPage(
  title = 'Examples of DataTables',
  sidebarLayout(
    sidebarPanel(
      
      radioButtons(
        inputId="radio",
        label="Variable Selection Type:",
        choices=list(
          "All",
          "Manual Select"
        ),
        selected="All"),
      
      conditionalPanel(
        condition = "input.radio != 'All'",
        selectizeInput(
          'show_vars', 
          'Columns in cars to show:',
          choices=names(hw), 
          selected = names(hw),
          multiple =TRUE),
        
        uiOutput("category1")
      )
      
    ),
    mainPanel(
      verbatimTextOutput("summary"), 
      tabsetPanel(
        id = 'dataset',
        tabPanel('hw', dataTableOutput('mytable1'))
      )
    )
  )
))

服务器:

library(shiny)
library(ggplot2)

shinyServer(function(input, output) {
  
  
  Data <- reactive({
    
    if(input$radio == "All"){
      hw
    } else {
      hw[,input$show_vars,drop=FALSE]
    }
    
  })
  
  output$category1 <- renderUI({
    selectizeInput('cat1', 'Select cyl:', choices = c("All",sort(as.character(unique(hw$cyl)))),selected = "All")

  })

  df_subset <- eventReactive(input$cat1,{
    if(input$cat1=="All") {df_subset <- hw}
    else{df_subset <- hw[hw$Position == input$cat1,]}
  })


  output$mytable1 <- renderDataTable({
     df_subset()
      hw[, input$show_vars, drop = FALSE]
  })
})

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    这是您的示例的仪表板:

    # Packages
    library(shiny)
    library(DT)
    library(dplyr)
    
    # UI
    ui <- fluidPage(   
      # Application title
      titlePanel("Example"),
    
      # Sidebar
      sidebarLayout(
         sidebarPanel(selectInput("si_cylinders", "Cylinders", 
                     choices = sort(unique(mtcars$cyl)), selected = "4")),
         mainPanel(dataTableOutput("dt_mtcars"))))
    
      # Server
      server <- function(input, output) {
    
      # Data set for output
      df_mtcars <- reactive({
        # 1. Read UI element
        cylinder_selected <- as.numeric(input$si_cylinders[1])
    
        # 2. Filter data set
        df <- mtcars %>% filter(cyl == cylinder_selected)
    
       # 3. Return result
       return(df)
      })
    
      # 2. Data table for output
      output$dt_mtcars <- renderDataTable({
        datatable(df_mtcars())
      })
    
     }
    
    # Run the application 
    shinyApp(ui = ui, server = server)
    

    【讨论】:

    • 好的,但是我如何将它与我的代码连接起来,因为它给我带来了最大的问题。你能帮我吗?
    • 收到“+1”作为答案可能会很好,至少就像我所做的那样:)
    • 谢谢。您可以通过添加新的 UI 元素来根据您的任务扩展我的代码
    猜你喜欢
    • 2020-08-05
    • 2019-03-07
    • 1970-01-01
    • 1970-01-01
    • 2012-10-18
    • 2018-04-14
    • 2020-09-29
    • 2011-12-13
    • 2022-11-20
    相关资源
    最近更新 更多