简体   繁体   中英

Shiny reactive subset

I am trying to subset data frame of three column (StockCode,Price,label)

but I had to used reactive and my ask is how to render label

I need somethink like 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")

    )
  )
)

server.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())

}

If we are looking to show the data.frame output use the renderDataTable from DT . For reproducibility, used the inbuilt dataset 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)

-output

在此处输入图片说明


The dataset rows can be pasted together to a string to be used in the 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)

-output

在此处输入图片说明


Or use htmlOutput with renderUI

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)

在此处输入图片说明

I could not get renderDataTable from DT to work. A message said some functions were masked. So, I removed DT and added in some code from the R Studio tutorial on tables and data.frames . This is what I came up with:

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)

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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