deploy shiny's publish button from R function-script - r

I have some code that reorganizes some spreadsheets and then creates a shiny from the spreadsheet. All of this code sits in a master script within functions. This way, when I need to run a particular function I just source the master script and run the function.
Is it possible to publish the output onto shinyapps.io without physically pushing the publish button? I just want to be able to run the function and have it publish automatically.
edit 1: is there was a way I could make "appDir" = "some function in my master script"?
edit 2: Fixed the problem to edit 1. Don't put shiny code within a function.
edit 3: Is there a way to call the app.R file and my .csv file in rsconnect if they are in different locations?

Related

Execute all R chunks of an external Rmd file from an R script

I have a .Rmd file called f1.Rmd (containing a mix of text and R chunks) and an R script called f2.R.
I would like to insert a set of R instructions in f2.R that would execute all the R chunks contained in f1.Rmd, in such a way that all variables created in f1.Rmd would be created in my current R session if I source f2.R
(similarly to what happens when clicking on "Run" -> "Run all chunks below from the Rstudio menu").
If you render the f1.Rmd file from your current environment, this should happen.
You can use rmarkdown::render() from the console or a .R script. This will create all the variables in your current environment. It will also have the side effect of making the document.
When you use the knit button in RStudio, this launches the render in a new r session as a background process.
See also the envir option for render.
See also this answer for other options. knitr: run all chunks in an Rmarkdown document
It also depends what your .Rmd is doing.

How to split Shiny app code over multiple files in RStudio? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I tried to split code for a shiny application over different files, but can't get this to work decently in Shiny. My attempt can be found in this demo
How can I split up my code over different files, but still keep the 'Run App button ' and have 'Code Completion' back in RStudio?
if not ! can i integrate shiny with Visual Studio ?
Yes, you can achieve that very easily in the same way you do that with every other project in RStudio: by using the R mechanisms provided to achieve that:
define functions and/or objects in separate files.
use source() in your main file to load their definitions
Code completion for shiny functions only occurs in RStudio when the shiny package is loaded using library(shiny). The 'Run App' button will be visible for the main file in the application. In the example below, that would be the app.R file. So if you want to run your app from within RStudio, you always have to go back to the main file.
standard example
An example :
In a file app.R you put:
library(shiny)
source('myUI.R', local = TRUE)
source('myServer.R')
shinyApp(
ui = myUI,
server = myserver
)
This code does nothing else but initiate the objects myUI and myserver and call the app.
The file myUI.R contains
source('Tabs.R')
myUI <- shinyUI({
fluidPage(
tabsetPanel(
Tab1,
Tab2
)
)
})
This file defines the UI object used in app.R. The function tabsetPanel takes a number of tabPanels as arguments. These tabPanels are created in the following file (Tabs.R), so that one has to be sourced before the UI is constructed.
The file Tabs.R contains:
Tab1 <- tabPanel("First Tab",
selectInput("select",
"Choose one",
choices = letters[1:3],
selected = 'a'))
Tab2 <- tabPanel("Second Tab",
textOutput('mychoice'))
This file creates the tabPanel objects to be added to the tabsetPanel. In my own code, I store every tabPanel definition in a separate file.
The file myServer.R contains:
myserver <- function(input,output,session){
output$mychoice <- renderText(
input$select
)
}
And if you want, you can again create separate files with functions that can be used inside the server function. But you always have to follow the classic R logic: assign things to an object and refer to that object at the position you want to insert it.
You can also source code directly inside the server() function. In that case you should source locally using source(..., local = TRUE), so the created objects are contained inside the server function. See also : https://shiny.rstudio.com/articles/scoping.html
Using modules
If you want to take it up a notch and reuse a certain logic and layout (eg a plot option control panel that should be attached to some plots), you should go to modules. (see also http://shiny.rstudio.com/articles/modules.html )
Modules can again be stored in a separate file, and that file sourced in the app.R file you have.
#Joris Meys 's answer covered the topic of splitting shiny code into files. Though one part of the question is to use the run app button, which may not be available even if the organization make a valid shiny app.
If you search this question you can find this issue, then following to the commit made in that issue you can find what's the rule to have a run app button, and this function about isShinyAppDir, this function of shinyfiletype:
Basically any folder have ui.R, server.R, app.R, global.R, www folder will be considered as shiny folder(the detailed conditions are more complex, see source code), then the above 4 files will have run app button.
One thing I noticed is that usually you can keep the app running, make some changes then reload app to see the changes, but if you sourced other file, reload app button will not reload changes in that sourced file.

Web application which runs R

I have written my R script with some functions to do some calculations. On the output of my calculations, I have created plots in shiny(4 tab panels in that as of now, which is using the data specified in my global.R). Now I am trying to automate the process and deliver it to non technical users. So something like, users feed in 2 csv files in web UI and click a button in the web UI, which will then call my r scripts to do the calculations, then my server.R script should build plots on the output of it. Any similar examples? I tried different links, but nothing seems to be relevant.
If I add my R script using source(**.R) and use the functions mentioned in there. Or Do I still have to save output in data folder and specify something in global.R or just use the output of the function in my plot construction?
If I have the fileInput option in first tab in my tabpanels, how can I make stop the user from viewing the other tabs(tabs with graphs) without loading the input csv files and clicking the "Go" button?
Can I do this whole thing in Shiny ? Or better to go for rook or some other framework which calls my r script just for calculations?

R: Scheduling code in a webpage

I'm trying to build a webpage displaying a few graphs. I want to use R to build the graphs, and I want these graphs to automatically update themselves periodically. For example, a webpage showing a graph of the stock price of a particular company over time, which refreshes every day with new data.
what is the best way to approach this? Using Rook to run the R-scripts? can I use it along Markdown, for example, to make the html webpage? Or do you suggest something else?
You can make your plots in a R file and write your webpage in markdown in which you call your R objects and plots. Alternatively you can also run the R code directly in the markdown file. With the knit2html function of the knitr package you can create the html page with the desired output. You can find basic examples on the knitr webpage.
You can schedule these file(s) on your own computer or on a server to update the data of the html output every day. If you have a machine that runs on Windows you can use the Windows Task Manager to run the batch file.
EDIT:
Here you can find a worked out example.

Create script from console input in R

Instead of first creating a script in R and then executing it afterwards, is it possible to do it the other way around?
I.e. I would type some user input in the console, and afterwards have R compile all the user inputs from the console (without the resulting outputs), to a script for later user.
Commands that you type into the console are stored in the history file (see ?savehistory). You can edit that to get your script.

Resources