## Highlighting bash code with knitr / rmarkdown

Issue I’m trying to generate an HTML report using RStudio, R Markdown and knitr. In the report I would like to display some bash code. I do not want to run the code but I would like it to be

## Differences in R Markdown syntax highlighting for in-line code

Issue The following minimal R Markdown file minimal.Rmd produces different minimal.html output (via “Knit HTML” in RStudio) depending on my system. The first is the result on my Windows 8 machine. The second is the result on my Windows 7

## syntax highlighting while rendering SQL using knitr

Issue knitr will execute SQL code using engine=’mysql’ but not highlight syntax: \documentclass{article} \begin{document} <<engine=’mysql’, engine.opts=”-h localhost”>>= SHOW DATABASES; @ \end{document} On the other hand using engine=’highlight’ will highlight the code but not execute it. \documentclass{article} \begin{document} <<engine=’highlight’, engine.opts=’-S sql

## Is rouge syntax highlighting meant to highlight non-base functions in R?

Issue I’m trying to understand the rouge syntax highlighter and in particular use it to highlight R code. Ultimately it’s for a website built with jekyll but I’ve been able to isolate my problems to just rouge (eg I use

## knitr syntax highlighting in .Rnw files (LaTeX)

Issue I am using .Rnw files to produce a report with LaTeX and knitr. My knitr_setup looks as follows: <<knitr_setup, echo=FALSE>>= library(knitr) opts_chunk$set(highlight = TRUE, cache = TRUE, eval = FALSE, size = “small”) options(width = 60) opts_knit$set(out.format = “latex”)

## Syntax highlighting rules and definitions

Issue Are there any standard syntax highlighting rules for R? If yes, where can they be found? Or does each editor, IDE, package (e.g., the highlight package) create their own rule set? Solution Each editor creates their own rule set.

## Highlight R Code Syntax in WordPress

Issue I would like to highlight R syntax in a wordpress blog, so that it looks similar as in RStudio. I found the wp-synthax-plugin, which is able to highlight R code as follows: However, the colors are in my opinion

## Sweave syntax highlighting in output – revisit

Issue My question is the same as the one in the link Sweave syntax highlighting in output. I have followed the suggestion by daroczig https://tex.stackexchange.com/questions/5113/how-to-colorize-syntax-using-r-sweave, and created the following test.Rnw file: \documentclass{article} \RequirePackage{fancyvrb} \RequirePackage{listings} \SweaveOpts{keep.source=TRUE} <<SweaveListingsPreparations, results=tex, echo=FALSE, strip.white=false>>= require(SweaveListingUtils)

## How to highlight all R function names with highlight.js?

Issue I want to extend highlight.js capabilities for R language so that (1) all function names that are followed by opening parenthesis ( and (2) all package names that are followed by :: and ::: operators would be highlighted (as

## Custom highlighting style in rmarkdown

Issue Is there a way to use a custom highlighting style in rmarkdown? Manual is a bit silent regarding that and the closest to that is to make a full blown custom css file for everything, that would however work

## How to use variable R code in a Knitr chunk?

Issue I want to demonstrate use of a piece of R code. But I want the code to be variable itself. Example two tasks: Randomly select two variables from a data frame and add those columns. Randomly select a set

## How to syntax highlight inline R code in R Markdown?

Issue This question is similar to consistent code html inline and in chunks with knitr. Instead of .Rhtml documents, I want to highlight inline R code in R Markdown documents, e.g., after r “plot(cars, main = ‘A scatterplot.’)” is compiled

## What does it mean when the variable name T is highlighted?

Issue I understand that R highlights specific text depending on its function, but what does it mean when the variable you are assigning is highlighted? And can it cause any problems? For example, T is highlighted automatically in: Solution It

## Print Script with syntax highlighting R-Studio

Issue After finishing statistical analysis in R I always like to print out the script since it gives a good overview and one can adjust eventual errors. I like the syntax highlighting in R-Studio because it facilitates reading and fast

## Stata syntax highlighting in Rmarkdown

Issue I am using Bookdown and Rmarkdown to compose a technical book for Stata users. I would like to include Stata highlighting syntax in the chunks of code, but I am not interested in actually executing Stata from Rstudio. I

## Override bad/wrong values in a main table with NA or null values listed on another lookup table in R

Issue The main table is large. Has certain undesired values that I want to override. I am writing into a lookup table the keys and new_value (NA) to override. Both have 2 keys (session_id and datetime), not one unique. Other

## Split string of concatenated digits into a numeric vector of individual values

Issue Suppose I have a long string of concatenated digits, 0 and 1: "1010101010101010100011011" I would like to split the individual values and separate them by a , like so "1,0,1,0,1,0,1,1,…". Then transform into a numeric vector like this: c(1,0,1,0,1,…)

## R Shiny unable to start png device after running a few hours

Issue I have had various R Shiny applications running successfully (some for over a year) on a Windows PC serving a local intranet network. In the last couple of weeks, the applications started to fail loading plots with the error:

## How to get the difference of 2 images to visually compare complex-data visualizations by substracting ggplot's images

Issue I am working with two giant datasets taken under similar circumstances and produces very similar, but non-equal results. I need to visually highlight differences between both. I previously built a heatmap helper function that is able to draw each

## How do I permanently change a vector in R?

Issue I am trying to simulate a tournament where, in each round, two randomly selected players out of thirteen play, and their Elo rating (a measure of their relative skill level) gets updated. Each player has a true underlying skill

## How to use tryCatch without global variables or superassignment

Issue I am writing an R package with a tryCatch() loop of the following form, in which I first try to fit a model using an error-prone method but then use a safer method if the first fails: # this

## Strictly speaking does the scoping assignment <<- assign to the parent environment or global environment?

Issue Often the parent environment is the global environment. But occasionally it isn’t. For example in functions within functions, or in an error function in tryCatch(). Strictly speaking, does <<- assign to the global environment, or simply to the parent

## Redundant variable naming issue with purrr::map and dplyr

Issue I would guess that this is a basic issue that isn’t specific to purrr at all, but it caught me off guard in this context. The general answer would be great if this isn’t about how purrr and dplyr

## Why does my R run in dynamic scoping? Shouldn't it be lexical?

Issue I just learned in class that R uses lexical scoping, and tested it out in R Studio on my computer and I got results that fit dynamic scoping, not lexical? Isn’t that not supposed to happen in R? I

## Saving R objects to global environment from inside a nested function called by a parent function using mcmapply

Issue I am trying to write an R-script that uses nested functions to save multiple data.frames (parallelly) to global environment. The below sample code works fine in Windows. But when I moved the same code to a Linux server, the

## Scoping and evaluating functions in R

Issue Given the following function f <- function(x) { g <- function(y) { y + z } z <- 4 x + g(x) } If one runs the following code in R why is the answer 10? I’m a little

## How to have helper function inherit variables from main function

Issue I would like to set up a function bar() which relies on a helper function foo(). I am struggling to figure out the cleanest way to setup the variable definitions so I don’t have to repeat the definitions in

## Emulate dynamic scoping in R to filter by arbitrary functions

Issue I have a number of files in a directory, and I want to distribute them between two newly created subdirectories based on whether their content satisfies a certain condition. Moreover, I need to perform this operation on multiple directories,

## Environment/scoping in furrr: nesting get() in future_map()

Issue I’m interested in learning more about how furrr finds stuff from the global environment, and asked generally about the black magic it performs. Here’s a specific example of a behavior I didn’t understand and could use some help with:

## Wrapping working code in a function and it stops working

Issue I have some code that works quite well, and I have it repeated in the same format in my code several times, so I’ve tried to wrap it in a generalised function and as soon as I do that,

## Passing arguments to a function via list and ignoring unused arguments

Issue Background I’m interested in using do.call to pass arguments to two functions with use of one list. The do.call solution should ignore unused arguments. Example my_sum <- function(a, b) { a + b } my_exp <- function(c, d) {

## Is it a good practice to call functions in a package via ::

Issue I’m writing some R functions that employ some useful functions in other packages like stringr and base64enc. Is it good not to call library(…) or require(…) to load these packages first but to use :: to directly refer to

Issue I’m drafting a simple Shiny App that provides access to a dynamic chart and a corresponding table. The relevant part of the server.R code looks like that: output$some_plot<- renderPlot({ # Subset data on change in the indicator selection chrt_demo_dta Continue reading ## Correct approach to caching a computationally expensive object generated within function Issue Background In the following scenario, I’m looking at the following workflow: Top-level runner function, in the example below running_function, calls a number of smaller functions. Some of those functions are computationally expansive, and will be called on the same Continue reading ## solving scoping issure in a custom function to extract data from htest object Issue I am struggling with some scoping issues where I am trying to extract dataframe. Here is a portion of a larger custom function to extract data from McNemar’s test and other htest objects: get_data <- function(x, …) { data_name Continue reading ## Force R function call to be self-sufficient Issue I’m looking for a way to call a function that is not influenced by other objects in .GlobalEnv. Take a look at the two functions below: y = 3 f1 = function(x) x+y f2 = function(x) { library(dplyr) x Continue reading ## R: Scoping error when calling function from script Issue I am building a large codebase in R that uses vectorization through many nested user-defined functions. These auxiliary functions are, by in large, written in their own R script for debugging and maintenance purposes. I would like to source Continue reading ## control scoping of arguments supplied to lm() from whithin a function called by lapply Issue I have a function that takes a dataset, extracts different variables, and then makes linear models from those variables (it expects the response in the last column). I want the data argument of the calls for these models to Continue reading ## Determining which version of a function is active when many packages are loaded Issue If I have multiple packages loaded that define functions of the same name, is there an easy way to determine which version of the function is currently the active one? Like, lets say I have base R, the tidyverse, Continue reading ## How to correctly scope a dataframe in R to use with a formula in a function Issue After some digging (1, 2, 3), there appears to be a few posts about using formulas within functions causing scoping issues if I am understanding them correctly. Some suggests to use an environment, assign, or <<- to get around Continue reading ## R Shiny: Change tabs from within a module Issue I have a shiny app with multiple tabs, and I would like to have action buttons within the tabs that allow the user to switch tabs. I found the following pages: https://www.titanwolf.org/Network/q/e6b187b8-6cad-4ece-ad16-7ec73ed2f758/y How to switch between shiny tab panels Continue reading ## How to use the R environment and the globalenv() function Issue I’m currently reading Hands-On Programming With R and the author first suggests this code to deal cards from the top of a deck: deal <- function(deck) { deck[1,]} but that code has an obvious problem: you’ll always get the Continue reading ## R scoping question: Object exists but I can't do anything with it Issue I’d like to feed a value from one function into another that I’m calling on inside the first, and I can’t seem to get the scoping right. Crucially, the two functions are defined separately. Here’s an example: little_fun <- Continue reading ## Data objects not found in embedded vegan:cca. Scoping issue? Issue I try to embed cca (and also capscale) from package vegan (version 2.5-7, R 4.1.2) in another function to test an analysis pipeline with some data transformation and then varying model formulas. The used data matrices (e.g. bio and Continue reading ## How do you use "<<-" (scoping assignment) in R? Issue I just finished reading about scoping in the R intro, and am very curious about the <<- assignment. The manual showed one (very interesting) example for <<-, which I feel I understood. What I am still missing is the Continue reading ## Split data into training and test set: How to make sure all factors are included in training set? Issue I have a data frame called b. I split this into a training set and test set. smp_size <- floor(0.75 * nrow(b)) set.seed(123) train_ind <- sample(seq_len(nrow(b)), size = smp_size) b_train <- b[train_ind, ] b_test <- b[-train_ind, ] b contains Continue reading ## R: Use model$xlevels to subset data to get to training data

Issue Consider mtcars example: model <- fit3 <- lm(mpg ~ factor(am)+wt+qsec+hp+drat, data = mtcars) When you do model\$xlevels we get the levels of the variable that was used for modeling. If I want to use these level details to subset