1

Hi I'm relatively new to Shiny and am not sure how to do this. I am making a dashboard that should first pull the relevant dataframe based on user selectInput, after which further selectInput functions will further filter down the sheet for the relevant price. However, I can't seem to link the InputId from the selectInput to the relevant dataframe name. (Below is code)

UI.R

ui <- navbarPage(
  "Dashboard",
  tabPanel(
    "Cost1",
    fluidPage(
      selectInput("type",
                  label = "Select Type",
                  choices = NULL),
      textOutput("message")
    )
  )
)

Server.R

#load libraries, data
library(tidyr)
library(readxl)
library(dplyr)
library(purrr)

a <- read_excel('source.xlsx', sheet = 'a')
b <- read_excel('source.xlsx', sheet = 'b')
c <- read_excel('source.xlsx', sheet = 'c')

mylist <- list(a = a, b = b, c = c)

server <- function(input, output, session) {
  
  updateSelectInput(session,
                    "type",
                    choices = names(mylist))
  

  material = reactive(input$type)
  price <- material[1,"price"]

  
  output$message <- renderText({
    paste(price)
      
  })
   
}

Thank you!

NewCoder01
  • 11
  • 1
  • You can use `get`. Please see my related answer [here](https://stackoverflow.com/questions/70667870/r-shiny-automatically-adding-filters-with-the-names-of-the-columns-and-select/70679505#70679505). – ismirsehregal Jan 19 '22 at 05:49

1 Answers1

0

There is a few things that need to correct in your original code - here is my code for 3 files global.R, server.R, and ui.R with detail explanation comments. (my habit of separating them so it easier to manage.

global.R

#load libraries, data
library(shiny)
library(tidyr)
library(readxl)
library(dplyr)
library(purrr)

# This is just a generation of sample data to be used in this answer.
set.seed(1)
generate_random_df <- function(name) {
  tibble(
    product = paste0(name, "-", round(runif(n = 10, min = 1, max = 100))),
    price = runif(10))
}
a <- generate_random_df("a")
b <- generate_random_df("b")
c <- generate_random_df("c")
mylist <- list(a = a, b = b, c = c)

server.R

set.seed(1)
generate_random_df <- function(name) {
  tibble(
    product = paste0(name, "-", round(runif(n = 10, min = 1, max = 100))),
    price = runif(10))
}
a <- generate_random_df("a")
b <- generate_random_df("b")
c <- generate_random_df("c")
mylist <- list(a = a, b = b, c = c)

server <- function(input, output, session) {
  
  updateSelectInput(session,
                    "type",
                    choices = names(mylist))
  
  # to extract the data you need to reference to mylist as the Input only take
  # the name of your list not the dataset within it
  price <- reactive({
    # Here the material command also inside the reactive not as you do initially
    material <- mylist[[input$type]]
    
    paste0(material[1,"price"])
  })
  
  # You don't need renderText for this just assign the value to message
  output$message <- price
  
  # I also output the table for easier to see
  output$price_table <- renderTable(mylist[[input$type]])
}

ui.R

ui <- navbarPage(
  "Dashboard",
  tabPanel(
    "Cost1",
    fluidPage(
      selectInput("type",
                  label = "Select Type",
                  choices = NULL),
      textOutput("message"),
      tableOutput("price_table")
    )
  )
)

Here is the screenshot of the app

enter image description here

Sinh Nguyen
  • 4,277
  • 3
  • 18
  • 26