【发布时间】:2021-10-11 06:10:07
【问题描述】:
几天前,我在post 中得到了答复。在那一刻,解决方案是完美的,但我意识到我忘了问我如何使用超过 1 个checkboxInput 来做到这一点。因为......我已经尝试了很多东西,但该解决方案不适合我使用 2 个复选框输入。也许可以使用相同的解决方案更改某些内容来完成,但是,由于我是使用闪亮的新手,所以我找不到方法。
上一篇文章的代码和这篇文章的不同之处在于,我添加了一个条件面板和两个复选框输入,而不是 1。
由于这里的条件是如果用户选择条件(播放),我认为解决方案是写eventReactive(input$play,{})。但是,checkboxInputs 都不起作用。
另一方面,如果您编写 eventReactive(input$change_log2,{}),其中一个 checkboxInputs(对数)有效。但是如果你选择另一个(srqt)它不会做任何事情。
我已经看到另一种方法是使用 observe 或 observeEvent 但我无法将结果保存在变量中,所以...我需要 eventReactive...
我有点迷茫。
有人可以帮助我吗?最终我会添加更多的 checkboxInputs...所以我需要一种可以使用超过 2 个 checkboxInputs 的方法。
代码如下:
library(shiny)
# Define UI
ui <- fluidPage(
# Application title
titlePanel("My app"),
sidebarLayout(
sidebarPanel(
uiOutput("selected_sample_one"),
uiOutput("selected_sample_two"),
checkboxInput("play", strong("I want to play my data"), value = FALSE),
conditionalPanel(
condition = "input.play == 1",
checkboxInput("change_log2", "Log2 transformation", value = FALSE),
checkboxInput("run_sqrt", "sqrt option", value = FALSE))
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("plot")
)
)
)
# Define server
server <- function(input, output,session) {
data <- reactive({
numbers <- c(5,345,55,10)
df<-data.frame(t(numbers))
names(df) <- c("S1", "S2", "S3", "S4")
return(df)
})
data1 <- eventReactive(input$play,{
df <- data()
if(input$change_log2 == TRUE){
df <- log2(df)
}
if(input$run_sqrt == TRUE){
df <- sqrt(df)
}
return(df)
})
samples_names <- reactive({
req(data())
samples <- colnames(data())
return(samples)
})
output$selected_sample_one <- renderUI({
selectizeInput(inputId = "sample_one_axis", "Select the 1st sample", choices=samples_names(), options=list(maxOptions = length(samples_names())))
})
# With this function you can select which sample do you want to plot in the y-axis.
output$selected_sample_two <- renderUI({
selectizeInput(inputId = "sample_two_axis", "Select the 2nd sample", choices=samples_names(), selected=samples_names()[2], options=list(maxOptions = length(samples_names())))
})
output$plot <- renderPlot({
req(input$sample_one_axis,input$sample_two_axis,data1())
barplot(c(data1()[,input$sample_one_axis], data1()[,input$sample_two_axis]))
})
}
# Run the application
shinyApp(ui = ui, server = server)
非常感谢,
问候
【问题讨论】:
-
只需将
data1 <- eventReactive替换为data1 <- reactive就可以了。 -
@Limey 是的!我没有尝试这样做,因为我遇到了反应性问题,有人在我的另一篇帖子中用
eventReactive解决了这个问题。但是,现在有 2 个选项,实际上我没有问题(至少现在)。非常感谢!
标签: r shiny selectinput