Splunk Search

How to pass search results to an R function?

SHR
New Member

Hi, I unsuccessfully tried the following approach:

sourcesystem=ABCD earliest=1313131313 latest=1313161616 | r "
source=('modlog.r')
output=modlog(input)
"

with the uploaded script 'modlog.r':

mod_log <- function(a){
p=a$ThreadID+1000
return(p)

}

The field ThreadID is just a number, changing to "p=as.numeric(a$ThreadID)+1000" leads to the same error.

The error I get is basically: command="r", could not find function "modlog".
However, if I upload other functions that don't use "input" as an argument then these are found.

The complete error text: command="r", konnte Funktion "modlog" nicht finden: Traceback (most recent call last): File "D:\Splunk\etc\apps\r\bin\r.py", line 186, in main fieldnames=fieldnames) File "D:\Splunk\etc\apps\r\bin\r.py", line 96, in r scripts.get_custom_scripts_path(), File "D:\Splunk\etc\apps\r\bin\framework.py", line 74, in exeute raise RError('%s' % err) RError: konnte Funktion "modlog" nicht finden

Can anybody see where I made a mistake?

Tags (1)
0 Karma

krwinters11
Path Finder

Along with @rfujara_splunk's answer, you need to edit your output line. It should be this:
output=mod_log(input)
Or like this:
output=data.frame(mod_log(input))

so, all together:
| r "
source('modlog.r') 
output = data.frame(mod_log(input))
"

0 Karma

rfujara_splunk
Splunk Employee
Splunk Employee

I think this is an syntax error. Just try to change the line:

source=('modlog.r')

to this:

source('modlog.r')

Does that work?

0 Karma
Get Updates on the Splunk Community!

Announcing Scheduled Export GA for Dashboard Studio

We're excited to announce the general availability of Scheduled Export for Dashboard Studio. Starting in ...

Extending Observability Content to Splunk Cloud

Watch Now!   In this Extending Observability Content to Splunk Cloud Tech Talk, you'll see how to leverage ...

More Control Over Your Monitoring Costs with Archived Metrics GA in US-AWS!

What if there was a way you could keep all the metrics data you need while saving on storage costs?This is now ...