Guest User

Untitled

a guest
Apr 26th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.27 KB | None | 0 0
  1. BEGIN TRY
  2.  
  3. exec sp_execute_external_script
  4. @language = N'R',
  5. @script = N'
  6. # Summary Mean
  7. #
  8. # Calculates the mean of all independent variables in a table of data
  9. # grouped by code{code}. Note that independent variables are
  10. # identified as all columns matching the following pattern: the letter
  11. # "c" followed by a one-or-more digit number.
  12. #
  13. # @param x Table to summarize
  14. # @importFrom rlang .data
  15. #
  16. # @return Summary table where each distinct code{code} value is
  17. # represented by one row with columns for the respective means of
  18. # each independent variable.
  19. #install.packages("dplyr")
  20.  
  21. x = clsdStudies
  22. code_mean <- function(x) {
  23.  
  24. `%>%` = magrittr:: `%>%`
  25. dplyr::group_by(x, .data$StudyID) %>%
  26. dplyr::summarize_at(dplyr::vars(dplyr::matches("c\d+")), mean)
  27.  
  28. }
  29. # dfcm<- code_mean
  30. ',
  31. @input_data_1 = N'
  32. Select
  33. c.StudyID, c.RespID, c.ProductNumber, c.ProductSequence, c.BottomScaleValue,
  34. c.BottomScaleAnchor, c.TopScaleValue, c.TopScaleAnchor, c.StudyDate,
  35. c.DayOfWeek, c.A, c.B, c.C, c.D, c.E, c.F,
  36. c.DependentVarYN, c.VariableAttributeID, c.VarAttributeName, c.[1] as c1,
  37. c.[2] as c2, c.[3] as c3, c.[4] as c4, c.[5] as c5, c.[6] as c6, c.[7] as c7, c.[8] as c8
  38. from ClosedStudyResponses c
  39. --Sensory Value Attributes only for mean and standard deviation analytics.
  40. where VariableAttributeID = 1
  41. and c.StudyID = @StudyID
  42. ',
  43. @input_data_1_name = N'clsdStudies',
  44. @params = N'@StudyID int',
  45. @StudyID = @StudyID_outer
  46.  
  47. WITH RESULT SETS
  48. (
  49. ("StudyID" int, "RespID" int, "ProductNumber" int, "ProductSequence" int, "BottomScaleValue" varchar(max),
  50. "BottomAnchorValue" varchar(max), "TopScaleValue" varchar(max), "StudyDate" date, "DayOfWeek" varchar(max),
  51. "Code_Mean" int)
  52. )
  53.  
  54. DECLARE @return_value int,
  55. @StudyID_outer int
  56.  
  57. SELECT @StudyID_outer = 21
  58.  
  59. EXEC @return_value = [dbo].[spCodeMeans]
  60. @StudyID = 21,
  61. @StudyID_outer = @StudyID_outer OUTPUT
  62.  
  63. SELECT @StudyID_outer as N'@StudyID_outer'
  64.  
  65. SELECT 'Return Value' = @return_value
  66.  
  67. EXECUTE statement failed because its WITH RESULT SETS clause specified 1 result set(s), but the statement only sent 0 result set(s) at run time.
Add Comment
Please, Sign In to add comment