我正在构建一个闪亮的表单,它将从textInput字段中获取数据,并将这些输入与文本文件(将通过文件输入上传)组合在一起,并在主面板中显示输出 . 有一个动作按钮,用于第一次更新数据(从文本输入中获取数据并与处理后的文本文件合并),我添加了另一个操作按钮,用于添加新数据(此目的是添加新数据以添加新数据)数据集与现有数据一样,新的数据集将由文件输入上传) . 下面给出了样本数据集,它是纯文本格式的文件 . 此样本数据集可被视为第二个文本文件 . 样本数据:
# AREA ADC-MEAN ADC-STD DEV ADC-MIN ADC-MAX ADC-MED
1 12.0000 0.000644667 1.96669e-005 0.000606000 0.000671000 0.000644000
2 12.0000 0.000610250 1.43154e-005 0.000577000 0.000624000 0.000617000
我根据场景编写了shinnyApp . 我能够通过合并和输出作为表来更新文本输入和文本文件输出 . 但是无法将一组新数据添加为行 . 脚本如下:
library(shiny)
library(ggplot2)
library(xlsx)
library(xlsxjars)
library(rJava)
library(shinythemes)
# Define UI -----------
# ---------------------
ui <- fluidPage(theme = shinytheme("sandstone"),
# header
headerPanel("DTI post analysis conversion"),
sidebarLayout(
# sidebar for form
sidebarPanel(
h3("Information",""),
textInput("ani_id", "Patient ID",""),
textInput("scan_id", "Scan ID",""),
textInput("Tech_id", "Tech Id",""),
textInput("Age_weeks", "Age weeks",""),
fileInput("textfile", "Upload the text file"),
actionButton("update", "Update"),
helpText("Click to insert the data "),
br(),
actionButton("addEntry", "Add New Data"),
helpText("Click to insert new data "),
br(),
downloadButton("downloadData", "Download"),
helpText("Click for download the data (.csv) ")
),
# output for viewing
mainPanel(
DT::dataTableOutput("tableDT")
)
)
)
# Define server logic ------
# --------------------------
server <- function(input, output) {
# process the textinput
Frontal_Cortex_table <- eventReactive(input$update,{
# creating table
aniRoi2 <- data.frame(Animal_ID = rep(input$ani_id,2),
Scan_ID = rep(input$scan_id,2),
Tech_ID = rep(input$Tech_id,2),
Age_weeks = rep(input$Age_weeks,2),
stringsAsFactors = FALSE)
return(aniRoi2)
})
# process the text file and download
textdata <- eventReactive(input$update,{
file1 <- input$textfile
if(is.null(file1)){return()}
a <- read.table(file= file1$datapath,
sep="\t",
fill=FALSE,
strip.white=TRUE)[1:2,]
# Split the text file and shape as column
af <- as.character(a)
af1 <- matrix(unlist(strsplit(af, split=" +")), ncol=7, byrow =TRUE)
ad <- data.frame(af1[1:2,3:7])
colnames(ad)<- c("ADC_MEAN", "ADC_STD", "ADC_MIN", "ADC_MAX", "ADC_MED")
return(ad)
})
# merge two function as data.frame
mytable2 <-reactive({
dm = cbind.data.frame(Frontal_Cortex_table(), textdata())
})
# add new row (?)
addData <- observeEvent(input$addEntry, {
mytable2 <- isolate({
newLine <- reactive({cbind.data.frame(Frontal_Cortex_table(), textdata())})
rbind.data.frame(mytable2,newLine)
})
})
# output the data as table
output$tableDT <- DT::renderDataTable(
mytable2()
)
# download the file
output$downloadData <- downloadHandler(
filename = function() {
paste("DTI", "csv", sep = ".")
},
content = function(file) {
write.csv(mytable2(), file, row.names = FALSE)
}
)
}
# Run the app ----------
# ----------------------
shinyApp(ui = ui, server = server)
我收到错误消息,指出:
Warning: Error in [[: object of type 'closure' is not subsettable Stack trace (innermost first):
73: rbind.data.frame
66: isolate
65: observeEventHandler [/Users/rahatjahan/Dropbox/Database dev/DTIApp/Ask questions.R#95]
1: runApp
我知道,这是一个很长的帖子,但试着解释并提供一切,这样就不会有任何混乱 .
您的意见和建议将不胜感激 .
1 回答
问题解决了,每次迭代都会添加新行 . 新的文本数据集:
和修改后的代码
希望有所帮助 .