vishakha deshmukh vishakha deshmukh - 2 months ago 29
R Question

How to get a table or dataframe as a output in R shiny app using selectInput function?

I am super new to shiny app. I want to get a table output using selectInput function.
I have to import(not upload) different csv files in place of object error1,2,3,4 but I when I am doing this I am not getting proper output in mainpanel. Please help me in importing csv files in right place so that I could get table as an output in mainpanel. Thanks in advance!!

library(shiny)

ui <- shinyUI( fluidPage(
titlePanel(h3("PUMA", style = "color:black")),
sidebarLayout(
sidebarPanel(
tags$head(
tags$style("body{backgroud-color: pink;}")),
selectInput("mydata", "ERROR MESSAGE:",
choices = c("Clamping lever closing not detected-Error number 7 / 31"="error1",
"Filter paper wrong or not inserted-Error number 30 / 20"="error2",

"Device is heating up (working temp. not reached yet) Heating Option-Error number 32 / 51"="error3",

"Door is open (Timeout key)-Error number 15 / 100"="error4")
)
),
mainPanel(tabsetPanel(
tabPanel(h2("TABLE", style = "color:red"), verbatimTextOutput ("mydata")),
#tabPanel(h2("ERROR", style = "color:red"), verbatimTextOutput("ERROR")),
tags$head(tags$style("#mydata{color:blue;
font-size: 17px;
font-style: bold
}"))
),
width = 12)
)))

server <- shinyServer(function(input, output){

output$mydata <-renderPrint ({
input$mydata
})
})

shinyApp(ui = ui, server = server)

Answer

Your render function actually returns the selected item as a string from your input and not the object itself. You can wrap all the objects in a list or use get:

Option 1

# put all objects in a list
mDat <- list(error1 = cars, error2 = airquality, error3 = chickwts, error4 = co2)


server <- shinyServer(function(input, output){
   output$mydata <- renderPrint ({
      mDat[[input$mydata]]
   })    
})

Option 2

# This assumes that you have the objects error1, ..., error4 in the environment

server <- shinyServer(function(input, output){
   output$mydata <- renderPrint ({
      get(input$mydata)
   })    
})
Comments