Deploy Shiny App with a scheduled script - r

I have a simple script that in Rstudio works to deploy app:
rsconnect::setAccountInfo(name='xx', token='xx', secret='xx/xx')
library(rsconnect)
deployApp("xxx",launch.browser = FALSE)
After this prompt appears:
Update application currently deployed at https://xxx.shinyapps.io/xx/?
that block my scheduled script.
There's a way to skip this request and update the shiny app without manually type "Y" in the Console?

Adding to what waskuf said, try adding forceUpdate = T to your code.
deployApp("xxx", launch.browser = F, forceUpdate = T)
Worked for me, at least.

It works if you just write an unquoted Y in your script after the "deployApp" command and run it in one batch. Like this:
rsconnect::setAccountInfo(name='xx', token='xx', secret='xx/xx')
library(rsconnect)
deployApp("xxx", launch.browser = FALSE)
Y
Just make sure the lines including deployApp(...) and Y are both selected and not seperated by any other commands when executed.

Related

Pyinstaller opens Stockfish in console

I have made a GUI (using PySimpleGUI) where you can play against Stockfish (I used python-chess module). I make an .exe-file using Pyinstaller --noconsole, but when i run it, it opens Stockfish in a console. When I run it form source, in PyCharm, Stockfish runs silently in the background.
The relevant lines of code are (I guess):
engine = chess.engine.SimpleEngine.popen_uci(engine_filename, shell = False)
and, a bit later,
best_move = engine.play(board, chess.engine.Limit(depth=20)).move
Any advice on how I can make Stockfish run silently in the background also form the .exe-file?
Define your engine like below.
import subprocess
engine = chess.engine.SimpleEngine.popen_uci(
engine_filename,
shell = False,
creationflags=subprocess.CREATE_NO_WINDOW)
See python subprocess ref.

Cron Job to execute R script that updates table not working! (no error shown in console)

When I run this in R, the console gives no error and it looks like the job has been setup but apparently not. The job is to update a table in database every minute and by chosing a random sample (since this is a dummy). I'm not sure what is not working.
library(cronR)
path = "~/Documents/GitHub/abcdef/some_folder/DBI dummy.R"
cmd = cron_rscript(path)
cron_add(cmd, frequency = "minutely", at ="3:15PM", days_of_week = c(1:5),
id = "Automated_scheduler_test_3", description = "Testing_for_sample")
This is the script i'm trying to run:
library(DBI)
library(dplyr)
con <- connect_to_*database*()
dbRemoveTable(connect_to_*database*(schema = "*schema name*"), "*table_name*")
sample <- sample_n((dbReadTable(connect_to_*database*(schema = "*schema name*"), "*table_name*")),5)
table_id <- Id(database="*database_name*", schema="*schema_name*", table="*table_name*")
dbWriteTable(connect_to_database(schema = "*schema name*"), table_id, sample_n(sample,5), overwrite = TRUE)
I'm also unable to find a log for this cron job which could confirm if the job is executing.
Make sure
to cron_rscript, you provide the full path as in "/home/userabc/Documents/GitHub/abcdef/some_folder/DBIdummy.R" not a relative path like when using ~ and make sure the path does not use spaces
the cron daemon is running
consider adding options(echo = TRUE) at the start of your R scripts in order to debug your scripts in case of errors.
look to the log of the cron job, the path where it is you can find out when printing cmd

How to use shiny app as a target in drake

How to pass previous target (df) to ui and server functions that I use in the next command shinyApp. My plan looks like this:
plan <- drake_plan(
df = faithful,
app = shinyApp(ui, server)
)
ui and server are copied from the shiny tutorial. There's only one difference - I changed faithful to df (data in the previous target).
Now I'm getting an error:
Warning: Error in $: object of type 'closure' is not subsettable
[No stack trace available]
How to solve this? What's the best practice?
drake targets should return fixed data objects that can be stored with saveRDS() (or alternative kinds of files if you are using specialized formats). I recommend having a look at https://books.ropensci.org/drake/plans.html#how-to-choose-good-targets. There issues with defining a running instance of a Shiny app as a target.
As long as the app is running, make() will never finish.
It does not really make sense to save the return value of shinyApp() as a data object. That's not really what a target is for. The purpose of a target is to reproducibly cache the results of a long computation so you do not need to rerun it unless some upstream code or data change.
Instead, I think the purpose of the app target should be to deploy to a website like https://shinyapps.io. To make the app update when df changes, be sure to mention df as a symbol in a command so that drake's static code analyzer can pick it up. Also, use file_in() to declare your Shiny app scripts as dependencies so drake automatically redeploys the app when the code changes.
library(drake)
plan <- drake_plan(
df = faithful,
deployment = custom_deployment_function(file_in("app.R"), df)
)
custom_deployment_function <- function(file, ...) {
rsconnect::deployApp(
appFiles = file,
appName = "your_name",
forceUpdate = TRUE
)
}
Also, be sure to check the dependency graph so you know drake will run the correct targets in the correct order.
vis_drake_graph(plan)
In your previous plan, the command for the app did not mention the symbol df, so drake did not know it needed to run one before the other.
plan <- drake_plan(
df = faithful,
app = shinyApp(ui, server)
)
vis_drake_graph(plan)

R Studio Shiny Server - $ operator is invalid when using SQL scripts

Anyone run in to his problem? I have a shiny app that uses SQL files to import data from MS SQL server using the RODBC package. I've narrowed down the problem to here in the server.R file:
ch <- odbcConnect(dsn = xxxxxx)
iQry <- readChar("LeaderDashInd.sql", file.info("LeaderDashInd.sql")$size, T)
oQry <- readChar("LeaderDashOrg.sql", file.info("LeaderDashOrg.sql")$size, T)
iDat <- sqlQuery(channel = ch, query = iQry, stringsAsFactors = F)
oDat <- sqlQuery(channel = ch, query = oQry, stringsAsFactors = F)
odbcClose(ch)
# PREPROCESSING --------------------------
cy <- max(iDat$CampYear)
The app stops at the last line above and gives... Error in iDat$CampYear : $ operator is invalid for atomic vectors. I know this chunk is the problem because when I have the same app run off imported csv files, it works.
A couple things to note:
This code runs first in the server.R file outside the shinyServer function.
The app runs fine when launched from my workstation through R Studio. It only stops working when run on our Shiny Server installation.
Shiny packages are up to date and shiny server is a recent install.
Any thoughts?
FYI... changing the SQL query to a stored procedure and then calling the stored procedure with RODBC::sqlQuery fixed the problem. The $ operator invalid error was coming up because the query was not running and returning an empty vector.
Still doesn't explain why the stand alone query using readChar doesn't work in Shiny Server but hey... its a fix.

Is it possible to run (execute) an r script from inside of an r script?

first time here and maybe I am out of place with this question, but I was unable to found an answer
I want to run a Shiny App with only one button, that runs an R script in order to generate a PPT file, something like what happens in this link, just instead of the MSWord output the PPT.
I want to avoid to have all the lines inside the ReporteRs-PPT_v3.R script inside the Shiny script and just call it or execute it like in any other lenguage.
I was trying something like this:
observe({
source("C:/Data/Example/Shiny/App-Ficha/ReporteRs-PPT_v3.R")
})
Also tried this other option:
observe({
R CMD BATCH /ReporteRs-PPT_v3.R
})
Where ReporteRs-PPT_v3.R is an existent script that by itself generates a PPT file using ReporteRs
Any idea on where can I keep looking?
Thanks.
[UPDATE]
The error when try the first option:
Listening on http://127.0.0.1:6158
Error in source(serverR, local = new.env(parent = globalenv()), keep.source = TRUE) :
C:\Data\Example\Shiny\App-Ficha/server.R:219:0: unexpected end of input
217: }
218: )
^
The error with the second option:
Listening on http://127.0.0.1:6158
Error in source(serverR, local = new.env(parent = globalenv()), keep.source = TRUE) :
C:\Data\Example\Shiny\App-Ficha/server.R:161:9: unexpected symbol
160: #source("C:/Data/Example/Shiny/App-Ficha/ReporteRs-PPT_v3.R")
161: R CMD
^
Also, when I click the button in the Shiny App, nothing happens.

Resources