TL;DR、これは私の初めてのShiny Appであり、この事後の問題にこだわっています。
Excelの調査データを取得してggplot関数に入れるShinyアプリを作成しています。機能は動作するはずですが、調査の質問は年ごとに異なります。アプリに次のことをさせたい:
- 複数のExcelファイルを取り、それらを読む
- 組織名、ボランティアの数/時間、調査が行われた年の3つのドロップダウンメニューを表示し、XおよびYラベルとタイトルの3つのテキスト入力フォームを表示します。
- 各組織について、組織が出現した各年の回避バー付きのボランティアの数を示すヒストグラムを印刷します。
問題は2番目のタスクにあります。ファイルがアップロードされたときに、colnames()のリスト全体をドロップダウンメニューに配置して、組織名を持つ列を選択できるようにアプリが反応するようにします。 Stack Overflowに関する他の質問の解決策を試しましたが、すべてエラーになりました。
UIとサーバーのコードは次のとおりです。
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関数を含めていません。それに関して助けが必要な場合は、後で別の質問を送信します。
どうもありがとう、 大胆不敵
回答 1 件
関連した質問
それを機能させるためのわずか2つのマイナーな修正:
input$file
オブジェクト(または必要に応じてリスト)ですが、read_excel()
ファイルパスを含む文字列が必要です。input$file$datapath
を使用する 。ザ・
choices
引数には、名前付きまたは名前なしの値の名前付きリストが必要です。list(colnames(...))
を使用 あなたはそれにchioicesを含む単一の要素を持つリストを渡している:colnames()
を渡すだけです 。