问题描述
亲爱的朋友,
我目前正在尝试创建一个应用,用户可以在其中上传各种来源的数据集。我试图将代码隔离在一个单独的Shiny模块中。但是,R抛出错误,表明环境对象不可子集化。我对Shiny还是比较陌生,因此也希望对模块的使用提出任何建议。 :)
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,session) {
dataset <- fileInputServer("test")
output$head_data <- renderTable(head(dataset()))
}
shinyApp(ui,server)
我使用modalDialog是因为我认为它是询问用户有关读取功能的其他输入的最简单方法,例如对于read.csv是分隔符,对于read.xlsx等是startRow。
感谢您的答复
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)