我编写了以下程序:
ui.r
library(shiny)
shinyUI(fluidPage(
mainPanel(
textOutput("text1"),
checkboxGroupInput("checkGroup",
label = h3("Alternatives"),
choices = list("A" = 1,
"B" = 2,
"C" = 3,
"D" = 4),
selected = NULL),
actionButton("action", label = "Next")
)
))
服务器.r
library(shiny)
shinyServer(function(input, output) {
observe({
if(input$action>0 & input$action<=2){
valores<-renderText({
input$checkGroup
})
data<-unlist(strsplit(valores(), split=" "))
print(data)
}
})
})
我已经设法捕获所选的值并将它们放入向量中,因此我解决了之前遇到的问题。
我现在遇到的问题是,它始终输出我从复选框组中选择或取消选择的值,但我只想捕获按下“下一步”按钮后选择的最后一个值。
有什么帮助吗?我查过文档,但是很少。
最佳答案
这是剥猫皮的一种方法。
我们将 isolate
和 reactive
与 actionButton
一起使用,以确保在按下按钮之前不会评估任何内容。通过将输入本身与任何进一步的格式隔离,我们可以出于任何目的多次调用它。请注意,如果没有隔离,它将在输入更改时进行评估。
服务器.R
library(shiny)
shinyServer(function(input, output) {
# store as a vector to be called wherever desired
# evaluated whenever inputs change
datasetInput <- reactive({
perm.vector <- as.vector(input$checkGroup)
perm.vector
})
# call as a text output
output$textOut <- renderText({
input$action # makes sure nothing moves till the button is hit
# isolate prevents datasetInput from reactively evaluating
isolate(datasetInput())
})
# call in a different place as a data table output
output$dataTableOut <- renderDataTable({
input$action # makes sure nothing moves till the button is hit
isolate(df <- data.frame(inputs = datasetInput()))
df
}, options = list(
lengthMenu = list(c(5, 15, -1), c('5', '15', 'All')),
pageLength = 15)
)
})
ui.R
library(shiny)
shinyUI(fluidPage(
sidebarLayout(
sidebarPanel(
checkboxGroupInput("checkGroup",
label = h3("Alternatives"),
choices = list("A" = 1,
"B" = 2,
"C" = 3,
"D" = 4),
selected = NULL),
actionButton("action", label = "Next")
),
mainPanel(
h4("Print as text"), textOutput("textOut"),
h4("Print as data table"), dataTableOutput("dataTableOut")
)
)
))
关于r - 在 Shiny 中仅从复选框组获取一次值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29282524/