gpt4 book ai didi

r - Shiny 的 R 操作按钮控制 react 元素

转载 作者:行者123 更新时间:2023-12-04 12:11:05 25 4
gpt4 key购买 nike

不确定我是否应该使用这个术语。基本上,我有一个显示用户上传的 CSV 文件的 react 函数,我想使用 action button 来触发绘图生成过程。此时此刻,情节总是即时生成的。所以我想知道,在 renderPlot 函数中,如何让 action button 覆盖 reactive 元素。谢谢!

ui.R

library(shiny)

shinyUI(fluidPage(
titlePanel("Test"),
sidebarLayout(
sidebarPanel(
fileInput('file1', 'Choose CSV File',
accept=c('text/csv',
'text/comma-separated-values,text/plain',
'.csv')),

checkboxGroupInput('show_vars', 'Columns to show:',
choices=c("Col1"="Col1", "Col2"="Col2", "Col3"="Col3"),
c("Col1"="Col1", "Col2"="Col2", "Col3"="Col3")),

uiOutput("study"),
actionButton("go", "Draw the plot")
),
mainPanel(
dataTableOutput('contents'),
plotOutput("Plot", width = 800, height = 800)

)
)
))

服务器.R

shinyServer(function(input, output) {

dataInput <- reactive({
inFile <- input$file1
if (is.null(inFile)) return(NULL)
data_load<-subset(read.csv(inFile$datapath, header=T, sep=",", quote='"', stringsAsFactors=FALSE), select=input$show_vars)
data_study<-unique(data_load$Col2)
return (list("data_load"=data_load, "data_study"=data_study))
})

study <- reactive({
all_citation<-dataInput()$data_load$Col2
study_choose<-unlist(lapply(input$columns, function(x) which(all_citation==x)))
return (list("study_choose"=study_choose))
})

##Generate the data table####
output$contents <- renderDataTable({
study_choose_temp<-study()$study_choose
dataInput()$data_load[study_choose_temp,]
})

###I would like the actionButton controls reactive elements
###dataInput()$data_load$Col3[study()$study_choose]

output$Plot <- renderPlot({
input$go
hist(dataInput()$data_load$Col3[study()$study_choose])
})

output$study <- renderUI({
# If missing input, return to avoid error later in function
if(is.null(dataInput()$data_study)) return()
# Create the checkboxes and select them all by default
checkboxGroupInput("columns", "Choose studies to plot",
choices = dataInput()$data_study,
selected = dataInput()$data_study)

})
})

###CSV template

最佳答案

renderPlot() 中使用 isolate,这样它只会在 input$go 按钮被触发时更新。如果这样做,当其他 react 元素发生变化时,绘图不会重绘,但是 input$go 位于 isolate 之外,因此会导致绘图重绘。

 output$Plot <- renderPlot({
input$go
isolate(
hist(dataInput()$data_load$Col3[study()$study_choose])
)
})

关于r - Shiny 的 R 操作按钮控制 react 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26890465/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com