親愛なるサックオーバーフローの皆様へ
現在、ユーザーがさまざまなソースからデータセットをアップロードできるアプリを作成しようとしています。アップロード用のコードを別の Shiny モジュールに分離しようとしました。ただし、R は、環境オブジェクトがサブセット化できないというエラーをスローします。私は光沢のあるものにかなり慣れていないので、モジュールの使用に関するアドバイスもいただければ幸いです。:)
library(shiny)
fileInputUI <- function(id){
ns <- NS(id)
fileInput(
inputId = ns("file"),
label = "Select a file"
)
}
fileInputServer <- function(id){
moduleServer(id, function(input, output, session){
dataset <- reactive({
req(input$file)
# Get file extension and datapath
ext <- tools::file_ext(input$file$name)
datapath <- input$file$datapath
# Need reactive values to store input data in
read_data_options <- reactiveValues()
read_data_options$sep <- NULL
if(ext == "csv"){
choices_sep <- c(",", ";", "", "\\t")
names(choices_sep) <- c("Comma (,)", "Semicolon (;)", "White space ( )", "Tab separated (\\t)")
showModal(
ui = modalDialog(
selectInput(
inputId = NS(id, "sep"),
label = "Which separator is used for your data",
choices = names(choices_sep),
selected = ""
),
# Input is required so no dismiss button
footer = tagList(
modalButton("Dismiss"),
actionButton(
inputId = NS(id, "submit_csv"),
label = "Submit")
),
easyClose = TRUE
)
)
# Set input value from submit button
observeEvent(
eventExpr = input$submit_csv,
handlerExpr = {
read_data_options$sep <- choices_sep[input$sep]
removeModal()
read.csv(
file = datapath,
sep = read_data_options$sep
)
}
)
} else {
NULL
}
})
dataset
})
}
# Shiny App
ui <- fluidPage(
fileInputUI("test"),
tableOutput("head_data")
)
server <- function(input, output, session) {
dataset <- fileInputServer("test")
output$head_data <- renderTable(head(dataset()))
}
shinyApp(ui, server)
modalDialog を使用したのは、read.csv の場合のセパレーターや read.xlsx の startRow など、読み取り関数への追加の入力についてユーザーに尋ねる最も簡単な方法であると考えたからです。
返信ありがとうございます