How do I embed formatted R code in table cells in rmarkdown? - kableextra

I am working on a vignette for an R package where I want to show how to do the same thing in two R packages. My preferred approach would be to include several tables, where I can show the appropriate code next to each other, something like so:
I don't care about the exact fonts, but I would like to see the R code laid out nicely and clearly, like it would be in a regular rmarkdown chunk, but then in a cell of a table.
Is there a way to achieve something like this in rmarkdown?
(I imagine it can by done by typing html directly, but that would be very cumbersome because I don't know html very well.)

If I understand your question correctly, you just want the R code to be able to be displayed in Rmarkdown?
`r ''````{r}
setup <- pacakge2::gogo(x) %>%
dplyr::filter(blabla2)
packages2::plot(setup)
```
This allows you to show your code in an R chunk
If you want to show your entire code you could do this
````markdown
`r ''````{r}
steup <- pacakge2::gogo(x) %>%
dplyr::filter(blabla2)
packages::plot(setup)
```
````
Which actually renders your r code as markdown code

How to do it with my huxtable package. You'll need at least version 5.0.
library(huxtable)
hx <- huxtable(
c("", "Goal A", "Goal B", "Goal C"),
c("Package 1", "", "", ""),
c("Package 2", "", "", "")
)
hx[2, 2] <- "`setup <- package1::start_run()`"
markdown(hx)[2, 2] <- TRUE
quick_pdf(hx)
Result:

Related

Replace multiple substrings with blanks in Pandas

I have a situation where I want to replace part of a string with blanks. For example, my columns looks something like this:
user_comment
it was a good day but nothing in particular happened
nothing specific happening today
no comments. all ok
not much happening really, it will be fine
and the desired outcome I want is:
user_comment_clean
it was a good day but happened
happening today
all ok
it will be fine
Essentially I would like to remove parts of strings as shown above such as "nothing in particular" , nothing specific" , "no comment" and "not much happening really"
and I am using the following code to achieve this:
def remove_no_comments(text):
text = re.sub(r"^nothing in particular", ' ', text)
text = re.sub(r"^nothing specific", ' ', text)
text = re.sub(r"^no comment", ' ', text)
text = re.sub(r"^not much happening really", ' ', text)
text = text.lower()
return text
df['user_comments_clean] = df['user_comments_clean].astype(str).apply(remove_no_comments)
But while using this, it is making my other user inputs as nan and I am really not sure what I am doing wrong here. Any possible solutions to resolve this?
You could use str.replace() along with a regex alternation:
terms = ["nothing in particular", "nothing specific", "no comment", "not much happening really"]
regex = r'^(?:' + r'|'.join(terms) + r')\b\s*'
df["user_comment_clean"] = df["user_comment"].str.replace(regex, '', regex=True)

Extracting Data with .selectitem VBA-SAP

I am trying to extract the text from a topdown analysis in SAP using VBA. I basically need to move down the column, evaluate if the extracted text contains a certain character string, then extract the cell adjacent to it.
First: Does anyone know how to change this line of code (the script for that specific cell) so that I can extract its text to a variable rather than just select it?
Second: Does anyone know how to make this line variable using i as the variable?
session.findById("wnd[0]/usr/cntlCONTAINER/shellcont/shell/shellcont[1]/shell[1]").selectItem " 5", "C 35"
In similar cases, I proceeded as follows:
myRow = right(space(10) & cstr(i) , 11)
myText = session.findById("wnd[0]/usr/cntlCONTAINER/shellcont/shell/shellcont[1]/shell[1]").getItemText(myRow ,"C 35")
Regards,
ScriptMan

VB Macro Like Operator

I am very new to VB macro.In sheet 3 i have three columns named "Country " "Provinces" and "Risk"
For "Provinces", values present are NB,NS,NF,PE. IN sheet5, i have written a code like
`(val) Like "*[HH,HHJ,qqw,www]" Then
to check if user has provided input like NB or like NB, NS or like PE only then corresponding values from Risk will get displayed. Now with the above code , if user is entering value as "," also results is getting displayed as , is present in the Like statement.
Kindly guide me.
You can't use VBA Like that way. Try:
X = "abc, NB"
If X Like "*NB" Or _
X Like "*NS" Or _
X Like "*NF" Or _
X Like "*PE" Then
Debug.Print "X is in the Maritimes"
End If
If you set Option Compare Text at the beginning of your macro, you don't have to Ucase the string being tested.
For complex comparisons, you can use Regular Expressions in VBA. You need to set the appropriate reference.

gWidgets gfile: Directing User to a folder in the browser pop-up

I'm creating a GUI with R and I want to improve the gfile directory selector.
I'm trying to add an argument so that, when the browse window appears, it has preselected a part of the path.
I.e.
when
select.button <- gbutton("Select", container=win, handler = function(h,...) {
selection <- gfile(type = c ("selectdir"))
})
is activated I want the pop-up to show subfolders in a particular folder(frex the desktop).
I think that the filter argument is what I need(as per the manual cited below), but I can't quite make it work.
filter
Filter for files shown during selection. Can be nested list, as in example or a
named character vector with the names a description and value a file extension
(no dots) of files endings to match.
I've also tried initial.dir with the following combinations:
selection <- gfile(type = c ("selectdir"), initial.dir="/home/robert")
selection <- gfile(type = c ("selectdir"), initial.dir="/home/robert/")
selection <- gfile(type = c ("selectdir"), initial.dir="/home/robert/R/")
selection <- gfile(type = c ("selectdir"), initial.dir="home/robert/R/")
selection <- gfile(type = c ("selectdir"), initial.dir=(/home/robert/R/))

Convert long formula into an ARRAYFORMULA

Document: https://docs.google.com/spreadsheets/d/1N4cGw5eUq_3gCJh1w39qVatX9KV1_Hr-AqRHj_nbckA/edit#gid=1770960621
Question
How can I convert the following simple formulas at Schedule!C20:I29 into a single, simple ARRAYFORMULA at Schedule!C20?
=Count(Filter(Students!$B$5:$B, Find(C6, Filter(Students!$J$5:$O,Students!$J$4:$O$4 = 'Current Class'!$B$3))))
.
NOTE:
The above code is only a partial solution. I will substitute the ARRAYFORMULA version of the code into the correct part of the code at Current Class!L6
The C6 reference above can take on any cell between Schedule!C6:I15. I have named that range Timetable_Code. I thought I could do the following, but I was wrong...
=Arrayformula(Count(Filter(Students!$B$5:$B, Find(Timetable_Code, Filter(Students!$J$5:$O,Students!$J$4:$O$4 = 'Current Class'!$B$3)))))
Background
Originally, I created a table that now resides at 1st Version - Current Class!L6. This tab is only for your reference and will be deleted soon. Each cell has a formula with a slight modification. This formula works correctly; however, it is a behemoth and would be hard to modify...
=if(COUNTIF(Meta!$B$5:$B, CONCATENATE("=",if(L$5 = "THURSDAY", "TH", if(L$5 = "SUNDAY", "SU", left(L$5,1))), if(left($K6, 2) = "12", 0, left($K6, 1)))), CONCATENATE(if(L$5 = "THURSDAY", "TH", if(L$5 = "SUNDAY", "SU", left(L$5,1))), if(left($K6, 2) = "12", 0, left($K6, 1)), " ( ", Count(Filter(Students!$B$5:$B, Find(CONCATENATE(if(L$5 = "THURSDAY", "TH", if(L$5 = "SUNDAY", "SU", left(L$5,1))), if(left($K6, 2) = "12", 0, left($K6, 1))), Filter(Students!$J$5:$O,Students!$J$4:$O$4 = $B$3)))), " )") ,"")
.
Pros
I don't have to create any helper data.
All calculations are "in-memory"
Cons
Too large
Hard to modify
I like the output, but I don't like the cons, so I started to create a more edit-friendly version of the code that I am mostly OK with. This code is located at Current Class!L6 (and a secondary copy at Schedule!C33 - it will be deleted.) It has a single formula at Current Class!L6...
=arrayformula(if(COUNTIF(Meta!$B$5:$B, ("=" & Timetable_Code)), (Timetable_Code & " ( " & Timetable_StudentCount & " )") ,""))
.
Pros
Very easy to understand
Very easy to modify
No need to copy formula over to other cells
Cons
Two ( 2 ) helper tables were created ( one of which I think is unneeded)
Again, I like the output, but I really don't like the second helper table (Schedule!C20). I feel like this table can be eliminated, but I have not been able to figure out how.
If you really want to use arrayformula, here it is. For Schedule!C20.
=arrayformula((len(concatenate(index(Students!J5:O, , match('Current Class'!$B$3, Students!J4:O4, 0))))-len(substitute(concatenate(index(Students!J5:O, , match('Current Class'!$B$3, Students!J4:O4, 0))),C6:I15,"")))/len(C6:I15))
Probably you can use filter(as you did before) instead of index & match part, but I prefer index & match and don't want to dig more. Also you can use one help cell to store filter or index & match result to shorten the formula.
The core idea is from counting occurrences of given character in a string, ie len(a1) - len(substitute(a1, .... You can find many documents about it in the net.
Anyway, if I were you, I'd be satified with the current state. Just lock and hide the help tables or sheets. Nobody cares hidden sheets and if something bad happens, you can revert any change.
After getting a good answer from #Sangbok Lee, I decided to break apart each part of the function he gave to me. While doing that I found a highly unlikely connection to some work I did in the Google Sheets last week. A helper column I had in another tab kind of did what Sangbok Lee was trying to do. All I had to do was split that helper column into two columns (1 for the previous final calculation, 1 for) and calculate an additional count column
After reworking both of our formulas, and testing the result, I found a solution that I am even more satisfied with!
=arrayformula(if(countif(Meta!$B$5:$B, (Timetable_Code)), (Timetable_Code & " ( " & vlookup(Timetable_Code, StudentCount_Lookup, 2, false) & " )") ,""))
.
Check out the differences in the Google Sheet
Look at 1st Version - Current Class!L6 tab for the 1st version
Look at Current Class!L6 for the 2nd version
Look at Current Class!U6 for the 3rd and final version
Also look at tab Meta and Schedule for the differences.
Note: Green is old data, Red is new data

Resources