gpt4 book ai didi

R/Shiny - 如何响应式地编写包含 actionButton 的 .csv?

转载 作者:行者123 更新时间:2023-12-02 07:26:34 27 4
gpt4 key购买 nike

我试图弄清楚如何根据最终用户的选择编写 .csv。所做的选择将子集“geodata.csv”并在应用程序文件夹中写入单独的“solution.csv”文件。

注意 - 我创建了一个 github repo使解决问题更容易。它包含 geodata.csv、ui.R 和 server.R 但还没有 solution.csv!

geodata.csv

Postcode,HC,BSL,Position Location
10,1,A,C
10,1,A,D
10,1,A,D
11,1,B,C
11,1,B,C

ui.R

shinyUI(
pageWithSidebar(
headerPanel('Min. working example - write a csv based on user input'),

sidebarPanel(
selectInput("filter1", "First selection:"
, choices = c(Choose='', "A", "B")
#, multiple=T
),

selectInput("filter2", "Second selection:",
choices = c(Choose='', "C", "D")
),
br(),
p("Include actionButton to prevent write occuring before user finalises selection"),
actionButton("generateButton","Write Data")
),
mainPanel()
)
)

server.R

# Load data
setwd("/Users/lukesingham/SOtestApp")
geodata <- read.csv("geodata.csv", na.string = "#N/A", row.names=NULL)

# Reactivity to subset data ####
shinyServer(function(input, output) {
geodatasetInput <- reactive({

# BSL switch
selection <-switch(input$BSL
, A = "A"
, B = "B"
)
# Location switch
selection2 <-switch(input$Location
, C = "C"
, D = "D"
)

# subset based on selection
Subgeodata <- subset(geodata, BSL == selection & Position.Location == selection2)

# Execute selections on data upon button-press
input$generateButton

# aggregate by postcode
Subgeodata <- Subgeodata[1:2] #no longer need other columns
AggSubGdata <- aggregate(. ~ Postcode, data=Subgeodata, FUN=sum)
isolate(write.csv(AggSubGdata
, file = "/Users/lukesingham/SOtestApp/solution.csv"
, row.names=F
))
})
})

solution.csv

例如,根据用户对 AD 的选择,解决方案文件应如下所示:

Postcode,HC
10,2

最佳答案

下面是工作示例:

# Load data
setwd("/Users/lukesingham/SOtestApp")
geodata <- read.csv("geodata.csv", na.string = "#N/A", row.names=NULL)

# Reactivity to subset data ####
shinyServer(function(input, output) {
geodatasetInput <- observe({

# Execute selections on data upon button-press
if(input$generateButton == 0) return()

inp.BSL <- isolate(input$filter1)
inp.loc <- isolate(input$filter2)
if (inp.BSL=='' | inp.loc=='') return()

# BSL switch
selection <-switch(inp.BSL
, A = "A"
, B = "B"
)
# Location switch
selection2 <-switch(inp.loc
, C = "C"
, D = "D"
)

# subset based on selection
Subgeodata <- subset(geodata, BSL == selection & Position.Location == selection2)

# browser()
# aggregate by postcode
Subgeodata <- Subgeodata[1:2] #no longer need other columns
AggSubGdata <- aggregate(. ~ Postcode, data=Subgeodata, FUN=sum)
write.csv(AggSubGdata
, file = "solution.csv"
, row.names=F
)
})
})

以及对您的代码的简短分析:

  1. geodatasetInput 根本没有启动的主要原因是因为它是一个reactive() 表达式。 reactive() 仅在 @pops 的答案中被 renderTable() 之类的其他东西调用时才被评估。如果想让它自己执行,应该是observe()

  2. 在 observe() 表达式的开头使用 input$generateButton 可能是个好主意。

  3. ui.R中,你调用了一个数字输入域filter1,但是你试图以input$BSL的形式获取它的值> 来自 server.R; filter2 也是如此。

  4. 您希望 geodataSetInput 仅在 generateButton、所有其他 input$ 和带有 geodataSetInput 的响应式(Reactive)表达式上触发 应该与 isolate() 隔离。另一方面,没有必要隔离 write.csv,因为这个特定的函数调用不涉及任何“动态”参数。

关于R/Shiny - 如何响应式地编写包含 actionButton 的 .csv?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27854248/

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