0
votes

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

}
2
Can you provide a data set? And can you define how to want to subset your data frame? - MLavoie

2 Answers

1
votes

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

enter image description here


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

enter image description here


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)

enter image description here

0
votes

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)

enter image description here