如何为将在R Shiny中执行的R脚本选择选项



我正在制作一个R闪亮的应用程序,我已经有了两个单独的R脚本(Domain1.R和Domain2.R(,我正在将它们放入R闪亮中。

这些R脚本从PDF文件中提取表格(经过测试,效果良好(。我添加了用于列出域的选项";Domain1";以及";Domain2;以及提取按钮。问题是,在选择选项并单击提取按钮后,将执行两个R脚本。当选择相关选项时,我希望运行其中一个R脚本。

域选择(选项:域1和域2应该调用相应的R脚本,如果我从选项中选择域1,它应该运行代码"Domain1",然而,它现在执行"Domain1"one_answers"Domain2"R脚本。如何解决这个问题?

我是R闪亮的新手,如果有人能帮助我,我将不胜感激。

共享下面的整个代码:

library(shiny)
options(shiny.maxRequestSize=30*1024^2)
shinyApp(
ui = tagList(
navbarPage(
theme = "spacelab",
"Dataset",
tabPanel("Study report extracting",
sidebarPanel(
fileInput("file1", "Select datasets:",
accept = c(
"text/csv",
"text/comma-separated-values,text/plain",
".csv")
),
tags$hr(),
checkboxInput("header", "Header", TRUE),
textInput("txt", "Study info:", "Study name read"),
sliderInput("slider", "Tables to read:", 1, 100, 30),
tags$h5("Prepare extraction"),
actionButton("dataset", "Extract", class = "btn-primary")
),
mainPanel(
tableOutput("contents"),
tabsetPanel(

tabPanel("PDF File select",
h4("Domains"),
tableOutput("table"),
h3("Extracting..."),
selectInput("pdfExtract1", "Pick a PDF", choices = c('Domain1')),
tableOutput("preview"),
actionButton("pdfExtract", "Extract", class = "btn-primary"),
selectInput("pdfExtract1", "Pick a PDF", choices = c('Domain2')),
tableOutput("preview"),
actionButton("pdfExtract", "Extract", class = "btn-primary")


),
tabPanel("Raw data", "TBD"),
tabPanel("calculation", "TBD")
)
)
), # end of first tabpanel
tabPanel("Calculation",
sidebarPanel(
fileInput("file2", "Select datasets:",
accept = c(
"text/csv",
"text/comma-separated-values,text/plain",
".csv")
),
tags$hr(),
checkboxInput("header", "Header", TRUE),
textInput("txt2", "Study info:", "Study name read"),
sliderInput("slider", "Tables to read:", 1, 100, 30),
tags$h5("Preparing the calculation"),
actionButton("dataset2", "Extract", class = "btn-primary")
),
mainPanel(
tableOutput("contents2"),

tabsetPanel(
tabPanel("Datasets",
h4("Domains"),
tableOutput("table2"),
h3("Calculating...")
)
)
)
),
tabPanel("Comparision",
sidebarPanel(
fileInput("file3", "Select study report and datasets:"),
textInput("txt3", "Study info:", "Study name read"),
tags$h5("Prepare comparison"),
actionButton("action2", "Compare", class = "btn-primary")
),
mainPanel(
tabsetPanel(
tabPanel("PDF File select",
h4("Domains"),
tableOutput("table3"),
h3("Comparing..."),
),
tabPanel("calculation data", "TBD")
)
)
)
)
),
server = function(input, output, session) {
output$contents <- renderTable({
# input$file1 will be NULL initially. After the user selects
# and uploads a file, it will be a data frame with 'name',
# 'size', 'type', and 'datapath' columns. The 'datapath'
# column will contain the local filenames where the data can
# be found.
inFile <- input$file1

if (is.null(inFile))
return(NULL)

read.csv(inFile$datapath, header = input$header)
})
output$contents2 <- renderTable({
# input$file1 will be NULL initially. After the user selects
# and uploads a file, it will be a data frame with 'name',
# 'size', 'type', and 'datapath' columns. The 'datapath'
# column will contain the local filenames where the data can
# be found.
inFile <- input$file2

if (is.null(inFile))
return(NULL)

read.csv(inFile$datapath, header = input$header)
})
output$txtout <- renderText({
paste(input$txt, input$slider, format(input$date), sep = ", ")
})
output$table <- renderTable({
df <- c("Domain1","Domain2","Domain3","Domain4")
})
output$table2 <- renderTable({
df <- c("Domain1","Domain2","Domain3","Domain4")
})
output$table3 <- renderTable({
df <- c("Domain1","Domain2","Domain3","Domain4")
})
observeEvent(input$dataset, {
source("Domain1.R", local = TRUE)
})
observeEvent(input$dataset2, {
source("calculation.R", local = TRUE)
})
observeEvent(input$pdfExtract1, {     #When I press the extract button, nothing happens.
source("Domain1.R", local = TRUE)
})
observeEvent(input$pdfExtract1, {    #When I press the extract button, nothing happens.
source("Domain2.R", local = TRUE)
})


}
)

这里有一些元素共享相同的id:

selectInput("pdfExtract1", "Pick a PDF", choices = c('Domain1')),
tableOutput("preview"),
actionButton("pdfExtract", "Extract", class = "btn-primary"),
selectInput("pdfExtract1", "Pick a PDF", choices = c('Domain2')),
tableOutput("preview"),
actionButton("pdfExtract", "Extract", class = "btn-primary")

这是错误的。将此代码块替换为:

selectInput("pdfExtract1", "Pick a PDF", choices = c("Domain1", "Domain2")),
tableOutput("preview"),
actionButton("pdfExtract", "Extract", class = "btn-primary")

然后观察点击按钮,并运行与选择相对应的脚本:

observeEvent(input$pdfExtract, {    
if(input$pdfExtract1 == "Domain1"){ 
source("Domain1.R", local = TRUE)
}else{
source("Domain2.R", local = TRUE)
}
})

但你的应用程序中有一些不清楚的地方:当用户点击dataset按钮时,它也会运行Domain1.R

observeEvent(input$dataset, {
source("Domain1.R", local = TRUE)
})

我不知道你想做什么,所以我不能进一步评论这一点。

最新更新