我正在使用Shiny App,遇到一条错误消息,提示数据需要为二维。
该应用程序的目的是基于称为区域的列过滤数据集,并查看和更新数据集。应该很简单...不过,用户需要先上传数据集才能使用该应用程序。我假设此应用程序中使用的所有文件都会有一个列,即“区域”。我相信问题出在“#选择要打印的区域----”。
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")))
)
)
)
)
服务器:
# 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()
}
})
}