I am working on a Shiny App and came across an error message saying that the data needs to be 2-dimensional.
The aim of this app is to filter a data set based on a column called region and view and updated data set. Should be simple... Though, the user needs to upload the data set first in order to use the app. I am assuming that all files used within this application will have a column namely "region". I believe the issue is at "# Select regions to print ----".
UI:
library(shiny)
library(DT)
library(tidyverse)
# Define UI for data upload app ----
ui <- fluidPage(
# App title ----
titlePanel(title = h1("Upload file and select columns", align = "center")),
# Sidebar layout with input and output definitions ----
sidebarLayout(
# Sidebar panel for inputs ----
sidebarPanel(
# Input: Select a file ----
fileInput("uploaded_file", "Choose CSV File",
multiple = TRUE,
accept = c("text/csv",
"text/comma-separated-values,text/plain",
".csv")),
# Horizontal line ----
tags$hr(),
# Input: Checkbox if file has header ----
checkboxInput("header", "Header", TRUE),
# Input: Select separator ----
radioButtons("sep", "Separator",
choices = c(Semicolon = ";",
Comma = ",",
Tab = "\t"),
selected = ","),
# Horizontal line ----
tags$hr(),
# Input: Select number of rows to display ----
radioButtons("disp", "Display",
choices = c(All = "all",
Head = "head"),
selected = "all"),
# Select variables to display ----
# uiOutput("variables"),
# Select variables to display ----
uiOutput("regions")
),
# Main panel for displaying outputs ----
mainPanel(
tabsetPanel(
id = "dataset",
tabPanel("Data Explorer", div(style = "overflow-x: scroll",dataTableOutput("rendered_file")))
)
)
)
)
Server:
# Define server logic to read selected file ----
server <- function(input, output, session) {
# Read file ----
df <- reactive({
req(input$uploaded_file)
read.csv(input$uploaded_file$datapath,
header = input$header,
sep = input$sep)
})
output$regions <- renderUI({
selectizeInput(inputId = "select_region",
label = "Select region of interest",
choices = unique(df()$region),
multiple = T,
options = list(placeholder = 'Select the region of interest'))
})
# Select regions to print ----
df_sel <- reactive({
req(input$select_region)
df_sel <- df()$region == input$select_region
})
# Print data table ----
output$rendered_file <- DT::renderDataTable({
if(input$disp == "head") {
head(df_sel())
}
else {
df_sel()
}
})
}