callr | Call R from R | Reactive Programming library
kandi X-RAY | callr Summary
kandi X-RAY | callr Summary
Call R from R. It is sometimes useful to perform a computation in a separate R process, without affecting the current R process at all. This packages does exactly that.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of callr
callr Key Features
callr Examples and Code Snippets
Community Discussions
Trending Discussions on callr
QUESTION
I wrote a script that is supposed to run a background process if a button is pressed. After the process is finished, I would like to work further with the results.
Below is my script. The process in this script is 10 seconds sleeping followed by retrieving a list of the names of the files in the current folder. The background process is found in the function printLS(). This is performed in the background by the function r_bg().
...ANSWER
Answered 2022-Mar-16 at 10:18As explained in the examples here, wait()
means that we have to wait for the result of the background job before continuing the rest of the processes.
One way to keep the clock updating while running the background job is to use poll_io()
to check whether the job is finished (note that it is better to use poll_io()
than is_alive()
, as explained in this Github comment). I have done something similar in this question, although the general app is a bit more complicated.
Here's what you need to modify in the server:
QUESTION
This question is in the continuity of this one: Is it possible to stop executing of R code inside shiny (without stopping the shiny process)?.
The plot that I display in my app takes some time to produce, and I want the users to be able to stop its creation (for instance if they made a mistake in the options). I found this blog post about using callr
in Shiny. The workflow is the following:
- create an empty list of jobs/plots
- clicking on "start" creates a background process to create the plot
- if the user doesn't do anything, the plot is computed in the background. I use
invalidateLater()
every second to check if the background process is finished. If it is, then I display the plot. - if the user clicks on "stop" before the end of the process, the process is killed, removed from the list, and the previous plot is displayed (if there was no plot produced before, nothing is displayed)
- if the user doesn't do anything, the plot is computed in the background. I use
First, I'm not sure how this would scale when several people use the app at the same time. Since every background process is independent, I don't think one user would be blocking the others, but I may be wrong.
Second, I'd like to show a waiting indicator on the plot. So far, I used the package waiter
to do that, but the problem here is that renderPlot()
is being invalidated every second to check if the background process is finished. Therefore, waiter
appears and disappears repeatedly as the output is being invalidated.
Below is an example app that mimics the behavior I'd like to have:
...ANSWER
Answered 2022-Feb-15 at 09:06Regarding your first concern: this approach won't block other sessions. However, the polling via invalidateLater()
will create some load.
A great library to look at in this context is ipc and its introductory vignette.
Regarding the second issue: There is a simple fix for this behaviour. We can use req
and its cancelOutput
parameter - see ?req
:
cancelOutput: If TRUE and an output is being evaluated, stop processing as usual but instead of clearing the output, leave it in whatever state it happens to be in.
QUESTION
The scenario I'm emulating with the below minimal example is allowing a user to engage with a Shiny App (click the numericInput
control and see server-side events occur) while a long-running download is occurring (simulated with Sys.sleep(10)
within downloadHandler
).
In a synchronous setting, when the "Download" button is clicked, the user can still interact with UI elements, but other Shiny calculations (in this case, renderText
), get put in a queue. I'd like the asynchronous setting, where the download occurs in the background, and users can still interact with the UI elements and get desired output (e.g. renderText
).
I'm using callr::r_bg()
to achieve asynchronicity within Shiny, but the issue is that my current code of the downloadHandler is incorrect (mtcars
should be getting downloaded, but the code is unable to complete the download, 404 error message), I believe it's due to the specific way in which downloadHandler
expects the content()
function to be written, and the way I've written callr::r_bg()
is not playing nicely with that. Any insights would be appreciated!
Reference:
https://www.r-bloggers.com/2020/04/asynchronous-background-execution-in-shiny-using-callr/
Minimal Example:
...ANSWER
Answered 2021-Nov-04 at 14:25I figured out a solution, and learned the following things:
- Because downloadHandler doesn't have a traditional
input$X
, it can be difficult to include reactivity in the traditional way. The workaround was to present the UI as a hiddendownlodButton
masked by anactionButton
which the user would see. Reactivity was facilitated in the following process: user clicks actionButton -> reactive updates -> when the reactive finishes (reactive()$is_alive() == FALSE
), useshinyjs::click
to initiate thedownloadHandler
- Instead of placing the
callr
function within the downloadHandler, I kept the file within the content arg. There seems to be some difficulties with scoping because the file needs to be available within thecontent
function environment - I'm using a reactive function to track when the background job (the long-running computation) is finished to initiate the download using the syntax:
reactive()$is_alive()
- The
invalidateLater()
and toggling of a global variable (download_once
) is important to prevent the reactive from constantly activating. Without it, what will happen is your browser will continually download files ad infinitum -- this behavior is scary and will appear virus-like to your Shiny app users! - Note that setting global variables is not a best practice for Shiny apps (will think of a better implementation)
Code Solution:
QUESTION
BLUF: I am struggling to understand out how to use batching in the R targets package to improve performance in a static and dynamic branching pipeline processed in parallel using tar_make_future()
. I presume that I need to batch within each dynamic branch but I am unsure how to go about doing that.
Here's a reprex that uses dynamic branching nested inside static branching, similar to what my actual pipeline is doing. It first branches statically for each value in all_types
, and then dynamically branches within each category. This code produces 1,000 branches and 1,010 targets total. In the actual workflow I obviously don't use replicate
, and the dynamic branches vary in number depending on the type
value.
ANSWER
Answered 2021-Dec-10 at 22:07You are on the right track with batching. In your case, that is a matter of breaking up your list of 100 datasets into groups of, say, 10 or so. You could do this with a nested list of datasets, but that's a lot of work. Luckily, there is an easier way.
Your question is actually really well-timed. I just wrote some new target factories in tarchetypes
that could help. To access them, you will need the development version of tarchetypes
from GitHub:
QUESTION
I'm trying to set up a workflow that involves downloading a zip file, extracting its contents, and applying a function to each one of its files.
There are a few issues I'm running into:
How do I set up an empty file system reproducibly? Namely, I'm hoping to be able to create a system of empty directories to which files will later be downloaded to. Ideally, I'd like to do something like
tar_target(my_dir, fs::dir_create("data"), format = "file")
, but I know from the documentation that empty directories are not able to be used with format = "file". I know I could just do adir_create
at every instance which I need it, but this seems clumsy.In the reprex below I'd like to operate individually on each file using
pattern = map(x)
. As the error suggests, I'd need to specify a pattern for the parent target, sinceformat = "file"
. You can see that if I did specify a pattern for the parent target, I would again need to do it for its parent target. As far as I know, a pattern cannot be set for a target that has no parents (but I have been wrong many times before).
I have a feeling I'm going about this all wrong - thank you for your time.
...ANSWER
Answered 2021-Dec-09 at 17:56Here's an idea: you could track that URL with format = "url"
and then make the URL a dependency of all the file branches. Below, all of files
should rerun then the upstream online data changes. That's fine because all that does is just re-hash stuff. But then not all branches of stuff_done
should run if only some of those files actually changed.
On second thought, we probably need to hash the local files all in bulk. Not the most efficient, but it gets the job done. targets
wants you to use its own built-in storage system instead of external files, so if you can read the data in and return it in a non-file format, dynamic branching will be easier.
QUESTION
Since a few months, ggplot2 started to save png files with a transparent background. The code output in Rstudio and when saved as pdf looks great. It happens mainly with the use of themes when I omit the gray panel background. I tested it on my macbook with "Preview" and on a Windows Computer with the "foto viewer" there.
...ANSWER
Answered 2021-Nov-11 at 14:11Maybe indeed worth an answer for posterity...
Specify ggsave("test.png", dpi = 300, bg = "white")
Background (pun intended): the argument will be passed to grDevices::png
via the ...
argument. bg
controls the background of the device.
QUESTION
I was wondering what happens when callr_function = NULL? Is it just issues with things maybe being in the environment/side effects?
Mainly wondering because I was passing quite large spatio-temporal arrays (0.5 to 5 gigs) and callr serialization via saveRDS is quite slow.
The two things I was thinking about was forking callr and dropping in a different save function or just using callr_function = NULL.
...ANSWER
Answered 2021-Oct-13 at 17:54Ordinarily, targets
runs the pipeline in a fresh new reproducible external R session. callr_function = NULL
just says to run the pipeline in the current R session. I only recommend this for debugging because in serious use cases you could accidentally invalidate some targets based on changed data in your global environment. callr_function = NULL
will probably not help solve issues with large memory. For that, I recommend selecting a more efficient storage format for your data, e.g. tar_target(..., format = "feather"). You could also try tar_option_set(memory = "transient", garbage_collection = TRUE) for better memory efficiency.
QUESTION
I’m creating a Shiny app that uses the caret package to do some SVM free-text analysis.
The app runs fine without any error in my computer. I’m using R x64 4.0.4 and R studio 1.3.1093
I’m deploying app to an internal enterprise server https://rconnect.xxxx.com/connect/#/apps/####
This app is deployed in the server and started.
But when I reach the line where I run the train function:
ANSWER
Answered 2021-Aug-05 at 01:15Errors like this in Shiny apps are almost always a result of missing packages, which the logs confirm.
Turns out in this case I think the missing package is kernlab
, which I only found by reading the documentation given here: https://topepo.github.io/caret/train-models-by-tag.html#Support_Vector_Machines. It's a suggested package, not imported, so the command suggested in the comments by heds1 would sort this out.
QUESTION
The example below is a simple one which tries to assert the column y is always positive (y>0). How can I extract the errored data (row 3 with the negative value,into a dataframe maybe, or any convenient object) while allowing the workflow to continue with "cleaned" data?
...ANSWER
Answered 2021-Apr-12 at 09:23This is tricky, and the answer below doesn't solve this 100%. Now there are a number of different ways assertr lets you handle errors/stops, just see ?error_stop (which is the default).
You need to not only filter out rows that fail, but also collect them (all) for later inspection.
Below I wrote my own error handler. It fetches those rows that fail, filter them away, and stores them in the global environment under the varibale my.failed.rows
.
QUESTION
I ran into a missing graph issue while developing histograms and geometry bar plots in r using the ggplot function. The issue is intermittent and occurs across multiple data sets. One data set, the largest, is described here.
My initial code, used to generate randomized data, is this:
...ANSWER
Answered 2021-Mar-23 at 18:43The issue is that you are using scale_y_binned()
. The rest of the code works fine for me, except when I add this particular line to the plot. The "binning" is working (your y axis has %'s), but you see no geoms because ultimately, ggplot2
is plotting the histogram using the same geom used for geom_bar
/geom_col
. This geom requires a continuous y axis, and scale_y_binned()
is designed to "bin" or "discretize" the y axis. So... the bars are being plotting on both charts, but once binned, ggplot2
has no idea how to draw your geom.
As for why you're seeing the inconsistency... not sure. Sometimes there is a time component to executing the code. When I run your code, it consistently gives me the second chart (no bars).
To fix, you need to use scale_y_continuous()
and set the labels.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install callr
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page