I want to dyanamically change my URL at runtime for which I did string manipulations in R and it worked with this script:
x <- 'https://news.google.com/search?q='
var <- 'NREGA'
z <- '&hl=en-IN&gl=IN&ceid=IN%3Aen'
url <- paste0(x, var,z, collapse = '')
url
Now I want to change this variable var dynamically and the value for var needs to be retrieved from a knime node which can then be used in url. In my case it's the table creator node in knime to get this value but I'm not able to assign it to var.
Kindly suggest any knime node by which the value for 'var' can be obtained and then be used in 'R snippet' node in knime, and can be used in manipulating the url.
The first line of the code in your image is
knime.in <- knime.flow.in[["NREGA"]]
The expression knime.flow.in[["NREGA"]] in an R script node gets you the value of the flow variable named NREGA.
You probably don't want to assign that value to knime.in as that's the R dataframe containing the table from the input port of the KNIME node. Either you want to use that table - in which case don't overwrite it - or you don't, in which case just use the R Source (Table) node which has no input port.
Later on in your code you have the line
var <- knime.flow.in[["NREGA"]]
which should assign the flow variable value to the R variable var. If that isn't working the way you want, please edit your question to better describe what the problem is.
Related
I have a loop which is running over a vector containing names of a few tens of dataframes that are in my environment. On each iteration, I want to access the dataframe using the name, and access a specific column within it. As I understand it, the best way to access a variable with a string is using get().
But when I try and do this (with name being a variable containing the string "first.name"):
get(column.name, name)
I get the error:
Error in as.environment(pos) : no item called "first.name" on the search list
It does work if I try to run:
get(column.name, first.name)
So, assuming that get() is the right function for this, what am I doing wrong?
Reproduceable example:
my.df <- as.data.frame(x = seq(1:10), y = rnorm(10))
name <- "my.df"
get("x",name)
We may need to use
get(name)[[column.name]]
I am trying to set values with rvest for dynamic field names. So I retrieve the field name with the code below. f.account is a character of length one, containing a dynamic field name like OCmaCMFgHij
library(rvest)
url <- "https://sv01.net-housting.de/user/index.php"
sp.session <- html_session(url)
f0 <- html_form(sp.session)
f.account <- f0[[2]][['fields']][[1]]$name
Note: This is a little arbitrary example since f.account is not really dynamic in this case, but will always have the value username.
Now I want to set a fixed value to the field in the form with smth like this:
f1 <- set_values(f0, UQ(rlang::sym("f.account")) = "MyAccountName" ))
I tried to use the .dot statement or setNames as described here, but I have failed so far.
I want to, essentially, pass a value untouched through a function. So in the following example (in Rstudio):
example_function <- function(datain){
as.environment("package:utils")$View(datain)
}
I want the inner function to act as if I'm passing it the original object, in particular so the name which appears in the View window will have the name of the original object (X, say) rather than datain which is what currently occurs.
With deparse(substitute(datain)) you can get the original name of the argument passed.
Then, to accomplish what you asked for, you can simply do
example_function <- function(datain){
as.environment("package:utils")$View(datain, deparse(substitute(datain)))
}
Now the View window will be titled appropriately as you wanted.
However note that "I want the inner function to act as if I'm passing it the original object" request of yours is not possible in R. R does not support pass-by-reference. There are some workarounds, but if you only needed if for naming the View, the above fix should be fine.
You can also use get for this.
example_function <- function(datain){
as.environment("package:utils")$View(get(datain),datain)
}
in this case you don't pass the variable but rather the name of the variable as a string.
example_function("X")
I think this won't be very complex, but I'm unable to figure this out.
I have in Matlab a 17x1 struct object with 6 fields, named photolist. I only want to export the name field , to use in R.
photolist.name gives me the list I need, but when I want to store it in a variable:
name = photolist.name
I only get the first value, same for
name = getfield(photo_list, 'name')
and while
name = [photolist.name]
gives me all values, it does so in one long string without spaces.
using
save('temp.mat', 'photolist')
gives me something I can import to R, but then I need to go multiple nested layers deep to get the values I need, which is a workaround but not very satisfying.
How do i save just the .name fields to a variable?
Found it, was already answered here
names = extractfield(photolist, 'name')
And another way to get the same result is:
names = {photolist.name}
I am trying to use the hash package in R to replicate dictionary behavior in python. I have created it like this,
library(hash)
titles = hash(NAME = list("exact"=list('NAME','Age'), "partial"=list()),
Dt = list("exact"=list('Dt'), "partial"=list()),
CC = list("exact"=list(), "partial"=list()))
I can access the keys in the hash using keys(titles) , values using values(titles), and access values for a particular key using values(titles['Name']).
But how can I access the elements of the inner list? e.g. list('NAME','Age') ?
I need to access the elements based on its names, in this case - "exact" or else I need to know which element of the outer list this element belong to, whether its "exact" or "partial".
Simply:
titles[["NAME"]][["exact"]]
as hrbmstr wrote. There's nothing special about this whatsoever.
In your nested-list, "exact" and "partial" are simply two string keys. Again, there's no special magic significance to their names.
Also, this is in fact the recommended proper R syntax (esp. when the key is variable), it's not "bringing gosh-awful Python syntax".