简体   繁体   English

R Shiny:eventReactive和renderUI交互

[英]R Shiny: eventReactive and renderUI interaction

I'm trying to do a similar app to this one Shiny: dynamic dataframe construction; 我正在尝试与此Shiny做类似的应用程序:动态数据框构造; renderUI, observe, reactiveValues. renderUI,观察reactiveValues。 I would like to create an option to select, for example, the number of cylinders in the list. 我想创建一个选项来选择例如列表中的气缸数。 So that the table is automatically updated after selecting the number. 这样,在选择编号后表格将自动更新。 Could someone explain to me what I'm doing wrong? 有人可以向我解释我在做什么错吗? As you can see on the graphics program does not work well. 正如您在图形程序上看到的那样,它不能很好地工作。 在此处输入图片说明

My code: 我的代码:

ui: 用户界面:

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'))
      )
    )
  )
))

server: 服务器:

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]
  })
})

Here is the dashboard for your example: 这是您的示例的仪表板:

# 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)

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

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