R / Shiny - 如何写一个.csv反应包括一个actionButton? [英] R/Shiny - How to write a .csv reactively including a actionButton?
问题描述
我想弄清楚如何根据最终用户的选择来编写.csv。所做的选择将对geodata.csv进行子集化,并在应用程序文件夹中写入一个单独的solution.csv文件。
NB 我已创建了 github repo ,以便更轻松地解决问题。它包含geodata.csv,ui.R& server.R但没有solution.csv!
geodata.csv
邮政编码,HC,BSL,位置位置
10,1,A,C
10,1,A,D
10,1,A,D
11,1, C
11,1,B,C
ui.R
shinyUI(
pageWithSidebar(
headerPanel('Min。working example - write a csv based on用户输入'),
sidebarPanel(
selectInput(filter1,First selection:
,choices = c(Choose ='',A,B )
#,multiple = T
),
selectInput(filter2,Second choice:,
choices = c(Choose = (Include actionButton以防止在用户最终选择之前写入),
actionButton(generateButton ,Write Data)
),
mainPanel()
)
)
$ b b
server.R
#载入资料
setwd (/ Users / lukesingham / SOtestApp)
geodata< - read.csv(geodata.csv,na.string =#N / A,row.names = NULL)
#对子集数据的反应性####
shinyServer(function(input,output){
geodatasetInput< - reactive({
#BSL switch
选择<-switch(输入$ BSL
,A =A
,B =B
)
#位置切换
selection2 <开关(输入$ location
,C =C
,D =D
)
基于选择的子集
subgeodata& - 子集(地理数据,BSL ==选择&位置。位置==选择2)
#按下按钮时对数据执行选择
输入$ generateButton
根据邮政编码汇总
subgeodata< - subgeodata [1:2]#不再需要其他列
AggSubGdata< - aggregate(。〜Postcode,data = Subgeodata,FUN = sum)
isolate(write.csv(AggSubGdata
,file =/Users/luksingham/SOtestApp/solution.csv
,row.names = F
))
})
})
solution.csv
根据用户选择
A
和D
,解决方案文件应如下所示:邮政编码,HC
10,2
解决方案这是一个工作示例:
#加载数据
setwd(/ Users / lukesingham / SOtestApp)
geodata< - read.csv(geodata.csv,na.string =#N / A,row.names = NULL)
#对子集数据的反应性####
shinyServer(function(input,output){
geodatasetInput< - observe({
#按钮时输入数据
if(input $ generateButton == 0)return()
inp.BSL< - isolation(input $ filter1)
inp.loc< ; - isolate(input $ filter2)
if(inp.BSL ==''| inp.loc =='')return()
#BSL switch
selection <-switch(inp.BSL
,A =A
, B =B
)
#位置切换
selection2 <-switch(inp.loc
,C =C
,D =D
)
基于选择的子集
子地理< - 子集(地理数据,BSL ==选择& Position.Location ==选择2)
#browser()
#通过邮政编码汇总
Subgeodata< - Subgeodata [1:2]#不再需要其他列
AggSubGdata< - aggregate(。〜Postcode,data = Subgeodata ,FUN = sum)
write.csv(AggSubGdata
,file =solution.csv
,row.names = F
)
})$ b b})
并对您的代码进行简短分析:
geodatasetInput
不能启动的主要原因是因为它是一个)
表达式。 <@ c $ c> renderTable()在@pops的回答中调用时,如果你想要它自己执行,它应该是
observe()
。
在你的observe()表达式的开头输入$ generateButton 是一个好主意。
在
ui.R
中,您调用数字输入字段filter1
,但您尝试获取其值为input $ BSL
从server.R
;filter2
你想要
code>仅在geodataSetInput
generateButton
上触发,所有其他输入$
和具有geodataSetInput
应该用isolate()
隔离。另一方面,没有必要隔离write.csv
,因为这个特定的函数调用不涉及任何'动态'参数。
I am trying to figure out how to write a .csv based on selections made by the end user. The selections made will subset the "geodata.csv" and write a separate "solution.csv" file in the application folder.
N.B. - I have created a github repo to make solving the question easier. It contains the geodata.csv, ui.R & server.R but no solution.csv yet!
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
For example, based on user selections of
A
andD
the solution file should look like this:Postcode,HC 10,2
解决方案Here is the working example:
# 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 ) }) })
and a short analysis of your code:
The main reason
geodatasetInput
does not start at all is because it is areactive()
expression.reactive()
is evaluated only when it is called by something else likerenderTable()
in the answer of @pops. If you want it to be executed by itself, it should beobserve()
.It might be a good idea to have
input$generateButton
in the beginning of your observe() expression.In
ui.R
, you call a numeric input fieldfilter1
, but you try to obtain its value asinput$BSL
fromserver.R
; the same is true forfilter2
.As you want
geodataSetInput
to be triggered only ongenerateButton
, all otherinput$
and reactive expressions withinggeodataSetInput
should be isolated withisolate()
. On the other hand, there is no need to isolatewrite.csv
because this particular function call does not involve any 'dynamic' parameters.
这篇关于R / Shiny - 如何写一个.csv反应包括一个actionButton?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!