[英]Incompatibility between selectizeInput and checkboxInput in Shiny
我正在创建一个应用程序,它从一个目录中获取一个文件并获取列名,让用户有机会决定要使用哪个样本。
我没有上传文件,而是创建了一个数据框(类似于我的文件)并将其放入响应式函数中(这正是我上传文件时通常所做的)。
这是数据框
numbers <- c(5,345,55,10)
df<-data.frame(t(numbers))
names(df) <- c("S1", "S2", "S3", "S4")
> df
S1 S2 S3 S4
1 5 345 55 10
我的应用程序有一个checkboxInput
,您可以决定是否要对数据checkboxInput
进行对数运算。
如果我想比较 S1 与 S2,我意识到当我不点击进入框(并且我不做对数)时,样本不会改变。 这就是我想要的。
但是,如果我决定计算对数(我单击复选框),则示例 2 会发生变化(现在它比较了 S1 与 S1,我不明白为什么也不想这样做)。
这是代码:
library(shiny)
# Define UI
ui <- fluidPage(
# Application title
titlePanel("My app"),
sidebarLayout(
sidebarPanel(
uiOutput("selected_sample_one"),
uiOutput("selected_sample_two"),
checkboxInput("change_log2", "Log2 transformation", value = F),
),
# 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")
if(input$change_log2 == TRUE){
df <- log2(df)
}
return(df)
})
samples_names <- reactive({
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(), options=list(maxOptions = length(samples_names())))
})
output$plot <- renderPlot({
barplot(c(data()[,input$sample_one_axis], data()[,input$sample_two_axis]))
})
}
# Run the application
shinyApp(ui = ui, server = server)
笔记:
如果我将列名保存到向量中,则对数和一切正常。 而不是使用
samples_names <- reactive({
samples <- colnames(data())
return(samples)
})
如果我使用: samples_names <- c("S1", "S2", "S3", "S4")
并且我用这个新的向量samples_names
更改output$selected_sample_one
和output$selected_sample_two
,第二个样本不会改变(见新图)
但是,如果我增加列数或更改表,此代码将不起作用。 出于这个原因,我把它写在一个反应式函数中......
有谁知道如何解决这个问题?
首先十分感谢
那是因为您的反应数据框包含复选框信息,而 sample_names 取决于该数据框。 要将它们分开,请创建一个新的数据帧,您可以在其中执行日志操作,而使初始数据帧不进行日志转换。 试试这个
library(shiny)
# Define UI
ui <- fluidPage(
# Application title
titlePanel("My app"),
sidebarLayout(
sidebarPanel(
uiOutput("selected_sample_one"),
uiOutput("selected_sample_two"),
checkboxInput("change_log2", "Log2 transformation", value = F)
),
# 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$change_log2,{
df <- data()
if(input$change_log2 == TRUE){
df <- log2(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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.