I have a weird situation - and I apologize in advance, but this issue doesn't lend itself to a reproducible example. Here is the situation
Here is the skeleton of the problem
Chunk 1
Chunk 2
...
Chunk N-1
Then I have a chunk which is basically of the form:
{r chunkname, results = 'asis'}
f <- function(x){
y <- some computation on x
return(y)
}
T1 <- f(A)
T2 <- f(B)
According to the structure, nothing should get displayed, but when I knit the Rmarkdown file, both T1 and T2 appear. This is true even if I use results = 'hide'.
Now the weirder part. If I put the offending chunk in an Rmarkdown file by itself, it behaves as it should, and does not display T1 and T2.
The preceding chunks (Chunk1 - ChunkN-1) all run exactly as expected - no weird behaviors.
Last clue - not sure if it matters. The function in the offending chunk involves generating a table with gt.
I'm sorry - I realize there isn't a lot to go on here, but this is a chunk in the middle of several thousand lines of code - all of which run fine. And, when the offending code is on its own, it behaves.
I believe I have found an aswer to this problem. I left something seemingly innocuous out of my post.
The original code looked basically like:
{r chunkname, results = 'asis'}
f <- function(x){
y <- some computation on x
return(y)
}
T1 <- f(A)
T2 <- f(B)
save(T1)
save(T2)
Note that in my original post, I failed to include the "save(T1)" part. Well, it turned out that was the problem. If, instead of save(T1) I substitute temp <- save(T1), the issue disappears.
I don't understand why a save was causing something to be displayed. I also don't really understand what "assigning" the save action to the variable temp even means. But - it suppresses the output of the results to the kintted file.
Related
I found the Splice functionality in Mathematica quite useful in the past. I am trying to insert mathematica expressions, formatted for Fortran, into Fotran code.
Does anyone have a small working example they would be willing to share? Thanks.
I could write Mathematica code
y=x^3
and construct a file test.fm with
program test
real x,y
x=1.0
y=
- <* y *>
write(6,*) "y",y
end
and the mathematica line
Splice["test.fm"]
would give a file test.f with
program test
real x,y
x=1.0
y=
- x**3
write(6,*) "y",y
end
Apparently this use of Splice is removed in recent Mathematica releases, and I get an error message
The function Splice with filename inputs is now obsolete and has been
superseded by FileTemplate.
I tried
FileTemplate["test.mf"]
but it returns something that apparently needs further output. I then tried
TemplateApply[FileTemplate["my.fm"]]
but this didn't work either.
FileTemplate yields a TemplateObject expression that you need to apply. Here is an example application using your code and StringTemplate, which similarly yields a TemplateObject expression.
Clear[x]
y = x^3
StringTemplate[" program test
real x,y
x=1.0
y=
- <* y *>
write(6,*) \"y\",y
end", InsertionFunction -> ToString#*FortranForm][<|"y" -> y|>]
I've been working on a pretty simple problem on HackerRank for a few days, but I'm stuck with timeout problems and I can't optimize my code any further.
The problem is this:
Given a 2D array of digits (dimensions R * C), try to find the occurrence of a given 2D pattern of digits (dimensions r * c).
Here you are a reproducible example of variables:
pattern <- c("11111", "11111", "11110")
text <- c("111111111111111",
"111111111111111",
"111111011111111",
"111111111111111",
"111111111111111")
R <- 5
C <- 15
r <- 3
c <- 5
It's sort of a regex problem, but in 2D, and this is something that I couldn't find anywhere as a ready-to-use function in R.
There are a few corner cases that I've been able to cope with, trying to avoid the brute force option (the above version is one of those cases, where the usual 'regexp' miserably fails to find the pattern).
Below it's my code: it works perfectly fine for 13 out of 15 cases, but it fails for reason of timeout when it goes against some tests with (e.g.) R*C = 500*500 and r*c = 236*208.
RW <- c()
pattern2 <- paste0(pattern, collapse = "")
RW <- c(rep(NA,(C-c+1)*(R-r+1)))
for (v in 1:(C-c+1))
{
for (y in 1:(R-r+1))
{
RW[(C-c+1)*(y-1)+v] <- paste0(substr(text[y:(y+r-1)],v,c+v-1),collapse="")
}
}
per <- ifelse(pattern %in% RW, result <- "YES",result <- "NO")
cat(result, "\n")
Please note that there are up to 5 cases for each test, and this is the reason why my code fails: while it could work breaking the test in 5 parts, it passes the time threshold when the cases are combined together with big RC and rc dimensions.
Does anybody have an idea on how to improve the code performance?
If you want to keep your approach my first suggestion would be to convert the strings to numeric matrices because substr is probably not very fast.
You can further make use of more sophisticated matching algorithms which shift the position for more than one place like the Knuth-Morris-Pratt Algorithm.
However, for loops will always be quite slow in R, so I feel that the best approach in this situation would indeed be a regular expression. If you concatenate the rows of the big grid into one long string the number of characters between the rows of the pattern is fixed. This means you can do something like this (which, I believe, solves the test case you gave):
grepl(
paste0(pattern[1], ".{", C - c, ",}",
pattern[2], ".{", C - c, "}",
pattern[3]),
paste0(text, collapse = "")
)
https://en.wikipedia.org/wiki/Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm
I am currently working on some fortran code and getting an error of.
`Duplicate statement label 201 at (1) and (2)`
My code is long but simplified looks like so:
integer :: y,x,method
x = 0
print*, "Please enter a value (1 or 2)."
read(*,*) method
if (method .EQ. 1) then
x = 10
do i=1,1000
y = x * i
Call OtherFile(y,x,i)
write(6,201) long,list,of,variables
201 format('OUTPUT: ',i5,1p,7d10.2)
enddo
stop
else if (method .EQ. 2) then
x = 10
do i=1,1000
y = x * i * i
Call OtherFile(y,x,i)
write(6,201) long,list,of,variables,
201 format('OUTPUT: ',i5,1p,7d10.2)
enddo
stop
end if
Edit: After posting earlier I realised this code would work. I cannot post the original work so I have best tried to recreate the problem.
Edit2: I have made the mistake in labelling this as Fortran77, it is infact an older version as it is written in punchcard format.
The lines in question are the ones beginning with 201.
I can resolve the issue simply by chosing 201 and 202 before each FORMAT section respectively, however there are other files reliant upon the argument 201 as statement number.
Can anyone tell if there is a solution to this?
I think this warrants a full explanation which does not fit in a comment. The FORMAT is not an executable statement. It just instructs the compiler in which format print or read DATA. There can always only be one FORMAT statement with the same label in a given scope. It does not matter on which line that exactly is.
In particular, when executing the write, print or read statement, the program does not in any way jump to the FORMAT statement. The compiler just uses the format description defined there.
Therefore:
write(6,201) something
201 format('OUTPUT: ',i5,1p,7d10.2)
or
201 format('OUTPUT: ',i5,1p,7d10.2)
write(*,201) something
or
201 format('OUTPUT: ',i5,1p,7d10.2)
!many lines here
write(*,201) something
or
write(*,201) something
!many lines here
201 format('OUTPUT: ',i5,1p,7d10.2)
or
write(*,"('OUTPUT: ',i5,1p,7d10.2)") something
or
write(*,"(i5,1p,7d10.2)") 'OUTPUT: ', something
will all do the same thing and all will work just fine.
The last way with a character constant (or variable) inside the write statement instead of the FORMAT statement label is the modern way to go.
format statements are not executable statements. Except as noted below their location in the code isn't important. So, while it may be tempting to put a format together with the output statement
write(6,201) long,list,of,variables
201 format('OUTPUT: ',i5,1p,7d10.2)
that isn't necessary. When one wants to duplicate that output a little later, simply copying the two lines doesn't help: the error message shown can pop up.
As detail: write(unit=6,fmt=201) (keywords added for clarity) is asking to write to unit 6 using the format of the format statement labelled 201 which occurs in the same inclusive scope of that write. A line later 201 format (...) is providing such a thing.
The error is that no two statements in the same scope may have the same label. Deleting one of them, or changing the label (and its reference) would be suitable.
Vladimir F's answer goes into more detail about other approaches to address the format re-use.
I have a very long script R that plots very complicated data. I only use the plots to have a visual idea of what I am doing but I can compute the results without the plots and obviously not plotting anything makes things much faster. Occasionally, however, I still need to visualize what the program does to keep debugging it.
To achieve this plotting 'on or off' switch I am following this strategy.
For each line that has commands relevant to the plotting functions of the script, I have a specific commented tag #toplot at the end of each relevant line. Using the power of regex substitution I then comment / uncomment these lines with the following commands.
The sample code:
a <- c(1:10)
b <- a/sin(a)
png('sin.png') #toplot
plot(b) #toplot
dev.off() #toplot
print(b)
To comment the 'tagged' lines:
:%s/.\+#toplot/###commline###\0/g
I get this:
a <- c(1:10)
b <- a/sin(a)
###commline### png('sin.png') #toplot
###commline### plot(b) #toplot
###commline### dev.off() #toplot
print(b)
To uncomment them:
:%s/###commline###//g
I get this:
a <- c(1:10)
b <- a/sin(a)
png('sin.png') #toplot
plot(b) #toplot
dev.off() #toplot
print(b)
I am no computer scientist so I don't know if there is a better, more elegant way of performing these kind of operations.
EDIT: It is important to mention that for plotting my data I need to go through many rounds of calculations and transformations so the different kinds of data fit in the plotting device. To perform these operations I use the history, I go up and down depending what I need.
Your approach looks fine to me.
If you can come up with a regular expression that captures all plot-related lines, you could do away with the #toplot marker, and let the comment substitution directly work on that instead.
You didn't mention whether you re-type the substitutions or use the history. I would definitely define a buffer-local command (and/or mapping) for that:
autocmd FileType r command! -buffer Comment %s/.\+#toplot/###commline###\0/g
autocmd FileType r command! -buffer Uncomment %s/###commline###//g
(Or put the :commands! into ~/.vim/ftplugin/r_commands.vim.)
If you properly define the 'comments' setting for your filetype (e.g. add b:###commline###) and 'commentstring', you may also be able to use one of the general comment plugins (like The NERD Commenter), which offer nice mappings to toggle a comment on/off.
This is ok, but isn't it easier to wrap each plotting command with a condition?
I am converting SAS code to C# code and I don't have platform to run SAS code, what I have only source code, and I have line of the code in SAS where one of the variables is empty and another is not and I would like to know what would be result of y in following operation.
x = .
y = 5 - x
x is empty (null)
Please advice.
Thanks a lot in advance for your help.
I believe the result of y will be . (empty)
When you perform an operation with an empty(null) variable the result is null.