TL; DR,这是我有史以来的第一个Shiny App,我坚持这个被动的问题。
我正在编写一个Shiny应用程序,它将获取Excel调查数据并将其放入ggplot函数中。该功能应该有效,但调查问题每年都有所不同。我希望应用程序执行以下操作:
问题在于第二项任务。我希望应用程序在文件上传时做出反应,将整个colnames()列表放入下拉菜单中,供用户选择哪些列具有组织名称。我已经尝试过Stack Overflow上其他问题的解决方案,但它们最终都会抛出错误。
这是我的用户界面和服务器代码:
library(rsconnect)
library(readxl)
library(shiny)
library(ggplot2)
dataset <- file
ui <- fluidPage(
shinythemes::themeSelector(),
titlePanel("Volunteer stats"),
sidebarLayout(
sidebarPanel(
#I need: names of each of the files,
# columns for org, num_vol, and survey year,
# labels for x axis, y axis, and title,
# name for the PDF to be created
fileInput(inputId = "file", label = "Choose Excel file", multiple = TRUE),
uiOutput("org_select"),
uiOutput("num_select"),
uiOutput("year_select"),
textInput(inputId = "org_label", label = "X axis label"),
textInput(inputId = "vols_label", label = "Y axis label"),
textInput(inputId = "plot_title", label = "Chart title"),
textInput(inputId = "pdf_title", label = "PDF title")),
mainPanel(
plotOutput(
outputId = "histogram"
)
)
))
server <- function(input, output) {
output$org_select <- renderUI({
selectInput("org_col", "Which column has the organization name?", choices = list(colnames(read_excel(input$file))), label = "Organization")
})
output$num_select <- renderUI({
selectInput("num_vols", "Which column has the relevant metric?", choices = list(colnames(read_excel(input$file))), label = "Number of Volunteers")
})
output$year_select <- renderUI({
selectInput("year", "Which column has the year?", choices = list(colnames(read_excel(input$file))), label = "Year")
})
#assemble all the files into a list based on the input, to be passed to ggplot (not shown)
getData <- reactive({
if (is.null(input$file)){
return(NULL)
}else{
numfiles = nrow(input$file)
files_list = list(
for(i in 1:numfiles)
{
XL_file = read_excel(input$file[[i, 'datapath']], header = TRUE)
lastrow = nrow(XL_file)
shift = function(x, n){
c(x[-(seq(n))], rep(NA, n))
}
XL_file$identity = shift(XL_file$identity, 1)
files_list[[i]] = XL_file[-lastrow, ]
}
)
}
})
getData()
shinyApp(ui = ui, server = server)
为简洁起见,我没有包含我的ggplot功能。如果我需要帮助,我稍后会提交一个单独的问题。
非常感谢, 霍元甲
答案 0 :(得分:0)
只需两个小修复就可以了:
input$file
是一个对象(或列表,如果需要),但read_excel()
期望包含文件路径的字符串。使用input$file$datapath
。
choices
参数指向命名或未命名值的命名列表。使用list(colnames(...))
,您将传递一个包含chioices的单个元素的列表:
list(colnames(read_excel('path/to/file')))
[[1]]
[1] "COL_1" "COL_2" "COL_3" "COL_4"
传递colnames()
。
server <- function(input, output) {
output$org_select <- renderUI({
selectInput("org_col", "Which column has the organization name?", choices = colnames(read_excel(input$file$datapath)), label = "Organization")
})
output$num_select <- renderUI({
selectInput("num_vols", "Which column has the relevant metric?", choices = colnames(read_excel(input$file$datapath)), label = "Number of Volunteers")
})
output$year_select <- renderUI({
selectInput("year", "Which column has the year?", choices = colnames(read_excel(input$file$datapath)), label = "Year")
})
}