【发布时间】:2017-09-28 01:19:52
【问题描述】:
我有一个闪亮的 server.R,它正在尝试执行以下操作:
我在侧边栏上有一个 selectInput,它允许用户选择 2 个条件之一,这随后会更改由条件面板组成的 UI,每个条件一个。
我的 server.R 设置如下:
if (input$option1) {return()}
else{run code corresponding to option 2 which outputs reactive variable 2}
if (input$option2) {return()}
else{run code corresponding to option 1 which outputs reactive variable 1}
.
.
.
run another function that depends on variable 1 or variable 2, depending on which input is chosen above
上述代码不起作用的原因是因为为了引用 input$option*,我需要处于响应式环境中。但很明显,我不能把它放在一个isolate() 调用中,因为我需要UI 来响应用户输入。很明显,我不能把它放在 observe() 调用中,因为我需要能够根据条件逻辑输出变量 1 或变量 2。那么我该如何完成我想要完成的事情呢?如有必要,我可以提供我的代码,但我认为这更多的是设计/程序流程问题。
谢谢。代码如下:
runApp(list(
ui = fluidPage(
headerPanel("App"),
sidebarPanel(
selectInput(
"simtype",
"Select Simulation Type",
c(SingleSubject = "SS", FromFile = "FF"),
selected = "FF",
multiple = FALSE
),
conditionalPanel(
condition = "input.simtype=='FF' ",
fileInput(
'file1',
'Upload File Here',
accept = c('text/csv', 'text/comma-separated-values,text/plain', '.csv')
),
# tags$hr(),
checkboxInput('header', 'Header', TRUE),
radioButtons(
'sep',
'Separator',
c(
Comma = ',',
Semicolon = ';',
Tab = '\t'
),
'Comma'
),
uiOutput('varselect1')
),
conditionalPanel(
condition = "input.simtype== 'SS' ",
aceEditor(
"text",
"",
mode = "r",
theme = "twilight",
height = "40px",
fontSize = 10
),
uiOutput('varselect2')
)
),
mainPanel(#uiOutput('tabs')
tabsetPanel(
tabPanel(title = 'FILE', value = "filetab", tableOutput("filetable")),
tabPanel(
title = "SUMMARY",
value = "summarytab",
dataTableOutput("summarytable")
),
id = "tabs"
))
),
server = function(input, output) {
if (input$simtype == "SS") {
return()
} else{
file <- reactive({
file <- input$file1
})
output$filetable <- renderTable({
file()
})
} #end if
output$varselect1 <- renderUI({
if (is.null(input$file1$datapath)) {
return()
}
list(actionButton("goButton", 'GO'),
numericInput("s1", "Input value 1", NULL))
}) # end renderUI()
if (input$simtype == "FF") {
return()
} else{
get.text <- reactive({
input$text
})
output$filetable <- renderTable({
X <- read.table(text = get.text())
X
}) # end renderTable()
} #end if
output$varselect2 <- renderUI({
if (input$text == "") {
return()
}
list(numericInput("s0", "Input value", NULL),
actionButton("goButton", 'GO'))
})
#end renderUI()
mydata <- reactive({
input$goButton
if (input$simtype == "FF") {
variable = file()
} else {
variable = X()
}
}) #end mydata()
output$summarytable <- renderDataTable({
variable
})
}
))
【问题讨论】:
-
目前还不清楚您在哪里拥有此代码或您希望它做什么。如果您可以创建一个最小的可重现示例,该示例可以运行以准确查看您在做什么并测试可能的解决方案/替代方案,那就太好了。 (理想情况下,调用
runApp并提供ui=和server=函数列表,如?runApp帮助页面底部。) -
如果我尝试将其复制/粘贴到 R 中,似乎有很多语法错误,因此它无法运行。您的
if/else语法也有问题。如果您的 if 语句执行一个块 ({..}),则else但与结束}在同一行开始。它不能从新行开始。否则,else 所属的位置可能会模棱两可。 -
立即尝试。它应该运行,除非它遇到原始问题中提到的问题,即我不能在没有反应上下文的情况下调用 input$*。
-
不。仍然得到错误。第一个错误是
unexpected 'else'。您似乎没有尝试解决我提到的if/else问题。 -
它现在运行。您提到的问题是风格问题。