我下面有一个简单的闪亮应用程序。在此应用程序中,我希望用户能够上载自己的csv,然后自动将其添加为另一个数据集“ D.B”(由我在原始应用程序中创建)下方的复选框组中。然后,当用户选择文件时,该文件将显示为表格。
在这里,我设法重命名了上载的文件。但是,在保持“更改名称”功能的同时,我无法随后将重命名的表与renderTable()
连接。
# Define UI for data upload app ----
ui <- fluidPage(
# App title ----
titlePanel("Uploading Files"),
# Sidebar layout with input and output definitions ----
sidebarLayout(
# Sidebar panel for inputs ----
sidebarPanel(
# Input: Select a file ----
fileInput("file1", "Choose CSV File",
multiple = FALSE,
accept = c("text/csv",
"text/comma-separated-values,text/plain",
".csv")),
checkboxGroupInput("datasetSelector","Data Files", choices=c("D.B")),
textInput("filename","Set Filename",value = "Set Name")
),
# Main panel for displaying outputs ----
mainPanel(
# Output: Data file ----
tableOutput("contents")
)
)
)
#server.r
server <- function(input, output, session) {
observeEvent(input$filename, {
req(input$file1)
Name<-input$filename
updateCheckboxGroupInput(session,"datasetSelector", choices=c("D.B",Name))
})
observeEvent(input$file1, {
Data<-input$file1$datapath
Name<-input$filename
New <- read.csv(Data)
updateCheckboxGroupInput(session,"datasetSelector",
choices=c("D.B",input$file1$name))
})
D.B <- reactive({
if("D.B"%in% input$datasetSelector){
x <- read.csv("something.csv", stringsAsFactors = F)
}
})
output$contents <- renderTable({
New
})
}
答案 0 :(得分:2)
这是一种解决方法。
此解决方案使用闪亮的输入功能来命名列表。在命名列表中,标签是显示名称,而值是返回值。例如。 c("a" = 1, "b" = 2)
将在用户界面中显示标签a
和b
,但将在服务器中返回1
和2
的值。
由于您希望标签中的一个来自输入,因此有必要分两部分构建有序列表。首先是值,然后是标签。例如。 mylist = c(1,2)
设置值,然后names(mylist) = c("a","b")
分配标签。
这是我工作的代码:
library(shiny)
# Define UI for data upload app ----
ui <- fluidPage(
# App title ----
titlePanel("Uploading Files"),
# Sidebar layout with input and output definitions ----
sidebarLayout(
# Sidebar panel for inputs ----
sidebarPanel(
# Input: Select a file ----
fileInput("file1", "Choose CSV File",
multiple = FALSE,
accept = c("text/csv",
"text/comma-separated-values,text/plain",
".csv")),
checkboxGroupInput("datasetSelector","Data Files", choices=c("D.B" = "original")),
textInput("filename","Set Filename",value = "Set Name")
),
# Main panel for displaying outputs ----
mainPanel(
# Output: Data file ----
tableOutput("contents")
)
)
)
#server.r
server <- function(input, output, session) {
observeEvent(input$filename, {
req(input$file1)
Name<-input$filename
# make list
choices = c("original", "loaded")
# assign labels to list
names(choices) = c("D.B", Name)
updateCheckboxGroupInput(session,"datasetSelector", choices=choices)
})
observeEvent(input$file1, {
Data<-input$file1$datapath
Name<-input$filename
New <- read.csv(Data)
# make list
choices = c("original", "loaded")
# assign labels to list
names(choices) = c("D.B", Name)
updateCheckboxGroupInput(session,"datasetSelector", choices=choices)
})
D.B <- reactive({
if("D.B"%in% input$datasetSelector){
x <- read.csv("something.csv", stringsAsFactors = F)
}
})
output_table = reactive({
if("original" %in% input$datasetSelector)
return(D.B())
if("loaded" %in% input$datasetSelector)
return(read.csv(input$file1$datapath))
})
output$contents <- renderTable({
output_table()
})
}
# Run the app ----
shinyApp(ui = ui, server = server)