user90
user90

Reputation: 381

textAreaInput is not working with fileInput

library(shiny)
ui <- fluidPage(
titlePanel("Uploading Files"),
sidebarLayout(
sidebarPanel(
fileInput("file1", "Choose CSV File",multiple = TRUE,accept = c("text/csv","text/comma-        separated-values,text/plain", ".csv")), 
textAreaInput("paste", "Or manufacturer", "Paste one manufacturer per line"),),
mainPanel(tableOutput("contents"))))
 server <- function(input, output) {
output$contents <- renderTable({
 if(!is.null(input$file1))
 {
 req(input$file1)
data <- as.matrix(mpg)
df <- read.delim(input$file1$datapath,header = F)
manufacturer <- unique(df[, 1])
data[data[, 1] %in% manufacturer, ]}
else
{
 req(input$file1)
 paste_data <-({ input$paste })
 write.table(paste_data,"111")
 data <- as.matrix(mpg)
 manufacturer <- unique(paste_data[, 1])
 data[data[, 1] %in% manufacturer, ]
}})}
shinyApp(ui, server)

I used the above code to allow the user to input as two types, One is from the file and second is to paste some inputs. I used mpg data (ggplot2::mpg) as reference. But the code is only working when user input via file, if user is paste some input as like this

audi
dodge
ford

not showing anything. Why is textAreaInput not working ?

Upvotes: 0

Views: 25

Answers (1)

Hasan Bhagat
Hasan Bhagat

Reputation: 405

Try the code below. Use observeEvent instead of if else. Also, i have added a conditional panel. For the text input, if you ask the input to be comma or space separated, you can simplify your text data conversion to a table using 'strsplit'

library(shiny)
ui <- fluidPage(
  titlePanel("Uploading Files"),
  sidebarLayout(
    sidebarPanel(
      selectInput("choose","Choose file source",choices = c("file","text"),selected = NULL),
      conditionalPanel("input.choose=='file'",fileInput("file1", "Choose CSV File",multiple = TRUE,accept = c("text/csv","text/comma-        separated-values,text/plain", ".csv"))),
      conditionalPanel("input.choose=='text'",
                       p("Enter a comma or space separated list"),
                       textAreaInput("paste", "Or manufacturer",placeholder =  "Paste one manufacturer per line"),)),
      
    mainPanel(tableOutput("contents"))))
server <- function(input, output) {

  observeEvent(input$file1,{
    req(input$file1)
    output$contents=renderTable({data <- as.matrix(mpg)
                                df <- read.delim(input$file1$datapath,header = F)
                                manufacturer <- unique(df[, 1])
                                data[data[, 1] %in% manufacturer, ]})
    })
  
observeEvent(input$paste,{
  req(input$paste)
  mfg=strsplit(input$paste,",| ")
  output$contents=renderTable(data.frame('manfucature'=unlist(mfg)))
  
})
  
  }
shinyApp(ui, server)

Upvotes: 1

Related Questions