gpt4 book ai didi

r - 使用操作按钮在 Shiny 的 R 中添加一个包含现有数据框的新行

转载 作者:行者123 更新时间:2023-12-04 02:01:19 24 4
gpt4 key购买 nike

我正在构建一个 Shiny 的表单,它将从 textInput 字段中获取数据,并将这些输入与文本文件(将通过文件输入上传)合并,并在主面板中显示输出。第一次更新数据有一个 Action 按钮(从文本输入中获取数据并与处理过的文本文件合并),我添加了另一个 Action 按钮来添加新数据(此添加新数据的目的是添加新数据)一组数据作为与现有数据的行,新的一组数据将通过文件输入上传)。下面给出了示例数据集,它是一个纯文本格式的文件。此示例数据集可以视为第二个文本文件。
样本数据:

#         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

我知道,这是一篇很长的文章,但尝试解释并提供所有内容,以免造成任何混淆。

您的意见和建议将不胜感激。

最佳答案

问题解决了,每次迭代它都会添加新行。
新的文本数据集:

#         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
3 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
4 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
5 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
6 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
7 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
8 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
9 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
10 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
# AREA FA-MEAN FA-STD DEV FA-MIN FA-MAX FA-MED
1 12.0000 0.233833 0.0171773 0.201000 0.262000 0.239000
2 12.0000 0.247417 0.0135275 0.220000 0.270000 0.248000
3 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
4 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
5 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
6 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
7 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
8 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
9 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
10 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
ADC-MEAN
0.000644667
0.000610250
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
ADC-STD DEV
1.96669e-005
1.43154e-005
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
FA-MEAN
0.233833
0.247417
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
FA-STD DEV
0.0171773
0.0135275
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000

和修改后的代码
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", "Insert 1st Data Set"),
helpText("Click to insert the data "),
br(),
fileInput("anothertextfile", "Upload another Text file"),
actionButton("addEntry", "Add New Data"),
helpText("Click to insert new data "),
br(),
actionButton("combine", "Combine the data sets"),
downloadButton("downloadData", "Download"),
helpText("Click for download the data (.csv) ")
),

# output for viewing
mainPanel(

DT::dataTableOutput("tableDT"),
DT::dataTableOutput("tableDT2")


)
)
)


# Define server logic ------
# --------------------------

server <- function(input, output) {

# process the textinput
Frontal_Cortex_table <- reactive({


# 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 <- reactive(
{
file1 <- input$textfile
if(is.null(file1)){return()}
#read.table(file=file1$datapath, sep=input$sep, header = input$header, stringsAsFactors = input$stringAsFactors)
a <- read.table(file= file1$datapath,
sep="\t",
fill=FALSE,
strip.white=TRUE)[1:20,]

# Split the text file and shape as column
af <- as.character(a)
#class(af)
#af
#nrow(a)
af1 <- matrix(unlist(strsplit(af, split=" +")), ncol=7, byrow =TRUE)
# typeof(af1)
# af1
ad <- data.frame(af1[1:2,3:7], af1[11:12, 3:7])

colnames(ad)<- c("ADC_MEAN", "ADC_STD", "ADC_MIN", "ADC_MAX", "ADC_MED",
"FA_MEAN", "FA_STD", "FA_MIN", "FA_MAX", "FA_MED")


return(ad)
})


# merge two function as data.frame
mytable2 <-eventReactive(input$update,{

dm <<- cbind.data.frame(Frontal_Cortex_table(), textdata())

})

# add new row (?)

addData1 <- eventReactive(input$addEntry, {
newLine <<- cbind.data.frame(Frontal_Cortex_table(), textdata())
})


addData <- eventReactive(input$addEntry, {
dm <<- rbind.data.frame(mytable2(),addData1())
})

addData2 <- eventReactive(input$addEntry, {
dm <<- rbind.data.frame(dm,addData1())
})

# output as data table
output$tableDT <- DT::renderDataTable(
mytable2()
)

# the combined data set with added row
output$tableDT2 <- DT::renderDataTable(
addData2()
)
# 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)

希望这会有所帮助。

关于r - 使用操作按钮在 Shiny 的 R 中添加一个包含现有数据框的新行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47083893/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com