How do i refresh a value from a CSV once the CSV is updated on interval in R?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















EDIT: I've made changes based upon below feedback but i'm still having the same issue with the file not refreshing. I've updated the code below with the edits as well as the function which made it difficult to reproduce



I have a CSV file that is being updated every 120 seconds. I am then doing calculations against this file and changing the color of text in ui.r.



I am really struggling with the syntax and while i can get everything to work when i launch the app, make changes, and relaunch the app, I cannot get my app to auto update.



The below code works fine when executed together in the console. So i'm confident I can read the file, do my calculation, and assign a value.



source("BusinessLogic.R") #This contains getColor()
my_df <- read.csv("test.csv")
my_value <- mean(my_df$score)
ui_value <- getColor(my_value)
ui_value
>[1] "yellow"


However in my full code, have two issues:
1) It seems as though my app is reading the file from memory instead of actually checking it again every time i launch the app



2) I cannot get the app to autorefresh the file and update the UI.



I was previously playing with IntervalLater but switching to reactiveFileReader thinking it might be easier. I've commented out the some of the variations i've been trying.



library(shiny)

#source("BusinessLogic.R") #Contains function getColor()
#my_df <- read.csv("test.csv")

ui <- htmlTemplate(
text_ = '
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="application/shiny-singletons"></script>
<script type="application/html-
dependencies">json2[2014.02.04];jquery[1.12.4];shiny[1.2.0]</script>
</head>
<body>
<p style="color:{{ui_value}}">color</p>
</body>
</html>'
)
server <- function(input, output, session) {
getColor <- function(x) {
if (x > 80) {
result <- "green"
}
else if (x > 50) {
result <- "yellow"
}
else {
result <- "red"
}
return(result)
}
my_df <- reactiveFileReader(1000, session, 'test.csv', read.csv)
#my_df <- read.csv("test.csv")
observe({
invalidateLater(1000, session)
my_value <- mean(my_df()$score)
output$ui_value <- getColor(my_value)
})
}
shinyApp(ui, server)


When executed correctly, the UI would update this color to red green or whatever anytime the average in that particular column (score) changes.










share|improve this question

























  • Make sure you are using my_df() to call the variable since it is reactive.

    – Chabo
    Jan 3 at 23:10











  • Try putting my_df <- read.csv("test.csv") under the observer as well, this will call the read.csv function when it invalidates (re-runs) but I am not sure if it will still be stubborn. Note you will need to remove the () following the call since it would no longer be considered reactive.

    – Chabo
    Jan 4 at 15:26




















0















EDIT: I've made changes based upon below feedback but i'm still having the same issue with the file not refreshing. I've updated the code below with the edits as well as the function which made it difficult to reproduce



I have a CSV file that is being updated every 120 seconds. I am then doing calculations against this file and changing the color of text in ui.r.



I am really struggling with the syntax and while i can get everything to work when i launch the app, make changes, and relaunch the app, I cannot get my app to auto update.



The below code works fine when executed together in the console. So i'm confident I can read the file, do my calculation, and assign a value.



source("BusinessLogic.R") #This contains getColor()
my_df <- read.csv("test.csv")
my_value <- mean(my_df$score)
ui_value <- getColor(my_value)
ui_value
>[1] "yellow"


However in my full code, have two issues:
1) It seems as though my app is reading the file from memory instead of actually checking it again every time i launch the app



2) I cannot get the app to autorefresh the file and update the UI.



I was previously playing with IntervalLater but switching to reactiveFileReader thinking it might be easier. I've commented out the some of the variations i've been trying.



library(shiny)

#source("BusinessLogic.R") #Contains function getColor()
#my_df <- read.csv("test.csv")

ui <- htmlTemplate(
text_ = '
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="application/shiny-singletons"></script>
<script type="application/html-
dependencies">json2[2014.02.04];jquery[1.12.4];shiny[1.2.0]</script>
</head>
<body>
<p style="color:{{ui_value}}">color</p>
</body>
</html>'
)
server <- function(input, output, session) {
getColor <- function(x) {
if (x > 80) {
result <- "green"
}
else if (x > 50) {
result <- "yellow"
}
else {
result <- "red"
}
return(result)
}
my_df <- reactiveFileReader(1000, session, 'test.csv', read.csv)
#my_df <- read.csv("test.csv")
observe({
invalidateLater(1000, session)
my_value <- mean(my_df()$score)
output$ui_value <- getColor(my_value)
})
}
shinyApp(ui, server)


When executed correctly, the UI would update this color to red green or whatever anytime the average in that particular column (score) changes.










share|improve this question

























  • Make sure you are using my_df() to call the variable since it is reactive.

    – Chabo
    Jan 3 at 23:10











  • Try putting my_df <- read.csv("test.csv") under the observer as well, this will call the read.csv function when it invalidates (re-runs) but I am not sure if it will still be stubborn. Note you will need to remove the () following the call since it would no longer be considered reactive.

    – Chabo
    Jan 4 at 15:26
















0












0








0


1






EDIT: I've made changes based upon below feedback but i'm still having the same issue with the file not refreshing. I've updated the code below with the edits as well as the function which made it difficult to reproduce



I have a CSV file that is being updated every 120 seconds. I am then doing calculations against this file and changing the color of text in ui.r.



I am really struggling with the syntax and while i can get everything to work when i launch the app, make changes, and relaunch the app, I cannot get my app to auto update.



The below code works fine when executed together in the console. So i'm confident I can read the file, do my calculation, and assign a value.



source("BusinessLogic.R") #This contains getColor()
my_df <- read.csv("test.csv")
my_value <- mean(my_df$score)
ui_value <- getColor(my_value)
ui_value
>[1] "yellow"


However in my full code, have two issues:
1) It seems as though my app is reading the file from memory instead of actually checking it again every time i launch the app



2) I cannot get the app to autorefresh the file and update the UI.



I was previously playing with IntervalLater but switching to reactiveFileReader thinking it might be easier. I've commented out the some of the variations i've been trying.



library(shiny)

#source("BusinessLogic.R") #Contains function getColor()
#my_df <- read.csv("test.csv")

ui <- htmlTemplate(
text_ = '
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="application/shiny-singletons"></script>
<script type="application/html-
dependencies">json2[2014.02.04];jquery[1.12.4];shiny[1.2.0]</script>
</head>
<body>
<p style="color:{{ui_value}}">color</p>
</body>
</html>'
)
server <- function(input, output, session) {
getColor <- function(x) {
if (x > 80) {
result <- "green"
}
else if (x > 50) {
result <- "yellow"
}
else {
result <- "red"
}
return(result)
}
my_df <- reactiveFileReader(1000, session, 'test.csv', read.csv)
#my_df <- read.csv("test.csv")
observe({
invalidateLater(1000, session)
my_value <- mean(my_df()$score)
output$ui_value <- getColor(my_value)
})
}
shinyApp(ui, server)


When executed correctly, the UI would update this color to red green or whatever anytime the average in that particular column (score) changes.










share|improve this question
















EDIT: I've made changes based upon below feedback but i'm still having the same issue with the file not refreshing. I've updated the code below with the edits as well as the function which made it difficult to reproduce



I have a CSV file that is being updated every 120 seconds. I am then doing calculations against this file and changing the color of text in ui.r.



I am really struggling with the syntax and while i can get everything to work when i launch the app, make changes, and relaunch the app, I cannot get my app to auto update.



The below code works fine when executed together in the console. So i'm confident I can read the file, do my calculation, and assign a value.



source("BusinessLogic.R") #This contains getColor()
my_df <- read.csv("test.csv")
my_value <- mean(my_df$score)
ui_value <- getColor(my_value)
ui_value
>[1] "yellow"


However in my full code, have two issues:
1) It seems as though my app is reading the file from memory instead of actually checking it again every time i launch the app



2) I cannot get the app to autorefresh the file and update the UI.



I was previously playing with IntervalLater but switching to reactiveFileReader thinking it might be easier. I've commented out the some of the variations i've been trying.



library(shiny)

#source("BusinessLogic.R") #Contains function getColor()
#my_df <- read.csv("test.csv")

ui <- htmlTemplate(
text_ = '
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="application/shiny-singletons"></script>
<script type="application/html-
dependencies">json2[2014.02.04];jquery[1.12.4];shiny[1.2.0]</script>
</head>
<body>
<p style="color:{{ui_value}}">color</p>
</body>
</html>'
)
server <- function(input, output, session) {
getColor <- function(x) {
if (x > 80) {
result <- "green"
}
else if (x > 50) {
result <- "yellow"
}
else {
result <- "red"
}
return(result)
}
my_df <- reactiveFileReader(1000, session, 'test.csv', read.csv)
#my_df <- read.csv("test.csv")
observe({
invalidateLater(1000, session)
my_value <- mean(my_df()$score)
output$ui_value <- getColor(my_value)
})
}
shinyApp(ui, server)


When executed correctly, the UI would update this color to red green or whatever anytime the average in that particular column (score) changes.







r shiny






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 4 at 2:32







Basballguy

















asked Jan 3 at 22:34









BasballguyBasballguy

266




266













  • Make sure you are using my_df() to call the variable since it is reactive.

    – Chabo
    Jan 3 at 23:10











  • Try putting my_df <- read.csv("test.csv") under the observer as well, this will call the read.csv function when it invalidates (re-runs) but I am not sure if it will still be stubborn. Note you will need to remove the () following the call since it would no longer be considered reactive.

    – Chabo
    Jan 4 at 15:26





















  • Make sure you are using my_df() to call the variable since it is reactive.

    – Chabo
    Jan 3 at 23:10











  • Try putting my_df <- read.csv("test.csv") under the observer as well, this will call the read.csv function when it invalidates (re-runs) but I am not sure if it will still be stubborn. Note you will need to remove the () following the call since it would no longer be considered reactive.

    – Chabo
    Jan 4 at 15:26



















Make sure you are using my_df() to call the variable since it is reactive.

– Chabo
Jan 3 at 23:10





Make sure you are using my_df() to call the variable since it is reactive.

– Chabo
Jan 3 at 23:10













Try putting my_df <- read.csv("test.csv") under the observer as well, this will call the read.csv function when it invalidates (re-runs) but I am not sure if it will still be stubborn. Note you will need to remove the () following the call since it would no longer be considered reactive.

– Chabo
Jan 4 at 15:26







Try putting my_df <- read.csv("test.csv") under the observer as well, this will call the read.csv function when it invalidates (re-runs) but I am not sure if it will still be stubborn. Note you will need to remove the () following the call since it would no longer be considered reactive.

– Chabo
Jan 4 at 15:26














1 Answer
1






active

oldest

votes


















1















1) It seems as though my app is reading the file from memory instead
of actually checking it again every time i launch the app




You are not calling the file correctly once it is uploaded to the reactive varaible my_df



Instead:



my_df <- reactiveFileReader(1000, session, 'test.csv', read.csv)
my_value <- mean(my_df()$score)



2) I cannot get the app to autorefresh the file and update the UI.




You are only running your server code once as you are not using any reactive environments to re-analyze the result.



observe({
invalidateLater(1000, session)
my_value <- mean(my_df()$score)
output$ui_value <- getColor(my_value)
})


Using an observe function with invalidateLater allows us to re-run the reactive expression at the same rate the file should be re-uploaded.



Note: I cannot test this code as it is not reproducible.






share|improve this answer
























  • Thanks for the right direction. I went ahead and added how getColor works as it's a simple function.

    – Basballguy
    Jan 4 at 2:23












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54030733%2fhow-do-i-refresh-a-value-from-a-csv-once-the-csv-is-updated-on-interval-in-r%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1















1) It seems as though my app is reading the file from memory instead
of actually checking it again every time i launch the app




You are not calling the file correctly once it is uploaded to the reactive varaible my_df



Instead:



my_df <- reactiveFileReader(1000, session, 'test.csv', read.csv)
my_value <- mean(my_df()$score)



2) I cannot get the app to autorefresh the file and update the UI.




You are only running your server code once as you are not using any reactive environments to re-analyze the result.



observe({
invalidateLater(1000, session)
my_value <- mean(my_df()$score)
output$ui_value <- getColor(my_value)
})


Using an observe function with invalidateLater allows us to re-run the reactive expression at the same rate the file should be re-uploaded.



Note: I cannot test this code as it is not reproducible.






share|improve this answer
























  • Thanks for the right direction. I went ahead and added how getColor works as it's a simple function.

    – Basballguy
    Jan 4 at 2:23
















1















1) It seems as though my app is reading the file from memory instead
of actually checking it again every time i launch the app




You are not calling the file correctly once it is uploaded to the reactive varaible my_df



Instead:



my_df <- reactiveFileReader(1000, session, 'test.csv', read.csv)
my_value <- mean(my_df()$score)



2) I cannot get the app to autorefresh the file and update the UI.




You are only running your server code once as you are not using any reactive environments to re-analyze the result.



observe({
invalidateLater(1000, session)
my_value <- mean(my_df()$score)
output$ui_value <- getColor(my_value)
})


Using an observe function with invalidateLater allows us to re-run the reactive expression at the same rate the file should be re-uploaded.



Note: I cannot test this code as it is not reproducible.






share|improve this answer
























  • Thanks for the right direction. I went ahead and added how getColor works as it's a simple function.

    – Basballguy
    Jan 4 at 2:23














1












1








1








1) It seems as though my app is reading the file from memory instead
of actually checking it again every time i launch the app




You are not calling the file correctly once it is uploaded to the reactive varaible my_df



Instead:



my_df <- reactiveFileReader(1000, session, 'test.csv', read.csv)
my_value <- mean(my_df()$score)



2) I cannot get the app to autorefresh the file and update the UI.




You are only running your server code once as you are not using any reactive environments to re-analyze the result.



observe({
invalidateLater(1000, session)
my_value <- mean(my_df()$score)
output$ui_value <- getColor(my_value)
})


Using an observe function with invalidateLater allows us to re-run the reactive expression at the same rate the file should be re-uploaded.



Note: I cannot test this code as it is not reproducible.






share|improve this answer














1) It seems as though my app is reading the file from memory instead
of actually checking it again every time i launch the app




You are not calling the file correctly once it is uploaded to the reactive varaible my_df



Instead:



my_df <- reactiveFileReader(1000, session, 'test.csv', read.csv)
my_value <- mean(my_df()$score)



2) I cannot get the app to autorefresh the file and update the UI.




You are only running your server code once as you are not using any reactive environments to re-analyze the result.



observe({
invalidateLater(1000, session)
my_value <- mean(my_df()$score)
output$ui_value <- getColor(my_value)
})


Using an observe function with invalidateLater allows us to re-run the reactive expression at the same rate the file should be re-uploaded.



Note: I cannot test this code as it is not reproducible.







share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 3 at 23:19









ChaboChabo

1,2301823




1,2301823













  • Thanks for the right direction. I went ahead and added how getColor works as it's a simple function.

    – Basballguy
    Jan 4 at 2:23



















  • Thanks for the right direction. I went ahead and added how getColor works as it's a simple function.

    – Basballguy
    Jan 4 at 2:23

















Thanks for the right direction. I went ahead and added how getColor works as it's a simple function.

– Basballguy
Jan 4 at 2:23





Thanks for the right direction. I went ahead and added how getColor works as it's a simple function.

– Basballguy
Jan 4 at 2:23




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54030733%2fhow-do-i-refresh-a-value-from-a-csv-once-the-csv-is-updated-on-interval-in-r%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas