I have a helper.R, which I want to source from app.R to refresh data. helper.R has path variables based on Sys.Date(), like
data_path <- paste0("data_", Sys.Date(), ".csv")
I have a shiny application running on a shiny server and I want to source helper.R everyday using observe
helper.R
cur_date <- Sys.Date()
app.R
observe({
invalidateLater(10000, session)
if (Sys.Date() > cur_date) {
source("helper.R")
}
})
For some reason this setup is not working for data refresh. I am not much familiar with scope of variables in R. Any help is appreciated.
Related
I'm trying to write tests in order to check if a shiny function fileInput() is reading files correctly.
My problem is that I don't know what to write in session$setInputs() in order to grab the file from my system.
Here is an example app:
library(shiny)
ui <- fluidPage(
tagList(
fileInput("file", "Please upload a file"),
tableOutput("text")
)
)
server <- function(input, output, session){
file <- reactive({input$file})
output$text <- renderTable({
req(file())
read.csv(file()$datapath)
})
}
shinyApp(ui, server)
Now, I want to be able to use testServer() in order to set a file address and see if my app loads it correctly, but I can't figure out how to do it:
address <- "path/to/text.csv"
testServer(server, {
session$setInputs(file = address)
print(file())
})
I think it has to do with the fact that fileInput() uploads the file to a temp folder and returns to shiny a dataframe where you can get the datapath, but I'm unable to simulate this pass in order to make the test work
I have the same question as you do, I did some investigating and could not find any way of testing fileInput with testServer or testthat. The best solution that I found was testing fileInput by taking a snapshot when recording a test with recordTest() of the shinytest package.
Sorry for answering this late.
I asked the same question at rstudio's forums and got an answer here
The basics of it are setting the file's datapath as a list:
address <- "path/to/text.csv"
testServer(server, { session$setInputs(file= list(datapath = address)) })
I am new to R and certainly very new to RShiny.
I wrote a packages which logs events into a log file. Rstudio is capable of viewing live logging unitl the file is 5MB. So now i am thinking about writing a Rshiny app that views the logs as they are being written to the file.
Which functions would help me to update the viewer?
Thanks!
You can call invalidateLater inside a reactive when you import the data. Data will be refreshed every time invalidateLater will fire (in my case every second).
Here a really silly example (my .csv doesn't update, it just prints that data is being refreshed to console):
library(shiny)
ui <- fluidPage(
tableOutput("data")
)
server <- function(input, output, session) {
# mtcars.csv will be read every second
mtcars_df <- reactive({
invalidateLater(1000, session)
read.csv("mtcars.csv")
})
# mtcars_df is a reactive, hence will force the table to re-render
output$data <- renderTable({
print(paste("Table refreshed at", Sys.time(), collapse = " "))
mtcars_df()
})
}
shinyApp(ui, server)
I've run into a problem with downloadHandler() in Shiny:
If I want to download any file via this function, the filename in the download window is the same as the name of the output-variable (in the example: "downloadData"), but not as it is declared in "filename=" in downloadHandler() (which should be "data-2017-02-13.csv").
Note that the following example is from the downloadHandler() - help page, so I guess there is a general problem with R or RStudio in which I write R scripts.
Additionally when I open the shiny app in a web browser, the problem vanishes.
This partially solves it, but I would still like to know why shiny is behaving differently inside RStudio and a web browser.
## Only run examples in interactive R sessions
if (interactive()) {
ui <- fluidPage(
downloadLink("downloadData", "Download")
)
server <- function(input, output) {
# Our dataset
data <- mtcars
output$downloadData <- downloadHandler(
filename = function() {
paste("data-", Sys.Date(), ".csv", sep="")
},
content = function(file) {
write.csv(data, file)
}
)
}
shinyApp(ui, server)
}
and here the download window I get:
I had the same issue when I used the RStudio preview window and was able to solve this issue by always opening a browser with the command
runApp(launch.browser = TRUE)
I have a shiny app with a number of tabPanels.
In one of these panels I want to write a time signal generated in seewave and tuneR to a www subdirectory.
If I run the commands on the R-promt, everything works, but I can't get it running in my server.R.
I tried observe, but then the input$ variables are not updated:
observeEvent(input$button,{
cat("Writing wav file")
})
eventReactive(input$button,{
"Inbutton"
pi<-4*atan(1)
s5<-synth(44100,5,input$freq1,input$amp1*2000,"sine",shape=NULL,p=0,
am=c(0,0,0),fm=c(0,0,0),harmonics=1,plot=FALSE,output="Wave")
s6<-synth(44100,5,input$freq2,input$amp2*2000,"sine",shape=NULL,
p=input$phase/180*pi,am=c(0,0,0),fm=c(0,0,0),
harmonics=1,plot=FALSE,output="Wave")
s7<-s5+s6
str(s7)
Wobj<-s7
wav_dir<-"./www"
wav_file<-file.path(wav_dir,"howling2.wav")
writeWave(Wobj,filename=wav_file)
play(s7)
})
I've managed somehow to save the file. I defined function that generates the sound and load it beforehand. Then in server function I call it like that:
server <- function(input, output){
observeEvent(input$button, {
sound <- sonify(input$name) # this is already a Wave object
wvname <- paste0("sound", input$button,".wav")
writeWave(sound, paste0("www/", wvname))
})
}
Then to implement it in UI follow this and this
I am trying to create a Shiny App.
I'd like it to download a CSV file from the web and store it on the local machine, then perform analysis.
My current approach is:
ui.R
library(shiny)
shinyUI(pageWithSidebar(
# Application title
headerPanel("TEST"),
sidebarPanel(
sliderInput("range", "Date Range:",
min = 0, max = 15, value = c(0,15))
),
# Show a tabset that includes a plot, summary, and table view
# of the generated distribution
mainPanel(
tabsetPanel(
tabPanel("Plot", plotOutput("plot"))
)
))
server.R
library(shiny)
shinyServer(function(input, output) {
datasetInput <- function(){
x1 <- strptime(Sys.time(), "%Y-%m-%d %H:%M:%S")
x2 <- strptime(file.info("/srv/shiny-server/Data/current.csv")$mtime, "%Y-%m-%d %H:%M:%S")
if ( difftime(x1, x2, units='mins') > 20 ){
str <- "wget http://www.web.com/file.csv -O /srv/shiny-server/Data/current.csv"
system(str)
}
data <- read.csv("/srv/shiny-server/Data/current.csv")
return(data)
}
output$plot <- renderPlot({
data <- datasetInput()
plot(data)
})
So, everything works. The data plots perfectly. The problem is the wget script doesn't get called. Regardless of where I put it.
For simplicity my main goal is to download and save a CSV file when the app runs. Then read in that CSV file as my main dataframe.
The ultimate goal is for my app to do the time check (Check if file is older then 20 minutes) every time someone does anything with the app. If it is older, I want to download/save the file, and update my data frame.
* note * using the wget function is a work around for a problem accessing a password protected CSV file.
The problem/solution is discussed here:
R Import - CSV file from password protected URL - in .BAT file
I don't know much about how Shiny works, the code used to generate the Shiny app is mostly from:
http://rstudio.github.io/shiny/tutorial/#tabsets
Try using the httr package instead of a raw wget. Here's an example of a Shiny application that also downloads a remote CSV file and parses it. https://github.com/trestletech/dallas-police/blob/master/shiny/server.R
Also you may find this tutorial valuable, as I think you could be using reactiveFunctions as the sources of your data input.