【问题标题】:Shiny reactive subset闪亮的反应子集
【发布时间】:2018-01-31 17:57:11
【问题描述】:

我正在尝试对三列(StockCode、Price、label)的数据框进行子集化

但我不得不使用响应式,我的问题是如何呈现标签

我需要像 renderText(dataset()$label) 这样的想法

ui.R

library(shiny)

# Define UI for app that draws a histogram ----
ui <- fluidPage(

  # App title ----
  titlePanel("Hello Shiny!"),

  # Sidebar layout with input and output definitions ----
  sidebarLayout(

    # Sidebar panel for inputs ----
    sidebarPanel(
     uiOutput("codePanel") 


    ),

    # Main panel for displaying outputs ----
    mainPanel(

     textOutput("text")

    )
  )
)

服务器.R

server <- function(input, output) {

output$codePanel<-renderUI({

selectInput("codeInput",label ="choose code",choices =data$StockCode)  


})


dataset<-reactive({ 

subset(data,data$StockCode==input$codeInput)  

})


 output$text<-renderText(dataset())

}

【问题讨论】:

  • 你能提供一个数据集吗?你能定义如何对你的数据框进行子集化吗?

标签: r shiny


【解决方案1】:

如果我们希望显示data.frame 输出,请使用DT 中的renderDataTable。为了重现性,使用了内置数据集iris

library(shiny)
library(DT)
ui <- fluidPage(
  # App title ----
  titlePanel("Hello Shiny!"),
  sidebarLayout(

    # Sidebar panel for inputs ----
    sidebarPanel(
      uiOutput("codePanel") 
    ),

    mainPanel(

      DT::dataTableOutput("text")

    )
  )
)

server <- function(input, output) {


  filt <- selectInput("codeInput",label ="choose code",
                        choices = as.list(unique(iris$Species)))

  output$codePanel <- renderUI({ filt

  })

  dataset<-reactive({ 

    subset(iris, Species == input$codeInput)  

  })


  output$text<-renderDataTable(dataset())


}

shinyApp(ui = ui, server = server)

-输出


数据集的行可以一起粘贴到一个字符串中,以在renderText中使用

ui <- fluidPage(
  # App title ----
  titlePanel("Hello Shiny!"),
  sidebarLayout(

    # Sidebar panel for inputs ----
    sidebarPanel(
      uiOutput("codePanel") 
    ),

    mainPanel(

      verbatimTextOutput("text")

    )
  )
)




server <- function(input, output) {


  filt <- selectInput("codeInput",label ="choose code",
                        choices = as.list(unique(iris$Species)))

  output$codePanel <- renderUI({ filt

  })

  iris$Species <- as.character(iris$Species)
  dataset<-reactive({ 

    do.call(paste, c(collapse = "\n", rbind(colnames(iris), subset(iris, Species == input$codeInput))))

  })


  output$text<-renderText(dataset())


}




shinyApp(ui = ui, server = server)

-输出


或者使用htmlOutputrenderUI

ui <- fluidPage(
  # App title ----
  titlePanel("Hello Shiny!"),
  sidebarLayout(

    # Sidebar panel for inputs ----
    sidebarPanel(
      uiOutput("codePanel") 
    ),

    mainPanel(

      htmlOutput("text")

    )
  )
)

server <- function(input, output) {


  filt <- selectInput("codeInput",label ="choose code",
                        choices = as.list(unique(iris$Species)))

  output$codePanel <- renderUI({ filt

  })

  dataset<-reactive({ 

    do.call(paste, c(collapse = "<br/>", rbind(colnames(iris), subset(iris, Species == input$codeInput))))

  })


  output$text<-renderUI(HTML(dataset()))


}




shinyApp(ui = ui, server = server)

【讨论】:

    【解决方案2】:

    我无法让 renderDataTableDT 工作。一条消息说某些功能被屏蔽了。所以,我删除了DT 并添加了来自R Studio 表格教程和data.frames 的一些代码。这是我想出的:

    library(shiny)
    
    ui <- fluidPage(
    
      # App title ----
      titlePanel("Subsetting Iris Dataset"),
    
      sidebarLayout(
    
        # Sidebar panel for inputs
        sidebarPanel(
          uiOutput("codePanel"),
    
          # Input: Numeric entry for number of obs to view
          numericInput(inputId = "obs",
                       label = "Number of observations to view:",
                       value = 10)
        ),
    
        mainPanel(
          tableOutput("view")
        )
      )
    )
    
    server <- function(input, output) {
    
      filt <- selectInput("codeInput", label = "choose code",
                           choices = unique(iris$Species))
    
      output$codePanel <- renderUI({ filt
    
      })
    
      dataset <- reactive({ 
    
        subset(iris, Species == input$codeInput)  
    
      })
    
      output$view <- renderTable(head(dataset(), n = input$obs))
    
    }
    
    shinyApp(ui = ui, server = server)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-07
      • 2013-07-16
      • 2019-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-16
      相关资源
      最近更新 更多