r - 在 Shiny 中更新复选框值
问题描述
我正在尝试制作一个 Shiny 应用程序,它执行以下操作:
1)上传这样的文件:
X Y
1 3
2 1
3 6
4 4
2) 按一个Run
按钮,默认情况下将文件值加 2,如果选中一个框,则乘以 2,
3) 根据生成的值制作散点图。
我的问题是(i)我需要选中/取消选中该框,然后再次按下Run
按钮以显示相应的图,并且(ii)该复选框on
每次都会返回。
当我在不按下按钮的情况下选中/取消选中该框时,如何更新绘图Run
?我试图将observe()
andupdateCheckboxInput()
函数放在eventReactive()
块之外,但它根本不起作用。
我的代码:
library(shiny)
library(ggplot2)
ui <- fluidPage(
fileInput(
inputId = "input_file",
label = "Choose an input file"
),
actionButton(
inputId = "run_button",
label = "Run"
),
checkboxInput(
inputId = "operation_button",
label = "multiply instead of summing",
value = FALSE
),
plotOutput(
outputId = "my_plot"
)
)
server <- function(input, output, session) {
my_data <- eventReactive(
input$run_button,
{
inFile <- input$input_file
if(is.null(inFile)){
return(NULL)
}
my_in <- read.table(inFile$datapath, header = T, sep = "\t")
my_function <- function(input, operation){
if(operation == "sum"){
input <- input + 2
}else if(operation == "multiply"){
input <- input * 2
}
return(input)
}
button_switch <- ifelse(input$operation_button == FALSE, "sum", "multiply")
observe(
{
updateCheckboxInput(session, "operation_button", "multiply instead of summing", value = button_switch)
}
)
my_in <- my_function(my_in, button_switch)
}
)
output$my_plot <- renderPlot(
{
my_df <- my_data()
ggplot(my_df, aes(x=X, y=Y)) +
geom_point()
}
)
}
shinyApp(ui = ui, server = server)
解决方案
您可以eventReactive()
依赖多个输入:
my_data <- eventReactive(c(input$run_button, input$operation_button),...)
推荐阅读
- node.js - 平静地响应耗时计算的最佳实践(带 Express 的 Node.js)
- shell - 理解 Unix 差异命令
- html - 使最后一个菜单项背景的颜色不同,以扩展并填充其余空间?
- macos - osx上不同插件中的不同Qt框架版本
- java - 执行 getFilter 时 ViewHolder 错误的行为
- matplotlib - 为什么 matplotlib.pyplot.imshow 不能正确显示颜色?
- amazon-web-services - 如何将请求的标头块从 API Gateway 传递到 Step 函数/lambda?
- opencv - 颜色上的模板匹配行为
- html - 如何设置样式但排除类及其子项?
- php - 在 php 中将 $this 作为集合传递给 $self 不起作用