Replace existng column in MSR - microsoft-r

Why does the following MSR code not replace the original column "Var1"?
rxDataStep(inData = input_xdf, outFile = input_xdf, overwrite = TRUE,
transforms = list(Var1 = as.numeric(Var1)),
transformVars = c("Var1")
)

At the moment, RevoScaleR doesn't support changing the type of a variable in an xdf file (even if you write to a different file). The way to do it is to create a new variable, drop the old, and then rename the new variable to the old name.
I would suggest doing this with a transformFunc (see ?rxTransform for more information), so that you can create the new variable and drop the old, all in one step:
rxDataStep(inXdf, outXdf, transformFunc=function(varlst) {
varlst$Var1b <- as.numeric(varlst$Var1)
varlst$Var1 <- NULL
varlst
}, transformVars="Var1")
# this is fast as it only modifies the xdf metadata, not the data itself
names(outXdf)[names(outXdf) == "Var1b"] <- "Var1"

MSR doesn't allow you to overwrite a variable in place with a different variable type.
You have two options: Write to a different variable or write to a different file. I have added a bit of code that shows that both solutions work as stated in MRS 9.0.1. As stated in the comments, there is some point in earlier versions where this might not work. I am not totally sure where that point is, so the code should let you know.
input_xdf <- "test.xdf"
modified_xdf <- "test_out.xdf"
xdf_data <- data.frame(Var1 = as.character(1:10),
Var2 = 2:11,
stringsAsFactors = FALSE)
rxDataStep(inData = xdf_data,
outFile = input_xdf,
rowsPerRead = 5,
overwrite = TRUE)
rxDataStep(inData = input_xdf,
outFile = input_xdf,
overwrite = TRUE,
transforms = list(Var1b = as.numeric(Var1)),
transformVars = c("Var1")
)
rxGetInfo(input_xdf, getVarInfo = TRUE, numRows = 5)
rxDataStep(inData = input_xdf,
outFile = modified_xdf,
transforms = list(Var1 = as.numeric(Var1)),
transformVars = c("Var1")
)
rxGetInfo(modified_xdf, getVarInfo = TRUE, numRows = 5)

Related

Update SD condition records using FM/BAPI

I am trying to update DATBI field of A* pricing condition tables using the data coming from excel file.I have tried below approaches but unable to update the entry as DATBI is key field. It creates new record instead of updating the existing one. Approaches:
FM: RV_CONDITION_COPY(with mai tain_ mode as B)
FM: RV_CONDITION_MAINTENANCE(using maintain_mode as B)
BAPI_PRICES_CONDITIONS(It changes KONH table but not A* table.Also, as per note#1135984,we shouldn't be using it)
FM:RV_KONDITION_SICHERN_V13A in update mode(This gives sy-subrc 4 in this FM post update A* from table__ syntax as the datbi is new coming from my excel file)
Idoc COND_A04(This also creates new entry instead of updating existing one)
BDC is the only approach that I can think of but looking at VK12 screen, it has been observed that based on key combination and its underlying A* tables, it should be dynamic.
Can you please help in this regard?Is there any dynamic BDC for VK12?
Undoubtedly we shouldn't use BAPI_PRICES_CONDITIONS, and note 94443 describes the problems it can cause when you push incorrect conditions into system, but sometime there is no way to avoid it. I tried this FM and it definitely worked on my system.
Here is the code for updating A009 table:
* header table
APPEND INITIAL LINE TO lt_head ASSIGNING <fs_head>.
<fs_head>-operation = '009'.
<fs_head>-cond_usage = 'A'.
<fs_head>-table_no = '007'.
<fs_head>-applicatio = 'V'.
<fs_head>-cond_type = 'ZP15'.
<fs_head>-valid_from = '20210103'.
<fs_head>-valid_to = '99991231'.
<fs_head>-cond_no = '$000000001'.
<fs_head>-varkey = 'BE110101001000635199993112'.
** items
APPEND INITIAL LINE TO lt_konh ASSIGNING <fs_konh>.
<fs_konh>-operation = '009'.
<fs_konh>-cond_no = '$000000001'.
<fs_konh>-cond_usage = 'A'.
<fs_konh>-table_no = '007'.
<fs_konh>-applicatio = 'V'.
<fs_konh>-cond_type = 'ZP15'.
<fs_konh>-valid_from = '20210103'.
<fs_konh>-valid_to = '99991231'.
<fs_konh>-created_by = sy-uname.
<fs_konh>-creat_date = sy-datum.
APPEND INITIAL LINE TO lt_konp ASSIGNING <fs_konp>.
<fs_konp>-operation = '009'.
<fs_konp>-cond_no = '$000000001'.
<fs_konp>-cond_count = '01'.
<fs_konp>-applicatio = 'V'.
<fs_konp>-cond_type = 'ZP15'.
<fs_konp>-scaletype = 'A'.
<fs_konp>-scalebasin = space.
<fs_konp>-scale_qty = '0'.
<fs_konp>-cond_p_unt = '1'.
<fs_konp>-cond_unit = 'ΠΆ'.
<fs_konp>-calctypcon = 'C'.
<fs_konp>-cond_value = '160'.
<fs_konp>-condcurr = 'EUR'.
CALL FUNCTION 'BAPI_PRICES_CONDITIONS'
EXPORTING
pi_initialmode = abap_true
TABLES
ti_bapicondct = lt_head
ti_bapicondhd = lt_konh
ti_bapicondit = lt_konp
ti_bapicondqs = lt_konm
ti_bapicondvs = lt_konw
to_bapiret2 = lt_return
to_bapiknumhs = lt_knumh
to_mem_initial = lt_buffer
EXCEPTIONS
update_error = 1
OTHERS = 2.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
WAIT = abap_true.
I tried the above code and record with the specified key was properly updated with new valid to field (DATAB). Updating DATBI is a bit more complicated since it is included in the primary key and cannot be updated in one step. You must delete the record with the old DATBI and the create the new one. In this blog you can read more about how to use this BAPI:
https://blogs.sap.com/2019/07/22/how-to-use-bapi_prices_conditions-to-mass-upload-price-conditions/

using paste0 in file name in exams2moodle

I am trying to create a loop to automatize exams generation using the examspackage....
I have created a series of exercices like this
gr1 <- c("ae1_IntroEst_1.Rmd","ae1_IntroEst_2.Rmd","ae1_IntroEst_3.Rmd","ae1_IntroEst_4.Rmd")
gr2 <- c("ae1_IntroProcEst_1.Rmd","ae1_IntroProcEst_2.Rmd","ae1_IntroProcEst_3.Rmd","ae1_IntroProcEst_4.Rmd")
...etc...
Now, I am creating a loop to export all the exercices to moodle xml:
for (i in 1:2){
grupo <- paste0("gr",i)
exams2moodle(grupo, name = paste0("mt1_",i, "_M"), dir = "nops_moodle", encoding = "UTF-8", schoice = list(answernumbering = "none", eval = ee))
}
But I am getting this error:
Error in xexams(file, n = n, nsamp = nsamp, driver = list(sweave = list(quiet = quiet, : The following files cannot be found: gr11.
If I replace "grupo" by "gr1" then it works... (but I am generating 20 exercices). I can't figure it out...
Any ideas?
Thanks!
Because grupo is a string: "gr1". The exams2moodle's first parameter is a string (in your case) and not the list of files (as you want).
If you want use a variable which name is in a string variable, you should use get (get: Return the Value of a Named Object)
Check the sample code:
> x <- 'foo'
> foo <- 'bar'
> x
[1] "foo"
> get(x)
[1] "bar"
>
In your case:
for (i in 1:2){
grupo <- paste0("gr",i)
exams2moodle(get(grupo), name = paste0("mt1_",i, "_M"), dir = "nops_moodle", encoding = "UTF-8", schoice = list(answernumbering = "none", eval = ee))
}

Change a dynamic variable name with rxSetVarInfo

Trying to change a variable name of an XDF with rxSetVarInfo.
I want to merge several data sets with common var names. (I know rxMerge can/will append to filenames where needed. I want to have more control than that.)
This works:
outLetter<- "A"
exp <- list(pct.A = list(newName = paste0("X.pct.",outLetter)))
rxSetVarInfo(varInfo = exp, data = tempXDFFile)
That's where I know the original column name, pct.A. What if that's dynamic? What if this is in a function that gets called several times with different outLetter's. (The "A" isn't hardcoded.)
This does not work:
function(outLetter){
exp <- list(paste0("pct.",outLetter) = list(newName = paste0("X.pct.",outLetter)))
rxSetVarInfo(varInfo = exp, data = tempXDFFile)
}
Nor does:
exp <- parse(text = exp)
rxSetVarInfo(varInfo = exp, data = tempXDFFile)
Yes, I can hardcode all the permutations. Trying to find a more elegant approach.
Please try this code:
dynamicName <- function(outLetter){
exp <- vector(mode="list", length=1)
names(exp) <- paste0("pct.",outLetter)
exp[[paste0("pct.",outLetter)]] = list(newName = paste0("X.pct.",outLetter))
rxSetVarInfo(varInfo = exp, data = tempXDFFile)
}
Before the call to rxSetVarInfo(), "exp" contains:
$pct.A
$pct.A$newName
[1] "X.pct.A"
Running your "this works" case, I see:
> outLetter<- "A"
> exp <- list(pct.A = list(newName = paste0("X.pct.",outLetter)))
>
> exp
$pct.A
$pct.A$newName
[1] "X.pct.A"
Hope this helps!
Note, please make sure that your dynamic naming function has access to the variable "tempXDFFile", you may want to consider passing it as a parameter, like:
dynamicName <- function(outLetter, data){
exp <- vector(mode="list", length=1)
names(exp) <- paste0("pct.",outLetter)
exp[[paste0("pct.",outLetter)]] = list(newName = paste0("X.pct.",outLetter))
rxSetVarInfo(varInfo = exp, data = data)
}

D3 Table Filter in R Shiny update SQL server

I am working on a project to update a SQL database with a Shiny app using D3 Table Filter.
I am able to query the server with different text inputs and the table will render with only those rows. The next step is to edit the table in the shiny app, and have that send a query back to the server to update it.
I have enabled editing in specific columns. How could I make an edit and have it send a query?
Thank you very much in advance.
Here is my code so far:
#install.packages("devtools")
#devtools::install_github("ThomasSiegmund/D3TableFilter")
library(shiny)
library(htmlwidgets)
library(D3TableFilter)
library(RSQLite)
library(RODBCext)
library(sqldf)
dbhandle = odbcDriverConnect(connection = "driver={SQL Server};server= ... ;database= ... ;trusted_connection=true")
fulldata = sqlExecute(dbhandle, "SELECT * FROM ...", fetch = TRUE, stringsAsFactors = FALSE)
ui <- fluidPage(
# Application title
titlePanel("Patient Search"),
sidebarLayout(
sidebarPanel(
textInput(inputId = "Id", label = "Search by Account Number, Date of Birth (YYYY-MM-DD) or Last Name"),
textInput(inputId = "NextAppt", label = "Search by Next Appointment (YYYY-MM-DD)"),
submitButton(text = "Go!")
),
mainPanel(
title = 'Patient Search with D3 Table Filter in Shiny',
fluidRow(
column(width = 12, d3tfOutput('data'))
)
)
)
)
# server.R
# --------------------------------------------------------
server <- shinyServer(function(input, output, session) {
#this reactive will return the row numbers that will need to be returned in our table.
#this could depend on any of our inputs: last name, DoB, account number, or next appointment
search.criteria <- reactive({
out <- c()
outAppt <- c()
if(grepl("\\d{4}\\-\\d{2}\\-\\d{2}", input$Id)==TRUE){
out <- which(fulldata$PatientDOB==input$Id)
print(out)
} else if(grepl("\\d{5}", input$Id)==TRUE){
out <- which(fulldata$AccountNo==input$Id)
} else{
out <- which(fulldata$PatientLastName==toupper(input$Id))
}
# filter for appointment
if(grepl("\\d{4}\\-\\d{2}\\-\\d{2}", input$NextAppt)==TRUE){
outAppt <- which(fulldata$NextAppt==input$NextAppt)
if(length(out)){
out <- intersect(out, outAppt)
}else{
out <- outAppt
}
}
out
})
#make the output table
output$data <- renderD3tf({
# Define table properties
tableProps <- list(
btn_reset = TRUE,
# alphabetic sorting for the row names column, numeric for all other columns
col_types = c("string", rep("number", ncol(fulldata)))
);
d3tf(fulldata[search.criteria(),],
tableProps = tableProps,
extensions = list(
list(name = "sort")
),
showRowNames = TRUE,
tableStyle = "table table-bordered",
#this optional argument enables editing on these specific columns
edit = c("col_49", "col_50", "col_51", "col_52", "col_53"));
})
#NEED TO ADD SOMETHING HERE TO SEND QUERY TO SERVER WHEN USER EDITS
})
runApp(list(ui=ui,server=server))
I used rhandsontable. It works better as you can convert the output using hot_to_r. But because of it's simple excel like formatting, it's difficult to render images like DT
If only data, go ahead and use rhandsontable.
Eg.
rhandsontable(df) %>%
hot_cols(colWidths = c(80,150,80,80,80,80,200,200,80,80,300,80,80), manualColumnResize = TRUE) %>%
hot_col(2:13, renderer = "html") %>%
hot_col(2:13, renderer = htmlwidgets::JS("safeHtmlRenderer")) %>%
hot_col(1, renderer = "
function(instance, td, row, col, prop, value, cellProperties) {
var escaped = Handsontable.helper.stringify(value),
img;
if (escaped.indexOf('http') === 0) {
img = document.createElement('IMG');
img.src = value;
Handsontable.dom.addEvent(img, 'mousedown', function (e){
e.preventDefault(); // prevent selection quirk
});
Handsontable.dom.empty(td);
td.appendChild(img);
}
else {
// render as text
Handsontable.renderers.TextRenderer.apply(this, arguments);
}
return td;
}")
})
observeEvent(input$submitComments, {
a = hot_to_r(input$upcomingAuctionsTable)
# sqlSave(myConnUpcom, a, tablename = "test", rownames = FALSE, varTypes = c(date = "varchar(255)"))
sqlUpdate(myConnUpcom, a, tablename = "temp", index = "item_id")
})

multiple selectizeInput from fileInput

I would like to have a user input a file (.csv) and from that file, two selectizeInputs will populate with the column names of the .csv. One will ask the user which of the columns from their uploaded file is the y-variable and which of the columns is the x-variable. This, I was able to do.
What I cannot do is the following: I would like to get the selection from their y-variable to disappear from their x-variable choices in the x-variable drop-down menu.
Also, I've used the answer to this question to try to help, but they are not using the values from a fileInput. As such, I cannot get my code, which is below, to work. Thank you for any advice/help you can give.
ui<- fluidPage(
titlePanel("Test"),
sidebarPanel(
fileInput(inputId = "file1", label = "Upload File"),
selectizeInput(
"sampleyvars", "Y-vars", choices = NULL, multiple = FALSE
),
selectizeInput(
"samplevars", "X-vars", choices = NULL, multiple = TRUE
)
),
mainPanel(h3("Nothing special")
)
)
server<- function(input, output, session) {
observe({
file1 <- input$file1
if(is.null(file1)){return()}
dataSet <- read.csv(file=file1$datapath)
vals1<-input$sampleyvars
vals2<-input$samplevars
updateSelectizeInput(session, "sampleyvars",
choices = colnames(dataSet)[! vals1 %in% vals2])
updateSelectizeInput(session, "samplexvars",
choices =colnames(dataSet)[! vals2 %in% vals1])
})
}
shinyApp(ui = ui,server = server)
You had wrong ID of the widget for X variable: samplevars instead of samplexvars which you used in update* function. I changed it to the latter ID and also slightly tweaked your code to get the desired effect.
Full example:
ui<- fluidPage(
titlePanel("Test"),
sidebarPanel(
fileInput(inputId = "file1", label = "Upload File"),
selectizeInput(
"sampleyvars", "Y-vars", choices = NULL, multiple = FALSE
),
# you had ID here wrong
selectizeInput(
"samplexvars", "X-vars", choices = NULL, multiple = TRUE
)
),
mainPanel(h3("Nothing special")
)
)
server<- function(input, output, session) {
data <- reactive({
file1 <- input$file1
req(file1)
dataSet <- read.csv(file=file1$datapath)
vars <- colnames(dataSet)
updateSelectizeInput(session, "sampleyvars", "Y-vars",
choices = vars, selected = vars[1])
updateSelectizeInput(session, "samplexvars", choices = vars[-1], selected = vars[2])
dataSet
})
observe({
varX <- colnames(data())
varX <- varX[!(varX %in% input$sampleyvars)]
updateSelectizeInput(session, "samplexvars", "X-vars", choices = varX)
})
}
shinyApp(ui = ui,server = server)