本文介绍了R / Shiny - 如何写一个.csv反应包括一个actionButton?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我想弄清楚如何根据最终用户的选择来编写.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 选择,A =A,B =B)#位置切换 selection2 ,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 ,A =A, B =B)#位置切换 selection2 ,C =C,D =D ) 基于选择的子集子地理 #browser()#通过邮政编码汇总 Subgeodata< - Subgeodata [1:2]#不再需要其他列 AggSubGdata< - aggregate(。〜Postcode,data = Subgeodata ,FUN = sum) write.csv(AggSubGdata ,file =solution.csv,row.names = F )})$ b b}) 并对您的代码进行简短分析: geodatasetInput 不能启动的主要原因是因为它是一个 )表达式。 renderTable()在@pops的回答中调用时,如果你想要它自己执行,它应该是 observe()。 在你的observe()表达式的开头输入$ generateButton 是一个好主意。 在 ui.R 中,您调用数字输入字段 filter1 ,但您尝试获取其值为 input $ BSL 从 server.R ; filter2 你想要 geodataSetInput code>仅在 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.csvPostcode,HC,BSL,Position Location10,1,A,C10,1,A,D10,1,A,D11,1,B,C11,1,B,Cui.RshinyUI( 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 datasetwd("/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.csvFor example, based on user selections of A and D the solution file should look like this:Postcode,HC10,2 解决方案 Here is the working example:# Load datasetwd("/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 a reactive() expression. reactive() is evaluated only when it is called by something else like renderTable() in the answer of @pops. If you want it to be executed by itself, it should be observe().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 field filter1, but you try to obtain its value as input$BSL from server.R; the same is true for filter2.As you want geodataSetInput to be triggered only on generateButton, all other input$ and reactive expressions withing geodataSetInput should be isolated with isolate(). On the other hand, there is no need to isolate write.csv because this particular function call does not involve any 'dynamic' parameters. 这篇关于R / Shiny - 如何写一个.csv反应包括一个actionButton?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-23 04:03